中興通訊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);

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章