C++实现单链表

1、引言。
对于链表这种基本数据结构,我们再熟悉不过了。今天,来复习下单链表的实现。
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。-- 百度百科
2、实例。

2.1 定义节点:

//定义节点
struct Node
{
	Hunter value;
	Node* pNext;

	Node(Hunter stu)
		:value(stu),
		pNext(NULL)
	{
	}
};

2.2 定义节点数据域:

//定义节点数据域
struct Hunter
{
	string name;
	int id;
	Hunter(string nm, int idd)
		:name(nm),
		id(idd)
	{
	}
};

2.3 定义链表类:

//定义链表
class CList
{
public:
	CList();
	~CList();

	void CreateList(Node* head);
	void Insert(Node node, int pos);
	void Delete(Node node);
	void Append(Node node);
	void Reverse();

	int Find(Node node);
	int GetLength();
	void Show();

private:
	Node *m_pHead;
	int m_nLength;
};
//链表类构造函数。
CList::CList()
{
	m_pHead = NULL;
	m_nLength = 0;
}

CList::~CList()
{
	Node* pTemp = m_pHead;
	if (pTemp)
	{
		delete pTemp;
		pTemp = NULL;
	}
}

//链表类的创建
void CList::CreateList(Node* head)
{
	if (head->value.name == "" || head->value.id == -1 || m_pHead != NULL)
	{
		cout << "Error data,create failed" << endl;
		return;
	}

	if (m_pHead == NULL)
	{
		m_pHead = head;
		cout << "Create successed" << endl;
	}
}
//节点的插入
void CList::Insert(Node node, int pos)
{
	if (pos <= 0)
	{
		cout << "Insert failed" << endl;
		return;
	}

	int index = 0;
	Node* temp = m_pHead;
	Node* pNode = new Node(node);

	while (temp != NULL&&index < pos)
	{
		temp = temp->pNext;
		index++;
	}
	if (temp == NULL)
	{
		cout << "Insert failed" << endl;
		return;
	}

	pNode->pNext = temp->pNext;
	temp->pNext = pNode;
	m_nLength++;
	cout << "Insert successed" << endl;
}
//节点删除操作
void CList::Delete(Node node)
{
	int nPos = Find(node);

	if (nPos < 0)
	{
		cout << "Illegal positon,delete failed" << endl;
		return;
	}

	int nLength = GetLength();
	if (nLength <= nPos)
	{
		cout << "Out of range,delete failed" << endl;
	}

	Node *temp = m_pHead;

	int nFlag = 0;
	if (nPos == 0)
	{
		temp = temp->pNext;
	}
	else
	{
		int nIndex = 1;
		while (nIndex < nPos)
		{
			temp = temp->pNext;
		}
		temp->pNext = temp->pNext->pNext;
	}
	m_nLength--;
}
//节点尾部追加
void CList::Append(Node node)
{
	Node* temp = m_pHead;

	if (temp == NULL)
	{
		CreateList(&node);
	}
	else
	{
		while (temp->pNext != NULL)
		{
			temp = temp->pNext;
		}
		Node*p = new Node(node);
		temp->pNext = p;
		cout << "Append successed" << endl;
	}
}
//链表反向操作
void CList::Reverse()
{
	if (m_pHead == NULL)
	{
		cout << "Empty list,reverse failed" << endl;
		return;
	}

	Node*pCurrent = m_pHead;
	Node*pNext = m_pHead->pNext;
	Node*temp = NULL;
	while (pNext != NULL)
	{
		temp = pNext->pNext;
		pNext->pNext = pCurrent;
		pCurrent = pNext;
		pNext = temp;
	}

	m_pHead->pNext = NULL;
	m_pHead = pCurrent;
	cout << "Reverse list:" << endl;
}
//链表查找操作
int CList::Find(Node node)
{
	Node* temp = m_pHead;
	int index = 0;
	while (temp != NULL)
	{
		if (temp->value.name == node.value.name&& temp->value.id == node.value.id)
		{
			return index;
		}
		else
		{
			temp = temp->pNext;
			index++;
		}
	}
	return -1;
}
//链表长度计算
int CList::GetLength()
{
	int nLength = 0;
	Node* temp = m_pHead;
	while (temp != NULL)
	{
		temp = temp->pNext;
		nLength++;
	}
	m_nLength = nLength;
	return nLength;
}
//打印函数s
void CList::Show()
{
	if (m_pHead == NULL)
	{
		cout << "Empty list" << endl;
		return;
	}

	Node* temp = m_pHead;
	while (temp != NULL)
	{
		cout << temp->value.name << '(' << temp->value.id << ')' << "->";
		temp = temp->pNext;
	}
	cout << "NULL" << endl;
}

2.4 主函数调用。

int main()
{
	cout << "TestInstance:" <<"\n"<< endl;

	CList list;
	Node node0(Hunter("Gon", 0));
	list.CreateList(&node0);
	
	Node node1(Hunter("Killua", 1));
	list.Append(node1);

	Node node2(Hunter("Kula", 2));
	list.Append(node2);

	Node node3(Hunter("Leiouli", 3));
	list.Append(node3);

	list.Show();

	Node node4(Hunter("Xisuo", 4));
	list.Insert(node4, 2);

	cout << list.GetLength() << endl;
	list.Show();

	list.Reverse();
	list.Show();

	Node node5(Hunter("Xisuo", 4));
	list.Delete(node5);

	list.Show();

	cin.get();
	return 0;
}

2.5 运行。
在这里插入图片描述
ok,完成。

入门萌新,浅知拙见,若有斧正,不胜感激。^ - ^

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值