//
#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;
}
单链表中删除重复元素的结点
最新推荐文章于 2021-11-17 19:17:45 发布