c++实现单链表

SingleList.h

#pragma once

class Node 
{
public:
	Node() {}
	Node(int info, Node* p) : data(info), next(p) {}
	~Node() {}

	int data;
	Node* next;
};

class SingleList
{
public:
	SingleList();
	~SingleList();
	//判断是否是空列表
	bool isEmpty();
	//头部插入
	void appendHead(int elem);
	//尾部插入
	void appendTail(int elem);
	//头部删除
	int deleteFromHead();
	//尾部删除
	int deleteFromTail();
	//插入
	void insertElem(int pos, int elem);
	//判断元素是否存在
	bool elemExists(int elem);
	//获取元素位置
	int  getElemPos(int elem);
	//删除首个遇到的与elem相同的元素
	void  deleteElem(int elem);
	//移除pos位置上的元素
	int  remove(int pos);
	//清空列表
	void clearList();
	//获取列表长度
	unsigned int getLength() { return m_length; }
	//打印列表
	void printList();
private:
	Node* pHead;
	Node* pTail;
	unsigned int m_length;
};

SingleList.cpp

#include "SingleList.h"
#include "limits.h"
#include <iostream>

using namespace std;

SingleList::SingleList()
	: pHead(nullptr), pTail(nullptr), m_length(0)
{

}


SingleList::~SingleList()
{
	while (pHead)
	{
		Node* pNode = pHead->next;
		delete pHead;
		pHead = pNode;
	}
	pTail = nullptr;
}

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

void SingleList::appendHead(int elem)
{
	pHead = new Node(elem, pHead);
	if (pTail == nullptr)
	{
		pTail = pHead;
	}
	m_length++;
}

void SingleList::appendTail(int elem)
{
	Node* node = new Node(elem, nullptr);
	if (pTail == nullptr)
	{
		pTail = node;
		pHead = pTail;
	}
	else
	{
		pTail->next = node;
		pTail = node;
	}
	m_length++;
}

int SingleList::deleteFromHead()
{
	if (!pHead)
		return INT_MIN;
	int elem = pHead->data;
	Node* pNode = pHead->next;
	delete pHead;
	pHead = pNode;
	if (m_length == 1)
		pTail = nullptr;
	--m_length;
	return elem;
}

int SingleList::deleteFromTail()
{
	if (!pTail)
		return INT_MIN;
	Node* pNode = pHead;
	while (pNode)
	{
		if (pNode->next == pTail)
		{
			break;
		}
		pNode = pNode->next;
	}
	if (!pNode)
		return INT_MIN;
	int elem = pTail->data;
	delete pTail;
	pTail = pNode;
	pTail->next = nullptr;
	if (m_length == 1)
		pHead = nullptr;
	--m_length;
	return elem;
}

void SingleList::insertElem(int pos, int elem)
{
	if (pos < 0 || pos > m_length)
		return;
	if (pos == m_length)
	{
		pTail->next = new Node(elem, nullptr);
		pTail = pTail->next;
		if (m_length == 0)
		{
			pHead = pTail;
		}
		++m_length;
	}
	else if(pos == 0)
	{
		pHead = new Node(elem, pHead);
		if (m_length == 0)
			pTail = pHead;
		++m_length;
	}
	else
	{
		Node* pNode = pHead;
		for (int cur = 0; cur < pos; ++cur, pNode = pNode->next);
		if (!pNode)
			return;
		pNode->next = new Node(elem, pNode->next);
		++m_length;
	}
}

bool SingleList::elemExists(int elem)
{
	if (m_length <= 0)
		return false;
	Node* pNode = pHead;
	while (pNode)
	{
		if (pNode->data == elem)
		{
			break;
		}
		else
		{
			pNode = pNode->next;
		}
	}
	if (pNode == nullptr)
	{
		return false;
	}
	return true;
}

int SingleList::getElemPos(int elem)
{
	if (m_length <= 0)
		return -1;
	Node* pNode = pHead;
	int pos = 0;
	while (pNode)
	{
		if (pNode->data == elem)
		{
			break;
		}
		else
		{
			pNode = pNode->next;
			++pos;
		}
	}
	if (pNode == nullptr)
	{
		return -1;
	}
	return pos;
}

