程序代码:
#include <stdio.h> #include <malloc.h> struct list{ int data; struct list *next; }; struct list *create(){ int n; struct list *head,*p,*q; head=(struct list *)malloc(sizeof(struct list)); head->next=NULL; q=head; printf("输入链表(0为结束):\n"); scanf("%d",&n); while(n<=0){ printf("值不正确\n"); scanf("%d",&n); } while(n>0){ p=(struct list *)malloc(sizeof(struct list)); p->data=n; p->next=NULL; q->next=p; q=p; printf("下一个"); scanf("%d",&n); } return head; } void shan(struct list *head){//删除链表中相等的元素 struct list *p,*q,*s; q=head->next; while(q){//printf("a"); // q->next 改成q p=q->next; s=q; while(p){//printf("b"); if(q->data==p->data){ s->next=p->next; free(p); p=s->next;//printf("c"); }else{//printf("d"); p=p->next; s=s->next; } }//printf("e"); q=q->next;//printf("f"); }//printf("d"); } void print(struct list *head){ while(head->next){ printf("%3d",head->next->data); head=head->next; } } void main(){ struct list *head; head=create(); print(head); printf("\n"); // zhuanzhi1(head); shan(head); print(head); }
梅尚程荀
马谭杨奚