c++实现循环链表

cyclelist.h

#pragma once
#include 

using namespace std;

class Node
{
public:
	Node() {}
	Node(int e, Node* p = nullptr) : data(e), next(p) {}
	~Node() {}
public:
	int data;
	Node* next;
};


class CycleList
{
public:
	CycleList();
	~CycleList();

	//判断列表是否为空
	bool isEmpty();
	//头部添加元素
	void appendHead(int e);
	//尾部添加元素
	void appendTail(int e);
	//获取元素位置
	int getElemPos(int e);
	//获取指定位置的Node指针
	Node* getElement(int pos);
	//向指定位置插入元素
	void insertElem(int pos, int e);
	//删除首个与e相同的元素
	void deleteElem(int e);
	//移除pos位置上的元素
	void removeElem(int pos);
	//清空列表
	void clear();
	//打印列表
	void printList();
	int getLength() { return m_length; }

private:
	Node* head;
	Node* rear;
	int m_length;
};

cyclelist.cpp

#include "CycleList.h"

CycleList::CycleList()
	: m_length(0)
{
	head = new Node;
	head->next = head;
	head->data = 0;
	rear = nullptr;
}

CycleList::~CycleList()
{
	if (head)
	{
		Node* p = head->next;
		while (p != rear)
		{
			head->next = p->next;
			delete p;
			p = head->next;
		}
		delete head;
		head = nullptr;
		delete rear;
		rear = nullptr;
	}
}

bool CycleList::isEmpty()
{
	if (m_length > 0)
		return false;
	return true;
}

void CycleList::appendHead(int e)
{
	if (head == head->next)
	{//空表
		Node* node = new Node(e);
		head->next = node;
		node->next = node;
		rear = node;
	}
	else
	{
		Node* node = new Node(e, head->next);
		rear->next = node;
		head->next = node;
	}
	++m_length;
}

void CycleList::appendTail(int e)
{
	if (head == head->next)
	{//空表
		Node* node = new Node(e);
		head->next = node;
		node->next = node;
		rear = node;
	}
	else
	{
		Node* node = new Node(e, head->next);
		rear->next = node;
		rear = node;
		node->next = head->next;
	}
	++m_length;
}

int CycleList::getElemPos(int e)
{
	if (head == head->next)
		return -1;
	Node* node = head->next;
	for (int i = 0; node != rear && i < m_length; ++i, node = node->next)
	{
		if (e == node->data)
		{
			return i;
		}
	}
	if (rear->data == e)
	{
		return m_length - 1;
	}
	return -1;
}

Node* CycleList::getElement(int pos)
{
	if (pos < 0 || pos >= m_length || head == head->next)
		return nullptr;
	Node* node = head->next;
	for (int i = 0; i < pos; ++i, node = node->next)
	{
		;
	}
	return node;
}

void CycleList::insertElem(int pos, int e)
{
	if (pos < 0 || pos > m_length)
		return;
	if (pos == 0)
	{
		head->next = new Node(e, head->next);
		if (m_length == 0)
		{
			rear = head->next;
		}
		rear->next = head->next;
	}
	else if (pos == m_length)
	{
		Node* node = new Node(e);
		if (m_length == 0)
		{
			head->next = node;
			node->next = node;
			rear = node;
		}
		else
		{
			rear->next = node;
			rear = node;
			node->next = head->next;
		}
	}
	else
	{
		Node* node = head->next;
		for (int i = 0; i < pos - 1; ++i, node = node->next)
		{
			;
		}
		Node* newNode = new Node(e, node->next);
		node->next = newNode;
	}
	++m_length;
}

