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

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;

}

打印结果


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值