这是我编写的程序,是关于创建链表,删除,插入等等问题!但是就是有些错误,改来改去还是错!!请您帮忙修改一下好吗?? #include <stdio.h> #include <malloc.h> typedef struct node *point; struct node { int data; point next; }; typedef point list;
list creat() { point p,q; list head; int x,i=0; head=(struct node *)malloc(sizeof(struct node)); p=head; printf("请输入数字:"); scanf("%d",&x); while(x!=0) { q=(struct node *)malloc(sizeof(struct node)); q->data=x; p->next=q; p=q; scanf("%d",&x); } p->next=NULL; p=head; while(p->next) { printf("%d->",p->next->data); p=p->next; } printf("NULL\n"); return head; }
int length(point p) { int i=0; while(p->next!=NULL) { p=p->next; i++; } return i; }
list cha(list head,point p) { int x,i=0,y; point q; printf("\n你想插入什么数:"); scanf("%d",&y); printf("插在第几位,请输入序号:"); scanf("%d",&x); while(i<x-1) { p=p->next; i++; } q=(struct node *)malloc(sizeof(struct node)); q->data=y; q->next=p->next; p->next=q; return head; }
list end(list head,point p) { point q; int x,j=0; printf("请输入要删除的数:"); scanf("%d",&x); while(p->next) { if(p->next->data==x) { p->next=p->next->next; q=p->next; free(q); } else {p=p->next;j++;} } p=head; if(j==length(p))printf("你输入的数不存在"); printf("删除后表变成:"); while(p->next) { p=p->next; printf("%d->",p->data); } printf("NULL\n\n"); return head; }
void swb(list head,point p) { point w,q; while(p->next) { w=p; w=w->next; while(w->next) if(w->next->data==p->next->data) { w->next=w->next->next; q=w->next; free(q); } else w=w->next; p=p->next; } p=head; printf("删除重复数据,最终结果:"); while(p->next) { p=p->next; printf("%d->",p->data); } printf("NULL\n\n"); }
void main() { int leng; list head; point p; head=creat(); p=head; leng=length(p); printf("\n表长是:%d\n",leng); head=cha(head,p); p=head; printf("插入后表变为:"); while(p->next) { p=p->next; printf("%d->",p->data); } printf("NULL\n\n"); p=head; head=end(head,p); p=head; swb(head,p); }