链表问题求助,唯一化函数出了些问题
唯一化函数LDulipcate()停在第一次循环,打印出“1”后就不动了如图

#include<stdio.h> #include<stdlib.h> //类型声明 //节点数据 typedef int ElemType; //节点 typedef struct Node{ ElemType data; struct Node *next; }Node; //链表 typedef struct List{ int size; Node header; Node tailer; }List; int main(void){ List l,*lp1,; lp1=_ListInit(); lp1=ListInsertData(lp1,20); lp1=ListInsertData(lp1,20); lp1=ListInsertData(lp1,20); lp1=ListInsertData(lp1,40); lp1=ListInsertData(lp1,40); printf("\n---------\nlp1:\n"); ListPrint(lp1); lp1=ListDeduplicate(lp1); printf("\n---------\nlp1:\n"); ListPrint(lp1); return 0; } //链表建立 List *_ListInit(void){ List *L; L=(List *)malloc(sizeof(List)); L->header.next=&(L->tailer); L->tailer.next=NULL; L->size=0; L->header.data=0; L->tailer.data=0; printf("List create successfully!\n"); return L; } //链表插入,接受数据 //作为头指针的下一个节点插入 List *ListInsertData(List *L,ElemType Data){ if (!L)printf("Error!"); Node N; N.next=L->header.next;//将原首元素接到临时指针上 L->header.next=(Node *)malloc(sizeof(Node));//头元素的指针域生成新节点 L->header.next->data=Data;//赋值 L->header.next->next=N.next;//原来的节点接到新增节点后 L->size++; return L; } //链表唯一化 List *ListDeduplicate(List *L){ Node *temp,*N=L->header.next; //从首元素开始 ElemType Data; int i=1; while(NULL!=N->next){//到达尾节点 N=L->header.next; Data=N->data; while(NULL!=N->next){ N=N->next; if(N->data==Data){ printf("%d\n",i);i++; temp=N->next->next; N->next=temp; free(temp); L->size--; } } } return L; } //遍历打印 void ListPrint(List *L){ if(!L)printf("Error!"); if(L->size==0)printf("Empty List!\n"); Node *N=L->header.next;//临时指针指向首元素 int i=1; printf("List length:%d\n",L->size); while(NULL!=N->next){ printf("number:%d\tdata:%d\n",i,N->data); N=N->next; i++; } printf("---------\n"); }
typedef int ElemType; //节点 typedef struct Node{ ElemType data; struct Node *next; }Node; //链表 //链表长度,头节点,尾节点 typedef struct List{ int size; Node *header; Node *tailer; }List; List *ListDeduplicate(List *L){ Node *temp,*N=L->header->next,*M=L->header->next; //从首元素开始 ElemType Data; if(NULL==N->next){printf("Elmty List!\n");return L;} while(NULL!=M->next->next){ //首元素数据参与操作,不需要移动到尾元素 Data=M->data; N=M; while(NULL!=N->next->next){//末元素数据参与操作 N=N->next; if(N->data==Data){ temp=N->next; N->next=temp->next; free(temp); L->size--; } } M=M->next; } return L; }
List *ListDeduplicate(List *L){ Node *temp,*N=L->header->next,*M=L->header->next; //从首元素开始 ElemType Data; if(NULL==N->next){printf("Elmty List!\n");return L;}//判空 while(NULL!=M->next->next){ Data=M->data;//确定需要处理的数据 N=M;//从M的下一个节点开始,找到重复元素,直到最后一个有数据的元素 while(NULL!=N->next->next){ N=N->next; if(N->data==Data){ temp=N->next;//删除重复节点 N->next=temp->next; free(temp); L->size--; } } M=M->next; } return L; }
[此贴子已经被作者于2017-8-20 07:04编辑过]
List *ListDeduplicate(List *L){ Node *temp, *prep, *N, *M = L->header.next; //从首元素开始 ElemType Data; if(NULL==M->next){printf("Elmty List!\n");return L;}//判空 while(NULL!=M->next){ Data=M->data;//确定需要处理的数据 prep = M; N = M->next; while(NULL!=N->next){ if(N->data==Data){ prep->next = N->next; free(N); L->size--; N = prep->next; }else{ prep = N; N = N->next; } } M = M->next; } return L; }
[此贴子已经被作者于2017-8-21 12:50编辑过]