void CycleList::deleteElem(int e)
{
	if (head == head->next)
		return;
	Node* node = head->next;
	Node* helpNode = head;
	int pos = -1;
	while (node != rear)
	{
		++pos;
		if (node->data == e)
		{
			break;
		}
		node = node->next;
		helpNode = helpNode->next;
	}

	if (m_length == 1)
	{
		if (rear->data == e)
		{
			head->next = head;
			rear = nullptr;
			delete node;
			node = nullptr;
		}
	}
	else
	{
		if (node != rear)
		{
			helpNode->next = node->next;
			if (pos == 0)
			{
				rear->next = node->next;
			}
			delete node;
			node = nullptr;
		}
		else
		{
			if (rear->data == e)
			{
				helpNode->next = node->next;
				delete node;
				node = nullptr;
				rear = helpNode;
			}
		}
	}
	--m_length;
}

void CycleList::removeElem(int pos)
{
	if (pos < 0 || pos >= m_length || head == head->next)
		return;
	Node* node = head->next;
	if (m_length == 1)
	{
		delete node;
		node = nullptr;
		head->next = head;
		rear = nullptr;
	}
	else
	{
		if (pos == 0)
		{
			Node* p = head->next->next;
			head->next = p;
			rear->next = p;
			delete node;
			node = nullptr;
		}
		else if(pos == m_length - 1)
		{
			for (int i = 0; i < pos - 1; ++i)
			{
				node = node->next;
			}
			delete rear;
			rear = node;
			node->next = head->next;
		}
		else
		{
			for (int i = 0; i < pos - 1; ++i)
			{
				node = node->next;
			}
			Node* temp = node->next;
			node->next = temp->next;
			delete temp;
			temp = nullptr;
		}
	}
	--m_length;
}

void CycleList::clear()
{
	Node* node = head->next;
	while (node != rear)
	{
		head->next = node->next;
		delete node;
		node = head->next;
	}
	head->next = nullptr;
	delete rear;
	rear = nullptr;
	m_length = 0;
}

void CycleList::printList()
{
	cout << "PrintList: " << endl;
	if (head != head->next)
	{
		Node* p = head->next;
		while (p)
		{
			cout << p->data << " ";
			p = p->next;
			if (p == head->next)
			{
				break;
			}
		}
	}
	cout << endl;
}

测试代码:main.cpp

#include "CycleList.h"

int main()
{
	CycleList testList;
	for (int i = 0; i < 7; ++i)
	{
		testList.appendHead(i);
		testList.appendTail(i);
	}
	testList.printList();

	cout << "GetElemPos(6): " << testList.getElemPos(6) << endl;
	cout << "GetElemPos(5): " << testList.getElemPos(5) << endl;
	cout << "GetElemPos(0): " << testList.getElemPos(0) << endl;

	cout << "GetElement(0): " << testList.getElement(0)->data << endl;
	cout << "GetElement(13): " << testList.getElement(13)->data << endl;
	cout << "GetElement(7): " << testList.getElement(7)->data << endl;

	testList.removeElem(0);
	testList.printList();
	testList.removeElem(12);
	testList.printList();

	testList.removeElem(7);
	testList.printList();

	testList.deleteElem(5);
	testList.printList();
	testList.deleteElem(5);
	testList.printList();
	testList.deleteElem(0);
	testList.printList();

	CycleList testList2;
	testList2.appendHead(2);
	testList2.appendHead(1);
	testList2.removeElem(1);
	testList2.printList();

	CycleList testList3;
	testList3.appendHead(1);
	testList3.deleteElem(1);
	testList3.printList();

	testList3.insertElem(0, 3);
	testList3.insertElem(0, 2); 
	testList3.insertElem(0, 1);
	testList3.insertElem(3, 3);
	testList3.insertElem(4, 2);
	testList3.insertElem(5, 1);
	testList3.printList();

	CycleList testList4;
	testList4.insertElem(0,1);
	testList4.insertElem(1,2);
	testList4.insertElem(1, 3);
	testList4.insertElem(3, 4);
	testList4.printList();

	testList.clear();
	testList.printList();
	return 0;
}

 

你可能感兴趣的:(数据结构/算法)