c++数据结构之单向链表

链表中的每个节点的数据我们用节点Node 表示,单向链表主要包括创建、插入、删除、链表长度等功能,

插入链表的头文件linkedlist.h

/*链表的实现,实现创建、插入、删除等操作*/
#include <string>
using namespace std;

struct StuInfo
{
	int id;  //学号
	string name;   //姓名
};

struct Node
{
	StuInfo info;
	Node *next;
	Node(){}
	Node(StuInfo x){ info = x; next = NULL; }
};

class LinkedList
{
public:
	LinkedList(); //构造函数
	~LinkedList(); //析构函数
	void insert(StuInfo info, int pos);
	void remove(int id);  //根据学生学号进行删除
	int find(int id); //根据学号进行查找,返回节点位置
	int getLength(); //获得链表长度
	void print();  //打印链表信息
private:
	Node *head;
	int length;

};

链表的接口的实现在文件linklist.cpp中,代码如下:

#include <iostream>
#include "linkedlist.h"
using namespace std;

//构造函数
LinkedList::LinkedList()
{
	head = NULL;
	length = 0;
}

//析构函数
LinkedList::~LinkedList()
{
	Node* temp;
	for (int i = 0; i < length; i++)
	{
		temp = head;
		head = head->next;
		delete temp;
	}

}

void LinkedList::insert(StuInfo info, int pos)
{
	if (pos < 0)
	{
		cout << "节点位置必须是正整数" << endl;
	}
	int index = 1;
	Node *temp = head;
	Node *node = new Node(info);
	if (pos == 0)
	{
		node->next = temp;
		head = node;
		length++;
		return;
	}
	while (index < pos && temp != NULL)
	{
		temp = temp->next;
		index++;
	}
	if (temp == NULL)
	{
		cout << "插入失败,位置序号超过链表长度" << endl;
		return;
	}
	node->next = temp->next;
	temp->next = node;
	length++;
	return;
}

void LinkedList::remove(int id)
{
	int pos = find(id);
	if (pos == -1)
	{
		cout << "链表中没有该学号,无法删除对应信息" << endl;
		return;
	}
	if (pos == 0)
	{
		head = head->next;
		length--;
		return;
	}
	int index = 1;
	Node *temp = head;
	while (index < pos)
	{
		temp = temp->next;
	}
	temp->next = temp->next->next;
	length--;
	return;
}

int LinkedList::find(int id)
{
	Node *temp = head;
	int index = 0;
	while (temp != NULL)
	{
		if (temp->info.id == id)
		{
			return index;
		}
		temp = temp->next;
		index++;
	}
	return -1;
}

int LinkedList::getLength()
{
	return length;
}

void LinkedList::print()
{
	if (head == NULL)
	{
		cout << "链表为空,没有信息" << endl;
		return;
	}
	Node *temp = head;
	while (temp != NULL)
	{
		cout << temp->info.id << "," << temp->info.name << endl;
		temp = temp->next;
	}
	cout << endl;
	return;
}

最后再写一个测试程序,测试代码文件linklist-demo.cpp

#include <iostream>
#include <string>
#include "linkedlist.h"
using namespace std;
int main()
{
	LinkedList head;
	StuInfo info1, info2, info3, info4, info5;
	info1.id = 1001, info1.name = "张三", info2.id = 1002, info2.name = "李四", info3.id = 1003, info3.name = "王五", info4.id = 1004, info4.name = "南瓜", info5.id = 1005, info5.name = "黄瓜";

	//测试插入数据
	cout << "测试插入:" << endl;
	head.insert(info1, 0);
	head.print();
	head.insert(info2, 1);
	head.print();
	head.insert(info3, 2);
	head.print();
	head.insert(info4, 0);
	head.print();
	head.insert(info5, 2);
	head.print();
	//测试删除数据
	cout << "测试删除:" << endl;
	cout << "链表长度为:" << head.getLength() << endl;
	head.remove(1004);
	head.print();
	cout << "链表长度为:" << head.getLength() << endl;
	head.remove(1004);
	head.print();
	return 0;
}

我们编译下代码,运行

g++ linklist.cpp linklist-demo.cpp -o linklist-demo

生成可执行文件linklist-demo,运行结果如下


在这里实现的单向链表表头后的第一个结点是0号结点,后面的才是1号结点,这里要注意一下,不然会看的晕头转向的。

猜你喜欢

转载自blog.csdn.net/u012154319/article/details/80715627