135-1821-9792

以c++的方式实现单链表-创新互联

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

创新互联2013年至今,先为宜春等服务建站,宜春等地企业,进行企业商务咨询服务。为宜春企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

#include
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;
		}
	}
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:以c++的方式实现单链表-创新互联
地址分享:http://kswsj.com/article/hjjji.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号