以c++的方式实现单链表

发布时间:2020-06-20 13:59:31 作者:刘子蛋gogogo
来源:网络 阅读:309

  之前用c语言的方式实现过单链表,现在用c++的方式实现单链表。

  以c++的类实现单链表,写完代码有了许多不一样的体会。感受到了两种语言的差异。

#include<iostream>
using namespace std;

class Slist
{

private:
	struct Node
	{
		int data;
		Node* pNext;
	};
	int size;
	Node* pHead;
	Node* pTail;
	Node* ByeNode(int _data)
	{
		Node* pNode = NULL;
		pNode = new Node;
		pNode->data = _data;
		pNode->pNext = NULL;

		return pNode;
	}
public:
	Slist()
	{
		pHead = NULL;
		pTail = NULL;
		size = 0;
	}

	Slist(const Slist& my_Node)
	{
		Node* pNode = (my_Node).pHead;

		for (int i = 0; i < my_Node.size; i++)
		{
			Pushback(pNode->data);
			pNode = pNode->pNext;
		}
	}
	~Slist();
	bool Empty();
	Node* operator[](int index)
	{
		if ((index<0) || (index>size - 1))
			return NULL;

		Node* pNode = pHead;
		for (int i = 0; i < index; i++)
			pNode = pNode->pNext;

		return pNode;
	}
	Slist& operator=(const Slist& my_Node)
	{
		Clear();

		Node* pNode = my_Node.pHead;
		for (int i = 0; i < my_Node.size; i++)
		{
			Pushback(pNode->data);
			pNode = pNode->pNext;
		}

		return *this;
	}
	void Pushback(int _data);
	void Popback();
	void PushFront(int _data);
	void PopFront();
	Node* Find(int _data)
	{
		if (Empty())
			return NULL;

		Node* pNode = pHead;
		while (pNode->pNext!=NULL)
		{
			if (pNode->data == _data)
				return pNode;

			pNode = pNode->pNext;
		}

		return NULL;
	}
	void Erase(Node* DelNode)
	{
		Node* pNode = pHead;

		while (pNode->pNext != DelNode)
			pNode = pNode->pNext;

		pNode->pNext = pNode->pNext->pNext;

		delete DelNode;
		DelNode = NULL;
	}
	void Clear()
	{
		if (Empty())
			return;

		while (size != 0)
		{
			PopFront();
		}
	}
	void sort();
};




Slist::~Slist()
{
	while (!Empty())
	{
		size--;
		Node* pNode = pHead;
		pHead = pHead->pNext;
		delete pNode;
		pNode = NULL;
	}
	pTail = NULL;
}

void Slist::Pushback(int _data)
{
	size += 1;
	Node* NewNode = ByeNode(_data);

	if (NULL == pHead)
	{
		pHead = NewNode;
		pTail = NewNode;
		return;
	}

	pTail->pNext = NewNode;
	pTail = pTail->pNext;
}

void Slist::Popback()
{
	if (Empty())
	{
		cout << "链表为空" << endl;
		return;
	}
		
	if (size == 1)
	{
		size -= 1;
		delete pHead;
		pHead = NULL;
		pTail = NULL;
		return;
	}

	size -= 1;
	delete pTail;
	
	Node* pNode = pHead;
	while (pNode->pNext != NULL)
		pNode = pNode->pNext;

	pTail = pNode;
}

void Slist::PushFront(int _data)
{
	Node* NewNode = ByeNode(_data);

	if (pHead == NULL)
	{
		pHead = NewNode;
		pTail = NewNode;
		size += 1;
		return;
	}

	Node* pNode = pHead;
	pHead = NewNode;
	pHead->pNext = pNode;

	size += 1;
}

void Slist::PopFront()
{
	if (Empty())
		return;

	if (1 == size)
	{
		delete pHead;
		pHead = NULL;
		pTail = NULL;
		return;
	}

	size -= 1;
	Node* pNode = pHead;
	pHead = pHead->pNext;
	delete pNode;
	pNode = NULL;
}

bool Slist::Empty()
{
	if (size == 0)
		return true;

	return false;
}

void Slist::sort()
{
	if (size <= 1)
		return;

	Node* pNode = pHead;
	while (pNode->pNext != NULL)
	{
		if ((pNode->data) > (pNode->pNext->data))
		{
			int tmp = pNode->data;
			pNode->data = pNode->pNext->data;
			pNode->pNext->data = tmp;
			
			pNode = pNode->pNext;
		}
		else
		{
			pNode = pNode->pNext;
		}
	}
}

  

推荐阅读:
  1. python单链表的实现
  2. 复杂单链表的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

private return public

上一篇:ORACLE ADF11g:VO获取页面当前行和多行

下一篇:(二)Python调用Zabbix api之从入门到放弃——登录并获取身份验证令牌

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》