【算法】C++链表的实现以及常见的链表操作和测试

自己实现链表常见的操作,用作记录,以备以后查看

#include <iostream>
#include <string.h>
using namespace std;

//定义节点
class Node
{
public:
	int m_data;
	Node *m_next;
	Node(int data):m_data(data),m_next(NULL){}
};


class List
{
public:
	//构造函数
	List():m_head(NULL){}
	~List();
	bool isEmpty(){return m_head;}
	//链表长度
	int Length();
	//打印链表
	void Print();
	//插入元素
	bool Insert(int data,int pos);
	//查找位置的元素
	Node* FindByPos(int pos);
	void Delete(int pos);
	//逆序
	void Reverse();
	//插入并排序
	void InsertSort(int data);
	//判断链表中是否存在环
	bool IsLoop();
	//有序的两个链表进行合并
	Node* Combine(Node *first);
public:
	Node *m_head;
};

Node* List::Combine(Node *first)
{
	//合并之后的头节点
	Node *head = NULL;
	//采用递归的方式进行合并
	if(this->m_head == NULL)
		return first;
	if(first == NULL)
		return this->m_head;
	if(m_head->m_data < first->m_data)
	{
		head = m_head;
		m_head = m_head->m_next;
		head->m_next = Combine(first);
	}
	else
	{
		head = first;
		head->m_next = Combine(first->m_next);
	}
	return head;
}

bool List::IsLoop()
{
	//设置两个指针,一个走一步,一个走两步,如果能重合,说明有环
	Node *first = m_head,*second = m_head;
	do
	{
		first = first->m_next;
		second = second->m_next->m_next;
	}while(second != NULL && second->m_next != NULL && first != second);
	if(first == second)
	{
		return true;
	}
	else
		return false;
}

//插入并排序
void List::InsertSort(int data)
{
	Node *tmp = m_head;
	Node *pre;
	//生成新的节点
	Node *node = new Node(data);
	//判断是不是空节点
	if(m_head == NULL)
	{
		m_head = node;
		return;
	}
	//判断是不是在头插
	if(data < m_head->m_data)
	{
		node->m_next = m_head;
		m_head = node;
		return;
	}
	while(tmp != NULL && tmp->m_data < data)
	{
		pre = tmp;
		tmp = tmp->m_next;
	}
	if(NULL == tmp)				//说明到了结尾,就在尾部插入
	{
		pre->m_next = node;
		return;
	}
	else						//在pre和tmp之间插入
	{
		node->m_next = pre->m_next;
		pre->m_next = node;
	}
}

void List::Reverse()
{
	if(Length() < 2)
		return;
	Node *cur = m_head,*next = m_head->m_next,*tmp;
	while(next != NULL)
	{
		tmp = next->m_next;
		next->m_next = cur;
		cur = next;
		next = tmp;
	}
	m_head->m_next = NULL;
	m_head = cur;
}


//删除给定位置后面的元素(后删)
void List::Delete(int pos)
{
	if(Length() <= pos)
	{
		cout << "给定删除位置超出数组大小,删除失败!" << endl;
		return;
	}
	Node *tmp = m_head;
	int index = 1;
	//删除头节点
	if(0 == pos)
	{
		m_head = tmp->m_next;
		delete tmp;
		return;
	}
	while(tmp != NULL && index < pos)
	{
		tmp = tmp->m_next;
		index++;
	}
	if(NULL == tmp)
	{
		cout << "给定删除位置超出数组范围,删除失败!" << endl;
		return;
	}
	Node *de = tmp->m_next;
	tmp->m_next = tmp->m_next->m_next;
	delete de;
}
//查找给定位置的元素
Node* List::FindByPos(int pos)
{
	Node *tmp = m_head;
	if(tmp == NULL)
	{
		return NULL;
	}
	int index = 1;
	while(tmp != NULL && index < pos)
	{
		tmp = tmp->m_next;
		index++;
	}
	if(tmp == NULL)
	{
		return NULL;
	}
	return tmp;
}

//根据位置插入节点(后插),0表示在头节点插入
bool List::Insert(int data,int pos)
{
	Node *tmp = m_head;
	//生成节点
	Node *node = new Node(data);
	//空链表
	if(m_head == NULL)
	{
		m_head = node;
		return 1;
	}
	if(0 == pos)
	{
		node->m_next = m_head;
		m_head = node;
		return 1;
	}
	else
	{
		int index = 1;
		while(tmp != NULL && index < pos)
		{
			tmp = tmp->m_next;
			index++;
		}
		if(NULL == tmp)
			return 0;
		node->m_next = tmp->m_next;
		tmp->m_next = node;
		return 1;
	}
}
//打印
void List::Print()
{
	if(m_head == NULL)
		cout << "空链表" << endl;
	Node *tmp = m_head;
	while(tmp != NULL)
	{
		cout << tmp->m_data << " ";
		tmp = tmp->m_next;
	}
	cout << endl;
}

List::~List()
{
	Node *tmp = NULL;
	while(m_head != NULL)
	{
		tmp = m_head->m_next;
		delete m_head;
		m_head = tmp;
	}
	cout << "~List 被调用" << endl;
}

int List::Length()
{
	int len = 0;
	Node *tmp = m_head;
	if(tmp == NULL)
		return len;
	else
	{
		while(tmp != NULL)
		{
			tmp = tmp->m_next;
			len++;
		}
	}
	return len;
}

void print(Node *p)
{
	if(NULL == p)
	{
		cout << "节点为空" << endl;
		return;
	}
	while(p!=NULL)
	{
		cout << p->m_data << " ";
		p = p->m_next;
	}
	cout << endl;
}

int main()
{
	List list;
	/*
	list.Insert(10,3);
	list.Insert(30,1);
	list.Insert(40,2);
	if(0 == list.Insert(20,2))
		cout << "插入失败!" << endl;
	list.Print();
	Node *find = list.FindByPos(5);
	if(NULL == find)
		cout << "查找失败" << endl;
//	cout << "查找的结果为:" << find->m_data << endl;
	int len = list.Length();
	cout << "链表长度为:" << len << endl;
	list.Delete(0);
	list.Insert(10,1);
	list.Insert(50,3);
	list.Print();
	cout << "逆序后:" << endl;
	list.Reverse();
	list.Print();*/
	list.InsertSort(10);
	list.InsertSort(50);
	list.InsertSort(80);
	list.InsertSort(30);
	list.InsertSort(20);
	cout << "list:";
	list.Print();
	/*
	Node *start = l ist.m_head->m_next->m_next->m_next;
	start->m_next = list.m_head->m_next;
	if(list.IsLoop())
		cout << "链表有环" << endl;
	else
		cout << "链表没有环" << endl;
//	list.Print();*/
	List list2;
	list2.InsertSort(11);
	list2.InsertSort(44);
	list2.InsertSort(33);
	list2.InsertSort(22);
	cout << "list2:" ;
	list2.Print();

	cout << "合并后:" << endl;
	Node *head = list.Combine(list2.m_head);
	print(head);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bimabushihaodongxi/article/details/80180840
今日推荐