有一个双向循环链表链表1和链表2,将这两个链表中重复的元素删除,并将这两个链表中相同的元素删除。例如链表1中输入的元素是2,1,3,4,1;链表2是3,1,4,5,6,;则输出链表1为2,;链表2为5,6;
算法思想:在链表1中查找重复的元素,如果有的话将重复的第二个以上的元素删除,留一个元素,之后将这个元素与链表2的所有元素比较,如果链表2中有元素与这个元素相等就将链表2的这个元素删除,无论链表2有无元素与这个元素相等,都将链表1中的这个元素删除。之后再链表2将自身重复的元素删除既可。
#include<stdio.h>
#include<stdlib.h>
struct stud
{
int data;
struct stud *front,*next;
};
void main()
{
struct stud *head1,*head2,*p,*q,*s1,*s2,*s,*q1;
head1=(struct stud *)malloc(sizeof(struct stud));
head2=(struct stud *)malloc(sizeof(struct stud));
head1->next=head1;
printf("请输入一个数/n");
p=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&p->data);
if(p->data!=1000)
{
head1->next=p;
p->front=head1;
p->next=head1;
head1->front=p;
}
printf("请输入一个数/n");
p=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&p->data);
while(p->data!=1000)
{
head1->next->front=p;
p->next=head1->next;
head1->next=p;
p->front=head1;
printf("请输入一个数/n");
p=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&p->data);
}
head2->next=head2;
printf("现在给第二个链表赋值/n");
printf("请输入一个数/n");
q=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&q->data);
if(q->data!=1000)
{
head2->next=q;
q->front=head2;
q->next=head2;
head2->front=q;
}
printf("请输入一个数/n");
q=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&q->data);
while(q->data!=1000)
{
head2->next->front=q;
q->next=head2->next;
head2->next=q;
q->front=head2;
printf("请输入一个数/n");
q=(struct stud *)malloc(sizeof(struct stud));
scanf("%d",&q->data);
}
printf("现在输出第一个链表/n");
p=head1->next;
while(p!=head1)
{
printf("%d/t",p->data);
p=p->next;
}
printf("/n现在输出第二个链表/n");
q=head2->next;
while(q!=head2)
{
printf("%d/t",q->data);
q=q->next;
}
p=head1->next;
q=head1->next;
while(p!=head1)
{
int data,count=0,flag=0;
data=p->data;
count=0;
flag=0;
q=head1->next;
while(q!=head1)
{
if(data==q->data)
{
count++;
if(count>=2)
{
s=q;
q=q->next;
s->next->front=s->front;
s->front->next=s->next;
free(s);
}
else
q=q->next;
}
else
q=q->next;
}
q1=head2->next;
while(q1!=head2)/*链表2中有与链表1中相同的元素就将它删掉*/
{
if(p->data==q1->data)
{
flag=1;
s1=q1;
q1=q1->next;
s1->front->next=s1->next;
s1->next->front=s1->front;
free(s1);
}
else
q1=q1->next;
}
if(count>=2||flag))/*算法关键所在,若在在链表1中的一个数,在链表1中出现了2次,或在链表2中出现过,就将链表1中这个数删除掉*/
{
s=p;
p=p->next;
s->next->front=s->front;
s->front->next=s->next;
free(s);
}
else
p=p->next;
}
p=head2->next;
q=head2->next;
while(p!=head2)
{
int data,count=0,flag=0;
data=p->data;
count=0;
flag=0;
q=head2->next;
while(q!=head2)
{
if(data==q->data)
{
count++;
if(count>=2)
{
s=q;
q=q->next;
s->next->front=s->front;
s->front->next=s->next;
free(s);
}
else
q=q->next;
}
else
q=q->next;
}
if(count>=2)/*将链表2中的重复元素删除*/
{
s=p;
p=p->next;
s->next->front=s->front;
s->front->next=s->next;
free(s);
}
else
p=p->next;
}
printf("/n删除两个链表中相同的元素之后的结果是,现在输出第一个链表/n");
p=head1->next;
while(p!=head1)
{
printf("%d/t",p->data);
p=p->next;
}
printf("/n现在输出第二个链表/n");
q=head2->next;
while(q!=head2)
{
printf("%d/t",q->data);
q=q->next;
}
printf("/n现在开始释放内存/n");
p=head1->next;
while(p!=head1)
{
s=p;
p=p->next;
free(s);
}
printf("链表1释放完毕/n");
p=head2->next;
while(p!=head2)
{
s=p;
p=p->next;
free(s);
}
printf("链表2释放完毕/n");
}
延伸阅读: