C++实现单链表(Singly-Linked List)追加、遍历、删除

这篇博客详细介绍了如何使用C++编程实现单链表的基本操作,包括向链表尾部追加节点、遍历链表以及删除特定节点的方法。通过实例代码和程序输出,帮助读者深入理解数据结构中的单链表概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="cpp">#include <iostream>
using namespace std;

struct Node
{
	int data;
	struct Node* next;
};

class List
{
	Node* head;
public:
	List() { head = NULL;}
	void Print();
	int GetSize();
	void Append(int addData);
	void EraseOne(int eraseData);
	void EraseOne_V2(int eraseData);
};

void List::Print()
{
	Node* tmp = head;

	//list is empty
	if (tmp == NULL)
	{
		cout << "Empty List!\n";
		return;
	}

	//list is not empty
	for(; tmp != NULL; tmp = tmp->next)
	{
		cout << tmp->data << " --> ";
	}
	cout << "NULL\n";
}

int List::GetSize()
{
	Node* tmp = head;

	//list is empty
	if(tmp == NULL)
	{
		return 0;
	}

	//list is not empty
	int num(0);
	for(; tmp != NULL; tmp = tmp->next)
	{
		++num;
	}
	return num;
}

void List::Append(int addData)
{
	//create a new node
	Node* newNode = new Node;
	newNode->data = addData;
	newNode->next = NULL;

	Node* tmp = head;

	//list is empty
	if(tmp == NULL)
	{
		head = newNode; //有可能引入重复释放指针的问题,可以使用智能指针。
		return;
	}

	//list is not empty, locate the last node
	for(; tmp->next != NULL; tmp = tmp->next)
	{
	}

	tmp->next = newNode;

}

void List::EraseOne_V2(int eraseData)
{
	Node* tmp = head;

	//空链表
	if (tmp == NULL)
	{
		return;
	}

	//只有一个节点的链表
	if (tmp->next == NULL)
	{
		if (tmp->data == eraseData)
		{
			delete tmp;
			head = NULL;
		}
		return;
	}

	//有两个及以上节点的链表,待删节点正是首节点
	if (tmp->data == eraseData)
	{
		head = head->next;
		delete tmp;
		tmp = NULL;
	}

	//有两个及以上节点的链表,待删节点不是首节点
	for (; tmp != NULL; tmp = tmp->next)
	{
		if ((tmp->next != NULL)  &&  (tmp->next->data == eraseData)) //找到待删节点的前一个节点
		{
			Node* tmp2 = tmp->next;
			tmp->next = tmp->next->next;
			delete tmp2;
			break;
		}
	}
}

void List::EraseOne(int eraseData)
{
	Node* tmp = head;

	////////////////////
	//list is empty
	////////////////////
	if(tmp == NULL)
	{
		return;
	}

	////////////////////
	//list is not empty
	////////////////////
	int locate(-1), idx(0);
	for(; tmp != NULL; tmp = tmp->next)
	{
		++idx;
		if(tmp->data == eraseData)
		{
			locate = idx - 1;
			break;
		}
	}

	if(locate == -1) // eraseData not found
	{
		return;
	}

	int listSize = GetSize();

	if(listSize == 1) // only one node in the list
	{
		delete head;
		head = NULL;
	}
	else // more than one node in the list
	{
		// eraseData is the 0th node
		if(locate == 0)
		{
			tmp = head;
			head = head->next;
			delete tmp; //注意这里释放tmp(并没有用new申请空间给tmp)的空间是可行的,我的理解是因为tmp是结构体指针(相对于基本数据类型指针)。但是不能重复释放。
			tmp = NULL;
		}

		//eraseNode is the last node
		if(locate == listSize - 1)
		{
			tmp = head;
			int idx(0);
			for(; tmp != NULL; tmp = tmp->next)
			{
				++idx;
				if(idx == locate - 1)
				{
					Node* toDel = tmp->next;
					tmp->next = NULL;
					delete toDel;
				}
			}
		}

		// eraseData is intermediate node
		if(locate > 0  &&  locate < listSize - 1)
		{
			tmp = head;
			int idx(0);
			for(; tmp != NULL; tmp = tmp->next)
			{
				++idx;
				if(idx == locate - 1)
				{
					Node* toDel = tmp->next;
					tmp->next = tmp->next->next;
					delete toDel;
				}
			}
		}
	} // more than one node in the list
}


int main (int argc, char** argv)
{
	  // New list
	    List list;
		list.Print();

		list.EraseOne_V2(100);

		list.Append(2);
		list.Print();
		list.EraseOne_V2(2);
		list.Print();

	    // Append nodes to the list
	    list.Append(100);
	    list.Print();
	    list.Append(200);
	    list.Print();
	    list.Append(300);
	    list.Print();
	    list.Append(300);
	    list.Print();
	    list.Append(500);
	    list.Print();

	    // Delete nodes from the list
	    list.EraseOne_V2(400);
	    list.Print();
	    list.EraseOne_V2(100);
	    list.Print();
	    list.EraseOne_V2(200);
	    list.Print();
	    list.EraseOne_V2(500);
	    list.Print();
	    list.EraseOne_V2(100);
	    list.Print();
	    list.EraseOne_V2(300);
		list.Print();

	    return 0;
}



程序输出结果:

100 --> NULL
100 --> 200 --> NULL
100 --> 200 --> 300 --> NULL
100 --> 200 --> 300 --> 300 --> NULL
100 --> 200 --> 300 --> 300 --> 500 --> NULL
100 --> 200 --> 300 --> 300 --> 500 --> NULL
100 --> 300 --> 300 --> 500 --> NULL
100 --> 300 --> 300 --> 500 --> NULL
100 --> 300 --> NULL
300 --> NULL
Empty List!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值