赞
踩
- #include<assert.h>
- typedef int SXBint;
- typedef struct SL
- {
- SXBint* a;
- int size;
- int capacity;
- }SLnode;
- void SeqLsitInit(SLnode* ps)
- {
- ps->a = NULL;
- ps->capacity = ps->size = 0;
- }
- void kuorong(SLnode* ps)
- {
- if (ps->capacity == ps->size)
- {
- int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- SXBint* temp = (SXBint*)realloc(ps->a, sizeof(SLnode) * Newcapacity);
- if (temp == NULL)
- {
- perror("error:");
- exit(1);
- }
- ps->a = temp;
- ps->capacity = Newcapacity;
- }
- }
- void SeqPushback(SLnode* ps, SXBint x)
- {
- kuorong(ps);
- ps->a[ps->size++] = x;
- }
- void Seq_dayin(SLnode ps)
- {
- for (int i = 0; i < ps.size; i++)
- {
- printf("%d->", ps.a[i]);
- }
- printf("NULL\n");
- }
- void deletes(SLnode* ps, int item) {
- int j = 0;
- for (int i = 0; i <= ps->size;i++) {
- if (ps->a[i] != item)
- {
- ps->a[j] = ps->a[i];
- ++j;
- }
- }
- ps->size = j - 1;
- }
-
- int main()
- {
- SLnode S;
- SeqLsitInit(&S);
- int n, num, item;
- printf("请输入顺序表的长度\n");
- scanf("%d", &n);
- printf("请输入顺序表的元素\n");
- for (int i = 0; i < n; i++)
- {
- scanf("%d", &num);
- SeqPushback(&S, num);
- }
- printf("顺序表的元素如下\n");
- Seq_dayin(S);
-
- printf("请输入item的值\n");
- scanf("%d", &item);
- deletes(&S, item);
- printf("删除item后\n");
- Seq_dayin(S);
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。