用C++实现线性表的链式存储结构

一、结点和链表的定义

class Node
{
public:
	Node(int data)
	{
		this->m_Data = data;
		this->m_Next = nullptr;
	}

	int m_Data;
	Node* m_Next;
};

class List
{
public:
	List()
	{
		this->m_Head = new Node(0);
		this->m_End = this->m_Head;
	}

	void CreateList_Head(int data);
	void CreateList_End(int data);
	void Insert(int pos, int data);
	void Delete(int pos);
	void Print();

	~List();
   
private:
	Node* m_Head;//头结点
	Node* m_End;//尾结点
};

二、整表的创建,我分为头插法和尾插法

头插法

void List::CreateList_Head(int data)
{
	Node* node = new Node(data);
    //这个if我是让头尾点和尾结点进行分离,这样才好后续的插入
    //初始化时我是头和尾是在一起的
	if (this->m_Head->m_Next == nullptr)
	{
		this->m_Head->m_Next = node;
		this->m_End = node;
	}
	else
	{
		node->m_Next = this->m_Head->m_Next;
		this->m_Head->m_Next = node;
	}
}

尾插法

void List::CreateList_End(int data)
{
	Node* node = new Node(data);
	if (this->m_Head->m_Next == nullptr)
	{
		this->m_Head->m_Next = node;
		this->m_End = node;
	}
	else
	{
		this->m_End->m_Next = node;
		this->m_End = node;
	}
}

三、插入函数,我个人认为是在表建立好的基础上进行插入的,所以你会看到,我的第一个if结果为 “链表为空,插入错误”。这是个人认为。

void List::Insert(int Tar_pos, int data)
{
	if (this->m_Head == this->m_End)
	{
		cout << "链表为空,插入错误" << endl;
		return;
	}
	
	int Cur_pos = 1;
	Node* node = this->m_Head;
	while (node && Cur_pos < Tar_pos)
	{
		node = node->m_Next;
		++Cur_pos;
		//还有就是这里的Cur_pos < Tar_pos为什么是这个?
		//因为,比如我表中已有4个元素了,但你所看到的这4个元素并不是表中的所有元素,
		//因为没算上头结点,所这里的Cur_pos是1并且node是从m_Head开始的,
		//这也意味着当Cur_pos = Tar_pos时node是在Cur_pos位置的上一个的结点的
		//你也可以变成这样

		//int Cur_pos = 1;
		//Node* node = this->m_Head->m_Next;
		//while (node && Cur_pos < Tar_pos - 1) {}
		//如果还不清楚的话,就动手模拟模拟。相信你可以的=。=
	}
	
	if (!node)
	{
		cout << "插入位置无效" << endl;
		return;
	}	

	Node* Insert_Node = new Node(data);
	Insert_Node->m_Next = node->m_Next;
	node->m_Next = Insert_Node;
}

四、删除函数

void List::Delete(int Tar_pos)
{
	int Cur_pos = 1;
	Node* node = this->m_Head;
	while (node && Cur_pos < Tar_pos)
	{
		node = node->m_Next;
		++Cur_pos;
	}
	
	if (!node)
	{
		cout << "删除无效位置" << endl;
		return;
	}

	Node* Temp_node = node->m_Next;
	node->m_Next = Temp_node->m_Next;
	delete Temp_node;
	Temp_node = nullptr;
}

五、析构函数。这里原本是整表的删除的,我把他写成了析构函数,你也可以单独的写删除函数,这里我为了和我手动开辟的空间有与之对应的释放就和在一起了

List::~List()
{
	Node* Temp_node1 = this->m_Head;
	Node* Temp_node2;
	while (Temp_node1)
	{
		Temp_node2 = Temp_node1->m_Next;
		delete Temp_node1;
		Temp_node1 = Temp_node2;
	}
}

六、整体的展示

#include <iostream>

using namespace std;

class Node
{
public:
	Node(int data)
	{
		this->m_Data = data;
		this->m_Next = nullptr;
	}

	int m_Data;
	Node* m_Next;
};

class List
{
public:
	List()
	{
		this->m_Head = new Node(0);
		this->m_End = this->m_Head;
	}

	void CreateList_Head(int data);
	void CreateList_End(int data);
	void Insert(int pos, int data);
	void Delete(int pos);
	void Print();

	~List();

private:
	Node* m_Head;//头结点
	Node* m_End;//尾结点
};

void List::CreateList_Head(int data)
{
	Node* node = new Node(data);
	if (this->m_Head->m_Next == nullptr)
	{
		this->m_Head->m_Next = node;
		this->m_End = node;
	}
	else
	{
		node->m_Next = this->m_Head->m_Next;
		this->m_Head->m_Next = node;
	}
}

void List::CreateList_End(int data)
{
	Node* node = new Node(data);
	if (this->m_Head->m_Next == nullptr)
	{
		this->m_Head->m_Next = node;
		this->m_End = node;
	}
	else
	{
		this->m_End->m_Next = node;
		this->m_End = node;
	}
}

void List::Insert(int Tar_pos, int data)
{
	if (this->m_Head == this->m_End)
	{
		cout << "链表为空,插入错误" << endl;
		return;
	}
	
	int Cur_pos = 1;
	Node* node = this->m_Head;
	while (node && Cur_pos < Tar_pos)
	{
		node = node->m_Next;
		++Cur_pos;
		//还有就是这里的Cur_pos < Tar_pos为什么是这个?
		//因为,比如我表中已有4个元素了,但你所看到的这4个元素并不是表中的所有元素,
		//因为没算上头结点,所这里的Cur_pos是1并且node是从m_Head开始的,
		//这也意味着当Cur_pos = Tar_pos时node是在Cur_pos位置的上一个的结点的
		//你也可以变成这样

		//int Cur_pos = 1;
		//Node* node = this->m_Head->m_Next;
		//while (node && Cur_pos < Tar_pos - 1) {}
		//如果还不清楚的话,就动手模拟模拟。相信你可以的=。=
	}
	
	if (!node)
	{
		cout << "插入位置无效" << endl;
		return;
	}	

	Node* Insert_Node = new Node(data);
	Insert_Node->m_Next = node->m_Next;
	node->m_Next = Insert_Node;
}

void List::Delete(int Tar_pos)
{
	int Cur_pos = 1;
	Node* node = this->m_Head;
	while (node && Cur_pos < Tar_pos)
	{
		node = node->m_Next;
		++Cur_pos;
	}
	
	if (!node)
	{
		cout << "删除无效位置" << endl;
		return;
	}

	Node* Temp_node = node->m_Next;
	node->m_Next = Temp_node->m_Next;
	delete Temp_node;
	Temp_node = nullptr;
}

List::~List()
{
	Node* Temp_node1 = this->m_Head;
	Node* Temp_node2;
	while (Temp_node1)
	{
		Temp_node2 = Temp_node1->m_Next;
		delete Temp_node1;
		Temp_node1 = Temp_node2;
	}
}

void List::Print()
{
	Node* cur = this->m_Head->m_Next;
	while (cur != nullptr)
	{
		cout << cur->m_Data << " ";
		cur = cur->m_Next;
	}
	cout << endl;
}

void Text()
{
	List L;
	L.CreateList_End(6);
	L.CreateList_End(1);
	L.CreateList_End(5);
	L.CreateList_End(7);

	L.Print();
}

int main()
{
	Text();

	system("pause");
	return 0;
}

这里我就不放结果了,如果想要结果的就自己写在Text()里吧。

有不对的地方欢迎指正=。=

我是小白,我是小白,我猜你看不到这里>,<

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值