C++链表(single-linked list)

本文详细介绍了单向链表的基本概念,并提供了直接代码实现,包括创建、添加、删除节点以及反转链表等操作。

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

单向链表(single-linked list)

直接代码

//IntSLList.h
#ifndef INTSLLIST_H
#define INTSLLIST_H

class IntSLLNode
{
public:
	IntSLLNode()
	{
		data = 0;
		next = 0;
	}

	IntSLLNode(int el, IntSLLNode* ptr = 0)
	{
		data = el;
		next = ptr;
	}

	int data;
	IntSLLNode* next;
};

class IntSLList
{
public:
	IntSLList();
	~IntSLList();
	
	bool IsEmpty();
	void AddToHead(int el);
	void AddToTail(int el);
	void DeleteFromHead();
	void DeleteFromTail();
	void DeleteNode(int el);
	bool IsInList(int el);	

	void ReverseList();	//反转链表

	IntSLLNode* GetListHead();	//获取链表头节点

private:
	IntSLLNode* head;
	IntSLLNode* tail;
};

#endif // !INTSLLIST_H


//IntSLList.cpp
#include "IntSLList.h"
#include <iostream>
using namespace std;

IntSLList::IntSLList()
{

}

IntSLList::~IntSLList()
{

}

bool IntSLList::IsEmpty()
{
	return head == NULL;
}

void IntSLList::AddToHead(int el)
{
	head = new IntSLLNode(el, head);
	if (tail == 0)
	{
		tail = head;
	}
}

void IntSLList::AddToTail(int el)
{
	if (tail == NULL)
	{
		head = tail = new IntSLLNode(el);
	}
	else
	{
		tail->next = new IntSLLNode(el);
		tail = tail->next;
	}
}

void IntSLList::DeleteFromHead()
{
	if (head != NULL)
	{
		IntSLLNode* ptemp = head;
		head = head->next;
		delete ptemp;
		ptemp = NULL;
	}
}

void IntSLList::DeleteFromTail()
{
	if (tail != NULL)
	{
		if (head == tail)
		{
			delete tail;
			head = tail = NULL;
		}
		else
		{
			IntSLLNode* ptemp = head;
			while (ptemp->next != tail)
			{
				ptemp = ptemp->next;
			}

			delete tail;
			tail = ptemp;
			tail->next = NULL;
		}
	}
}

void IntSLList::DeleteNode(int el)
{
	if (head != NULL)
	{
		if (head == tail && head->data == el)
		{
			delete head;
			head = tail = NULL;
		}
		else if (head->data == el)
		{
			IntSLLNode* ptemp = head;
			head = head->next;
			delete ptemp;
			ptemp = NULL;
		}
		else if (head->next != NULL)
		{
 			IntSLLNode* pFront = head;
 			IntSLLNode* pBehind = head->next;
			while (pBehind != 0)
			{
				if (pBehind->data == el)
				{
					pFront->next = pBehind->next;
					if (pBehind == tail)
						tail = pFront;

					delete pBehind;
					pBehind = NULL;
					break;
				}

				pFront = pBehind;
				pBehind = pBehind->next;
			}
		}
	}
}

bool IntSLList::IsInList(int el)
{
	IntSLLNode* ptemp = head;
	while (ptemp != NULL)
	{
		if (ptemp->data == el)
			return true;

		ptemp = ptemp->next;
	}

	return false;
}

void IntSLList::ReverseList()
{
	if (head != tail)
	{
		IntSLLNode* p1 = head;
		IntSLLNode* p2 = head->next;
		IntSLLNode* p3 = NULL;
		p1->next = NULL;
		while (p2 != NULL)
		{
			p3 = p2->next;
			p2->next = p1;
			p1 = p2;
			p2 = p3;
		}

		swap(head, tail);

	}
}

IntSLLNode* IntSLList::GetListHead()
{
	return head;
}

//MainProcess.cpp
#include "IntSLList.h"
#include <iostream>
using namespace std;

void Print(IntSLLNode* phead)
{
	while (phead != NULL)
	{
		cout << phead->data << " ";
		phead = phead->next;
	}
	cout << endl;
}


int main()
{
	IntSLList listt;
	listt.AddToTail(1);
 	listt.AddToTail(2);
	listt.AddToTail(3);
	listt.AddToTail(4);

	IntSLLNode* phead = listt.GetListHead();
	Print(phead);

	listt.ReverseList();
	phead = listt.GetListHead();
	Print(phead);

	cout << endl;
	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值