单链表中删除重复元素的结点

来源:互联网 发布:怎样看手机淘宝等级 编辑:程序博客网 时间:2024/05/17 02:30
//#include<iostream>using namespace std;typedef int dataType;struct node{       dataType data;       node * next;       };node *deletereplace(node *&head){     if (head == NULL || head->next->next == NULL || head->next == NULL)     return head; node *p = head->next; while (p != NULL) { node *s = p; while(s->next != NULL) { if (s->next->data == p->data) { node *r = s->next; s->next = s->next->next; free(r); } else s = s->next; } p = p->next; } return head;}node *creatlist(int n){     node *head = (node*)malloc(sizeof(node));     head->next = NULL;     node *p = head;     for (int i = 0; i < n; i++)     {         dataType x;         cin >> x;         node *s = (node*)malloc(sizeof(node));         s->data = x;         s->next = NULL;         p->next = s;         p = s;     }     return head;    }int show(node *head){    if (head == NULL || head->next == NULL)    return -1;    node *p = head->next;    while (p)    {          cout << p->data << ends;          p = p->next;    }    cout << endl;}int main(){    node *h = creatlist(5);    show(h);    node *w = deletereplace(h);    show(w);    system("pause");    return 0;    }

0 0