数据结构—类模板实现链表

本文介绍了一个通用模板链表的实现方法,并通过一个具体的示例展示了如何使用该链表进行元素的插入、删除等操作。同时,文章还探讨了链表在不同场景下的运用。

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

template_linklist.hpp

#pragma once
#include<iostream>
using namespace std;
//链表节点
template<class T>
class LinkNode
{
public:
	T data;//如果T是类类型的话记得一定要要在类内提供默认构造函数
	LinkNode<T>* next;
};

//链表
template<class T>
class LinkList
{
public:
	LinkNode<T> header;
	int size;

	LinkList()
	{
		this->header.next = NULL;
		this->size = 0;
	}
	//指定位置插入
	void Inset_LinkList(int pos, T data)
	{
		if (pos<0 || pos>this->size)
		{
			pos = this->size;
		}
		//创建新的节点
		LinkNode<T>* NewNode = new LinkNode<T>;
		NewNode->data = data;
		NewNode->next = NULL;
		//新节点入链表
		LinkNode<T>* Pcurrent = &this->header;
		for (int i = 0; i < pos; ++i)
		{
			Pcurrent = Pcurrent->next;
		}
		NewNode->next = Pcurrent->next;
		Pcurrent->next = NewNode;
		++this->size;

	}
	//头部插入
	void Push_Front(T data)
	{
		Inset_LinkList(0, data);
	}
	//尾部插入
	void Push_Back(T data)
	{
		Inset_LinkList(this->size, data);
	}
	//指定位置删除
	void RemoveByPos(int pos)
	{
		if (pos<0 || pos>this->size - 1)
		{
			cout << "RemoveByPos Transboundary" << endl;
			return;
		}

		//找到要删除位置的前一个节点
		LinkNode<T>* Pcurrent = &this->header;
		for (int i = 0; i < pos; ++i)
		{
			Pcurrent = Pcurrent->next;
		}
		LinkNode<T>* PDel = Pcurrent->next;
		Pcurrent->next = PDel->next;//把删除节点位置的前一个节点和删除节点位置的后一个节点连接起来
		delete PDel;
		--this->size;
	}
	//头删
	void Remove_Front()
	{
		if (this->size == 0)
		{
			cout << "size is 0,Remove_Front error" << endl;
			return;
		}
		RemoveByPos(0);
	}
	//尾删
	void Remove_Back()
	{
		if (this->size == 0)
		{
			cout << "size is 0,Remove_Back error" << endl;
			return;
		}
		RemoveByPos(this->size - 1);
	}
	//获得链表大小
	int Size_LinkList()
	{
		return this->mSize;
	}
	//值删除
	void RemoveByVal(T data)
	{
		if (this->size == 0)
		{
			cout << "size is 0,RemoveByVal error" << endl;
			return;
		}
		//辅助指针
		LinkNode<T>* Pcurrent = &this->header;
		LinkNode<T>* PDel = Pcurrent->next;
		while (PDel != NULL)
		{
			if (PDel->data == data)
			{
				Pcurrent->next = PDel->next;
				delete PDel;
				--this->size;
				break;
			}
			Pcurrent = PDel;
			PDel = PDel->next;
		}
	}

	//打印链表,函数模板
	template<class MYPRINT>
	void Print_LList(MYPRINT print_llist)
	{
		LinkNode<T>* Pcurrent = this->header.next;
		while (Pcurrent != NULL)
		{
			print_llist(Pcurrent->data);
			Pcurrent = Pcurrent->next;
		}
	}

public:
	LinkNode<T> header;
	int size;

};


template_linklist.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"template_linklist.hpp"
#include <string>
class person
{
public:
	person(){};//为节点的类模板T data提供默认构造函数
	person(string Name, int Age)
	{
		name = Name;
		age = Age;
	}
	bool operator==(const person& p)//重载等于,因为在值删除函数中使用了==符号
	{
		return this->name == p.name && this->age == p.age;
	}

public:
	string name;
	int age;

};

void Print_list(person& p )
{
	cout << "name:"<<p.name <<"age:"<< p.age << endl;
}

struct Print_list2
{
	void operator()(person& p)
	{
		cout << "name:" << p.name << "age:" << p.age << endl;
	}
};

int main()
{
	LinkList<person> list;
	person p1("aaa", 10);
	person p2("bbb", 20);
	person p3("ccc", 30);
	person p4("ddd", 40);
	person p5("eee", 50);
	person p6("fff", 60);

	//插入
	list.Push_Front(p1);
	list.Push_Front(p2);
	//尾插
	list.Push_Back(p3);
	list.Push_Back(p4);
	//指定位置插入
	list.Inset_LinkList(4, p5);
	list.Inset_LinkList(2, p6);
	list.Print_LList(Print_list);
	cout << "------------------" << endl;
	//指定位置删除
	list.RemoveByPos(5);
	//指定值删除
	list.RemoveByVal(p6);
	list.Print_LList(Print_list);
	cout << "------------------" << endl;
	//头删
	list.Remove_Front();
	list.Remove_Front();
	//尾删
	list.Remove_Back();
	list.Remove_Back();
	
	list.Print_LList(Print_list);
	getchar();
	return 0;

}

打印结果


面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值