删除数组内相同的数字
程序代码:
#include<stdio.h> #include<stdlib.h> struct arraylist{ int data; struct arraylist *next; }; typedef struct arraylist list; list *head,*work,*end; list *newlist(); int print_list(list *); int delet_list(list *); int i,equal;; int main(){ printf("请依次输入您需要判断的数字请以-1结束\n"); list *start; start=newlist(); print_list(head); delet_list(head); return 0; } list *newlist(){ head=(list *)malloc(sizeof(list)); head->next=NULL; end=head; scanf("%d",&i); while(i!=-1){ work=(list *)malloc(sizeof(list)); work->data=(int)i; work->next=NULL; end->next=work; end=work; scanf("%d",&i); } return head; } int print_list(list *head){ work=head->next; if(work==NULL) printf("您没有输入任何数字\n"); else{ printf("您输入的数字分别是:"); do{ printf("%d,",work->data); work=work->next; }while(work); } printf("\n"); return 0; } int delet_list(list *head){ list *p,*q; end=head->next; while(end){ work=end->next; p=end; while(work){ if(work->data==end->data){ p->next=work->next; q=work; work=work->next; free(q); }else{ p=p->next; work=work->next; } } end=end->next; } work=head->next; printf("删除相同的数后结果为:"); do{ printf("%d,",work->data); work=work->next; }while(work); printf("\n"); return 0; } [local]1[/local]