void SingleList::deleteElem(int elem)
{
	if (m_length <= 0)//空表
		return;
	if (m_length == 1)
	{
		if (pHead->data == elem)
		{
			delete pHead;
			pHead = pTail = nullptr;
		}
		--m_length;
	}
	else
	{
		Node* pNode = pHead;
		if (pHead->data == elem)
		{
			pHead = pHead->next;
			delete pNode;
			pNode = nullptr;
			--m_length;
			return;
		}
		for (; pNode && pNode->next && pNode->next->data != elem; pNode = pNode->next);
		if (!pNode)
			return;
		if (!pNode->next && pTail->data == elem)
		{
			while (pNode)
			{
				if (pNode->next == pTail)
				{
					break;
				}
				pNode = pNode->next;
			}
			if (!pNode)
				return;
			delete pTail;
			pTail = pNode;
			pTail->next = nullptr;
			--m_length;
			return;
		}
		Node* pTemp = pNode->next;
		pNode->next = pNode->next->next;
		delete pTemp;
		pTemp = nullptr;
		--m_length;
	}
}

int SingleList::remove(int pos)
{
	if (m_length <= 0 || pos < 0 || pos >= m_length)
		return INT_MIN;
	Node* pNode = pHead;
	int elem = INT_MIN;
	if (pos == m_length - 1)
	{//删除末尾
		while (pNode)
		{
			if (pNode->next == pTail)
			{
				break;
			}
			pNode = pNode->next;
		}
		if (pNode == nullptr)
			return elem;
		elem = pTail->data;
		delete pTail;
		pTail = pNode;
		pTail->next = nullptr;
		if (m_length == 1)
			pHead = nullptr;
		--m_length;
	}
	else if (pos == 0)
	{//删除头部
		elem = pHead->data;
		pHead = pHead->next;
		delete pNode;
		pNode = nullptr;
		if (m_length == 1)
			pTail = nullptr;
		--m_length;
	}
	else
	{
		int cur = 0;
		for (; cur < pos-1; ++cur, pNode = pNode->next);
		if (!pNode)
			return elem;
		elem = pNode->data;
		Node* pTemp = pNode->next;
		pNode->next = pNode->next->next;
		delete pTemp;
		pTemp = nullptr;
		--m_length;
	}
	return elem;
}

void SingleList::clearList()
{
	while (pHead)
	{
		Node* pNode = pHead->next;
		delete pHead;
		pHead = pNode;
	}
	pTail = nullptr;
	m_length = 0;
}

void SingleList::printList()
{
	cout << "PrintList: " << endl;
	Node* pNode = pHead;
	while (pNode)
	{
		cout << pNode->data << " ";
		pNode = pNode->next;
	}
	cout << endl;
}

测试代码:main.cpp

#include "SingleList.h"
#include <iostream>

using namespace std;

int main()
{
	SingleList* testList = new SingleList();
	if (testList->isEmpty())
	{
		for (int i = 0; i <= 6; ++i)
		{
			testList->appendHead(i);
			testList->appendTail(i);
		}
	}
	testList->printList();
	cout << "Length: " << testList->getLength() << endl;

	int ihead = testList->deleteFromHead();
	int itail = testList->deleteFromTail();
	cout << "DeleteElem: " << ihead << "  " << itail << endl;
	testList->printList();

	testList->deleteElem(0);
	testList->deleteElem(0);
	testList->printList();

	testList->remove(0);
	testList->remove(8);
	testList->printList();

	testList->deleteElem(1);
	testList->printList();
	testList->remove(3);
	testList->printList();

	testList->insertElem(0, 5);
	testList->insertElem(7, 5);
	testList->printList();

	if (testList->elemExists(2))
	{
		int pos = testList->getElemPos(2);
		cout << "Pos: " << pos << endl;
	}
	testList->clearList();
	testList->appendHead(1);
	testList->printList();

	delete testList;
	testList = nullptr;

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值