P53.2.(3)將兩個遞增的有序鏈表合併爲一個遞增的有序鏈表。要求結果鏈表仍使用原來兩個鏈表的存儲空間, 不另外佔用其它的存儲空間。表中不允許有重複的數據。

一把辛酸淚啊。。。。。。本人之前用的是DEVC++,可能是本人太菜,把這個編譯器給整出問題了,一直給我報錯,我就把它給卸載了,滿以爲換上了宇宙第一IDE就會過上幸福快樂的生活,沒想到是噩夢的開始。。。。。。。可能是本人太菜用VS2010時非常的刺手,一開始寫hello world給我各種報錯,我就去找錯誤源,我的天。。。。。兼容性、語言標準、編譯頭。。。。。後面寫好程序後找bug,我暈喲你給我報的都是些什麼錯?????0xc00000000005這一類的我看不懂 啊!!!!!!然後又去搜各種VS調試教程,我覺得這三天時間是最艱難的,充分感受到了開發環境的重要性。吐槽歸吐槽,不過還是得承認VS遠比我想象中強大!調試功能炒雞棒,雖然一直虐我。。。。。。

 

不說題外話了。這個題的算法比較簡單,但是爲了調用這個算法以驗證它的正確性,我將可能使用的鏈表的一些操作與之和放在一個project當中,以便以後再寫鏈表相關算法時可以直接調用相關函數而不用再重寫基礎操作的函數實現新算法。

1.工程文件結構:

2.這個題的算法(放在了LinkList.cpp中):

LinkList Get_Intersection_Num(LinkList &La, LinkList &Lb, LinkList &Lc)
{
    if(!La || !Lb)
    {
        cout<<"有空鏈表,無法求相交元素";
        return NULL;
    }

    LNode *pa, *pb, *pc, *temp;
    Lc = La;
    pa = La -> next;
    pb = Lb -> next;
//    pc = Lc -> next;

    while(pa && pb)
    {
        if(pa -> data == pb -> data)
        {
//             pc = pa;            

            temp = pb;
            pa = pa -> next;
            pb = pb -> next;
        }
        else if(pa -> data > pb -> data)
        {
            temp = pb;
            pb = pb -> next;
        }
        else
        {
            temp = pa;
            pa = pa -> next;
        }
        free(temp);
        temp = NULL;
    }
    while(pa)
    {
        temp = pa;
        pa = pa -> next;
        free(temp);
    }
    while(pb)
    {
        temp = pb;
        pb = pb -> next;
        free(temp);
    }
    return Lc;
}

3.main.cpp文件內容:

#include <iostream>
#include <stdlib.h>
#include "LinkList.h"


using namespace std;

int main()
{
	LinkList L1, L2, L3;
	int len1, len2;

	cout<<"\n輸入L1鏈表的長度:";
	cin>>len1;
	Creat_LinkList(L1, len1);
//	cout<<"\n打印L1元素:";
//	PrintLinkList(L1);

	cout<<"\n輸入L2鏈表的長度";
	cin>>len2;
	Creat_LinkList(L2, len2);
//	cout<<"\n打印L2元素:";
//	PrintLinkList(L2);

	Get_Intersection_Num(L1, L2, L3);
	cout<<"\n打印交集中的元素:\n";
	PrintLinkList(L3);


	system("PAUSE");
}

3.LinkList.h文件內容:

#ifndef LINKLIST_H
#define LINKLIST_H

const int TRUE = 1;
const int FALSE = 0;
typedef int Status;

typedef struct node 
{
	int data;
	node* next;
	int length;
}LNode, *LinkList;

void Creat_LinkList(LinkList &L, int len);
void Mesh_LinkLists(LinkList &L1, LinkList &L2);//L1和L2爲遞增序列,都有頭結點,將L2中的值按序插入L1中 
LinkList Get_Intersection_Num(LinkList &La, LinkList &Lb, LinkList &Lc);
void PrintLinkList(LinkList &L);

#endif

4.LinkList.cpp文件內容(包含了創建鏈表,打印鏈表元素等操作)

#include <iostream>
#include <malloc.h>
#include "LinkList.h"

using namespace std;
void Creat_LinkList(LinkList &L, int len)
{ 
	if(len <= 0)
	{
		cout<<"\n數據不合法!";
		exit(1);
	}
	
	int temp;
	LNode* head; 
	LNode* p; 
	
	//建立空鏈表 
	head = new LNode;
	head -> next = NULL;
	head -> length = len;

	L = head;
	p = head;
	
	//輸入數據存入鏈表 
	cout<<"\n輸入鏈表元素:";
	for(int i = 0; i < len; i++)
	{
		
		LNode* new_node = new LNode;
		cin>>temp;
		new_node -> data = temp;
		new_node -> next = NULL;
		new_node -> length = len;

		p -> next = new_node;
		p = p -> next; 
	}
	
	return;
}

void Mesh_LinkLists(LinkList &L1, LinkList &L2)//L1和L2爲遞增序列,都有頭結點,將L2中的值按序插入L1中 
{
	if(!L1)//L1爲空鏈表 
	{
		L1 = L2;
		return;
	}

	LinkList p1, p2, p3;
	p1 = L1 -> next;
	p2 = L2 -> next;
	p3 = L1;
	
	while(p1 && p2)
	{
		if(p1 -> data <= p2 -> data)
		{
			p3 -> next = p1;
			p1 = p1 -> next;
		}
		else 
		{
			p3 -> next = p2;
			p2 = p2 -> next;
		}
		p3 = p3 -> next;
	}
	
	//長鏈表較短鏈表多出的部分接在合成鏈表的後面 
	if(p1 != NULL)
	{
		p3 -> next = p1;
	}
	if(p2 != NULL)
	{
		p3 -> next = p2;
	}
	return; 
}


LinkList Get_Intersection_Num(LinkList &La, LinkList &Lb, LinkList &Lc)
{
	if(!La || !Lb)
	{
		cout<<"有空鏈表,無法求相交元素";
		return NULL;
	}

	LNode *pa, *pb, *pc, *temp;
	Lc = La;
	pa = La -> next;
	pb = Lb -> next;
//	pc = Lc -> next;

	while(pa && pb)
	{
		if(pa -> data == pb -> data)
		{
//	 		pc = pa;			

			temp = pb;
			pa = pa -> next;
			pb = pb -> next;
		}
		else if(pa -> data > pb -> data)
		{
			temp = pb;
			pb = pb -> next;
		}
		else
		{
			temp = pa;
			pa = pa -> next;
		}
		free(temp);
		temp = NULL;
	}
	while(pa)
	{
		temp = pa;
		pa = pa -> next;
		free(temp);
	}
	while(pb)
	{
		temp = pb;
		pb = pb -> next;
		free(temp);
	}
	return Lc;
}


void PrintLinkList(LinkList &L)
{
	LinkList p;
	p = L -> next ;
	
	while(p != NULL)
	{
		cout<<p -> data<<endl;
		p = p -> next;
	}
	return;   
}

5.測試結果: 

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