将两条有序单链表合成一条链表(两种写法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40990854/article/details/82749099

整体代码:

typedef
struct Node {
	int data;
	struct Node * next;
}Node;

class SingleList
{
	Node head;
	int length;
	static int I;
public:
	SingleList();
	void create(int len);
	void insert(int index,int data);
	void Delete(int index);
	int find(int index);
	int size();
	void print();
	void merge(SingleList list);
	~SingleList();
};

#include "SingleList.h"


SingleList::SingleList()
{
	head.next = NULL;
	head.data = 0;
	length = 0;
}

void SingleList::create(int len)
{
	for (int i = 0; i < len; i++)
	{
		int num;
		cin >> num;
		insert(i, num);
	}
}

void SingleList::insert(int index,int data)
{
	Node * p_head = &head;
	while (index --)
	{
		p_head = p_head->next;
	}
	Node * nodeTemp = new Node;
	nodeTemp->data = data;
	nodeTemp->next = p_head->next;
	p_head->next = nodeTemp;
	length++;
}

void SingleList::Delete(int index)
{
	Node * p_head = &head;
	index++;
	while (index --)
	{
		p_head = p_head->next;
	}
	Node * tmpPtr = p_head->next;
	p_head->next = p_head->next->next;
	delete tmpPtr;
	length--;
}

int SingleList::find(int index)
{
	if (index < 0 || index > length) {
		return 0;
	}
	Node * tmpPtr = head.next;
	while (index --)
	{
		tmpPtr = tmpPtr->next;
	}
	return tmpPtr->data;
}

int SingleList::size()
{
	return length;
}

void SingleList::print()
{
	Node * p_head = head.next;
	while (p_head != NULL)
	{
		cout << p_head->data << " ";
		p_head = p_head->next;
	}
	cout << endl;
}

void SingleList::merge(SingleList list)
{
	Node * p_head1 = head.next;
	Node * p_head2 = list.head.next;

	int i = 0;
	while (p_head1 != NULL && p_head2 != NULL)
	{
		if (p_head1->data > p_head2->data)
		{
			insert(i ++,p_head2->data);
			p_head2 = p_head2->next;
			continue;
		}
		if (p_head1->data < p_head2->data)
		{
			i++;
			p_head1 = p_head1->next;
			continue;
		}

		p_head1 = p_head1->next;
		p_head2 = p_head2->next;
		i++;
	}

	while (p_head2 != NULL)
	{
		insert(i++, p_head2->data);
		p_head2 = p_head2->next;
	}
}


//void SingleList::merge(SingleList list)
//{
//	Node * p_head1 = head.next;
//	Node * p_head2 = list.head.next;
//	
//	SingleList singleList;
//	int i = 0;
//	while (p_head1 != NULL && p_head2 != NULL)
//	{
//		if (p_head1->data > p_head2->data)
//		{
//			singleList.insert(i++, p_head2->data);
//			p_head2 = p_head2->next;
//			continue;
//		}
//		if (p_head1->data < p_head2->data)
//		{
//			singleList.insert(i++, p_head1->data);
//			p_head1 = p_head1->next;
//			continue;
//		}
//		singleList.insert(i++, p_head1->data);
//		p_head1 = p_head1->next;
//		p_head2 = p_head2->next;
//	}
//	while (p_head1 != NULL)
//	{
//		singleList.insert(i++, p_head1->data);
//		p_head1 = p_head1->next;
//	}
//
//	while (p_head2 != NULL)
//	{
//		singleList.insert(i++, p_head2->data);
//		p_head2 = p_head2->next;
//	}
//	head.next = singleList.head.next;
//}

SingleList::~SingleList()
{
}

#include "SingleList.h"
#include <iostream>
using namespace std;

int main(void)
{
	SingleList list;
	list.create(5);
	cout << "输出第一个链表:";
	list.print();
	/*list.insert(2, 100);
	list.print();
	cout << "插入某个节点完毕!" << endl;
	list.Delete(3);
	list.print();
	cout << "删除某个节点完毕!" << endl;
	cout << "find:" << list.find(3) << endl;
	cout << "插入某位置节点完毕!" << endl;
	list.print();*/

	SingleList list2;
	list2.create(5);
	cout << "输出第二个链表:";
	list2.print();

	cout << "链表合并:";
	//list2.merge(list);
	//list2.print();

	list.merge(list2);
	list.print();
	return 0;
}

第一种浪费空间,但是简单

void SingleList::merge(SingleList list)
{
	Node * p_head1 = head.next;
	Node * p_head2 = list.head.next;
	
	SingleList singleList;
	int i = 0;
	while (p_head1 != NULL && p_head2 != NULL)
	{
		if (p_head1->data > p_head2->data)
		{
			singleList.insert(i++, p_head2->data);
			p_head2 = p_head2->next;
			continue;
		}
		if (p_head1->data < p_head2->data)
		{
			singleList.insert(i++, p_head1->data);
			p_head1 = p_head1->next;
			continue;
		}
		singleList.insert(i++, p_head1->data);
		p_head1 = p_head1->next;
		p_head2 = p_head2->next;
	}
	while (p_head1 != NULL)
	{
		singleList.insert(i++, p_head1->data);
		p_head1 = p_head1->next;
	}

	while (p_head2 != NULL)
	{
		singleList.insert(i++, p_head2->data);
		p_head2 = p_head2->next;
	}
	head.next = singleList.head.next;
}

第二种思路有点乱,但是省时省空间

void SingleList::merge(SingleList list)
{
	Node * p_head1 = head.next;
	Node * p_head2 = list.head.next;

	int i = 0;
	while (p_head1 != NULL && p_head2 != NULL)
	{
		if (p_head1->data > p_head2->data)
		{
			insert(i ++,p_head2->data);
			p_head2 = p_head2->next;
			continue;
		}
		if (p_head1->data < p_head2->data)
		{
			i++;
			p_head1 = p_head1->next;
			continue;
		}

		p_head1 = p_head1->next;
		p_head2 = p_head2->next;
		i++;
	}

	while (p_head2 != NULL)
	{
		insert(i++, p_head2->data);
		p_head2 = p_head2->next;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40990854/article/details/82749099