中兴通讯2012笔试题删除双向循环链表中相同的数值

双向循环链表pHeadA ,pHeadB,

例如 pHeadA 数据为 1,2,3,4,5,7 pHeadB数据为 1,2,6,2,1,7,3

删除相同数值后为  pHeadA 4,5  pHeadB 6

#include <iostream>
using namespace std;
typedef struct DoubleLinkNode
{
	int data;
    struct DoubleLinkNode *pre ,*next;
}node;
void findx(node *pHeadA,node *pHeadB)
{
	node *pA,*pB,*pA_1,*pB_1;
	int x;
	for(pA=pHeadA->next;pA!=pHeadA;pA=pA->next)
	{
		for(pB=pHeadB->next;pB!=pHeadB;pB=pB->next)
		{
			if((pA->data == pB->data))
			{
				x = pA->data;
				cout<<"x="<<x<<endl;
				for(pA_1=pHeadA->next;pA_1!=pHeadA;pA_1=pA_1->next)
				{
					if(pA_1->data==x)
					{
						pA_1->pre->next = pA_1->next;
						pA_1->next->pre = pA_1->pre;
						
						pA_1 = pA_1->next;
						 
					}
				}
				for(pB_1=pHeadB->next;pB_1!=pHeadB;pB_1=pB_1->next)
				{
					if(pB_1->data==x)
					{
						pB_1->pre->next = pB_1->next;
						pB_1->next->pre = pB_1->pre;
						 
						pB_1 = pB_1->next;
						
					}
				}
				pA = pHeadA;
			}
		}
	}
}

void main()
{
	node *heada,*headb,*p0,*p1,*p2,*p3,*p4,*p5,*s0,*s1,*s2,*s3,*s4,*s5;
	heada = (node*)malloc(sizeof(node));
	headb = (node*)malloc(sizeof(node));
	p0 = (node*)malloc(sizeof(node));
	p1 = (node*)malloc(sizeof(node));
	p2 = (node*)malloc(sizeof(node));
	p3 = (node*)malloc(sizeof(node));
	p4 = (node*)malloc(sizeof(node));
	p5 = (node*)malloc(sizeof(node));

	s0 = (node*)malloc(sizeof(node));
	s1 = (node*)malloc(sizeof(node));
	s2 = (node*)malloc(sizeof(node));
	s3 = (node*)malloc(sizeof(node));
	s4 = (node*)malloc(sizeof(node));
	s5 = (node*)malloc(sizeof(node));
	 
	p0->data = 1;
	p1->data = 2;
	p2->data = 5;
	p3->data = 4;
	p4->data = 3;
	p5->data = 7;

	s0->data = 1;
	s1->data = 5;
	s2->data = 1;
	s3->data = 7;
	s4->data = 2;
	s5->data = 3;
	 
	heada->next = p0;
	p0->next = p1;
	p1->next = p2;
	p2->next = p3;
	p3->next = p4;
	p4->next = p5;
	p5->next = heada;

	heada->pre = p5;
	p5->pre = p4;
	p4->pre = p3;
	p3->pre = p2;
	p2->pre = p1;
	p1->pre = p0;
	p0->pre = heada;

	headb->next = s0;
	s0->next = s1;
	s1->next = s2;
	s2->next = s3;
	s3->next = s4;
	s4->next = s5;
	s5->next = headb;
	headb->pre = s5;
	s5->pre = s4;
	s4->pre = s3;
	s3->pre = s2;
	s2->pre = s1;
	s1->pre = s0;
	s0->pre = headb;
    findx(heada ,headb);

}


 

 

#include <iostream>
using namespace std;
typedef struct DoubleLinkNode
{
	int data;
    struct DoubleLinkNode *pre ,*next;
}node;
node *create(int n)
{
	node *head = new node();
	node *p =head;
	int x;
	while(n)
	{
		node *s = new node();
		printf("请输入数据:");
		scanf("%d",&x);
		s->data = x;
		p->next = s;
		s->pre  = p;
		p = s;
		n--;


	}
	p->next = head;
	head->pre = p;
	return head;
}

void findx(node *pHeadA,node *pHeadB)
{
	node *pA,*pB,*pA_1,*pB_1;
	int x;
	for(pA=pHeadA->next;pA!=pHeadA;pA=pA->next)
	{
		for(pB=pHeadB->next;pB!=pHeadB;pB=pB->next)
		{
			if((pA->data == pB->data))
			{
				x = pA->data;
				cout<<"x="<<x<<endl;
				for(pA_1=pHeadA->next;pA_1!=pHeadA;pA_1=pA_1->next)
				{
					if(pA_1->data==x) //找到链表A中的数据x,然后删除其中的数据x,
					{
						pA_1->pre->next = pA_1->next;//把节点pA_1前一个指向其pA_1的后一个
						pA_1->next->pre = pA_1->pre;//把pA_1的后一个节点指向的前缀指向pA_1的前一个节点
						//相当于把节点的前后节点进行了连接,把节点pA_1进行了孤立。
						pA_1 = pA_1->pre;//此时把pA_1设为它的原来的前一个节点,等到for循环时可以直接到下一个
						 
					}
				}
				for(pB_1=pHeadB->next;pB_1!=pHeadB;pB_1=pB_1->next)
				{
					if(pB_1->data==x)
					{
						pB_1->pre->next = pB_1->next;
						pB_1->next->pre = pB_1->pre;
						 
						pB_1 = pB_1->pre;
						
					}
				}
				pA = pHeadA;
			}
		}
	}
}

void main()
{
 

	node *heada ,*headb;
	int x,y;
	printf("请输入个数:");
	scanf("%d",&x);
	heada = create(x);
	printf("请输入个数:");
	scanf("%d",&y);
	headb = create(y);
         findx(heada ,headb);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值