求助用C语言编写线性表的顺序储存结构与链式储存结构的插入与删除代码
求助用C语言编写线性表的顺序储存结构与链式储存结构的插入与删除代码
程序代码:
void Insert() { struct Student *p1,*p2; int point; int i=1; system("cls"); p1=head; if (n==0) { printf("这是一张空表,返回菜单\n"); return; } printf("请输入插入节点的位置,现在共有%d个节点(输入0或小于-1退出,输入-1输出链表)\n",n); if (scanf("%d",&point)&&point<=0&&point!=-1) return; fflush(stdin); if (point==-1) { print(); return Insert(); } if (point>n) { printf("找不到该节点,请重新输入\n\n"); fflush(stdin); Insert(); } printf("请输入学生的学号和成绩(学号输入0或负数退出)\n"); printf("%d--",n+1); p2=malloc(LEN); if (scanf("%d%d",&p2->num,&p2->score)&&p2->num<=0) return; n++; while (1) { if (point==1) { p2->next=head; head->back=p2; head=p2; head->back=NULL; return Insert(); } if (point==n-1) { end->next=p2; p2->back=end; end=p2; end->next=NULL; return Insert(); } if (point!=i++) p1=p1->next; else { p2->next=p1; p2->back=p1->back; p1->back->next=p2; p1->back=p2; return Insert(); } } } void Delete() { struct Student *p=head; int point,i=1; system("cls"); if (n==0) { printf("这是一张空表,返回菜单\n"); return; } printf("请输入需要删除的节点编号(现在共有%d个节点,输入0或小于-1退出,输入-1输出链表)\n",n); if (scanf("%d",&point)&&point<=0&&point!=-1) return; fflush(stdin); if (point==-1) { print(); return Delete(); } if (point>n) { printf("找不到该节点,请重新输入\n\n"); fflush(stdin); return Delete(); } while (1) { if (point==1&&n!=1) { head=head->next; free(head->back); head->back=NULL; n--; return Delete(); } if (point==n&&n!=1) { end=end->back; free(end->next); end->next=NULL; n--; return Delete(); } if (n==1) { free(head); head=NULL; n--; printf("这是一张空表,返回菜单\n"); return; } if (point!=i++) p=p->next; else { p->back->next=p->next; p->next->back=p->back; free(p); n--; return Delete(); } } }