C++自定义实现通用链表栈

栈作为一种常见的数据结构,在我们平时中接触的比较多,在某些应用中有着重要的是作用,比如说在我们实现计算器计算表达式的结果时,就需要使用栈作为存储的数据结构。下面,我们就是用C++ 实现它的通用链式结构栈。

首先是栈的结点类,使用类的模板来定义:

#pragma once
#include<iostream>
#include<string >
using namespace std;
template< class T>
class Node
{
public:
	//默认构造
	Node();
	//有参构造
	Node(T t);
	~Node();
	//节点值
	T t;
	//下一个节点
	Node<T> *next;
	//上一个节点
	Node<T> *pre;

};
template<class T>
inline Node<T>::Node()
{
	
}
template< class T>
Node<T>::Node(T t)
{
	this->t = t;
}
template< class T>
Node<T>::~Node()
{
}

然后是链表的具体实现:

#pragma once
#include<iostream>
#include<string>
#include "Node.hpp"
using namespace std;
template<class T>
class LinkedList
{
public:
	LinkedList();
	LinkedList(T t[],int n);
	LinkedList( LinkedList<T> &list);
	//尾插法插入节点
	void add(const  T t);
	//删除节点
	Node<T>* removeNode(int index);
	//更新节点
	bool setNode(int index, const T t) const;
	//获取节点
	Node<T>* getNode(int index) const;
	//插入节点
	bool insertNode(int index, const T t) ;
	//获取节点个数
	int Size();
	//判断节点是否为空
	bool isEmpty();
	//清空全部节点
	void clear();
	 LinkedList<T> & operator=( LinkedList<T> &list);
	//析构函数释放节点的空间资源
	~LinkedList();
protected:
	//头结点
	Node<T> *head;
	//尾节点
	Node<T> *tail;
	//节点个数
	int size;
};

template<class T>
inline LinkedList<T>::LinkedList()
{
	//建立空的头结点
	head = new Node<T>();
	//建立空的尾节点
	tail = new Node<T>();
	//建立空链表
	head->next = tail;
	tail->pre = head;
}

template<class T>
inline LinkedList<T>::LinkedList(T t[], int n)
{
	//建立空的头结点
	head = new Node<T>();
	//建立空的尾节点
	tail = new Node<T>();
	//建立空链表
	head->next = tail;
	tail->pre = head;
	for(int i = 0;i < n;i++)
	{
		this->add(t[i]);
	}
}

template<class T>
inline LinkedList<T>::LinkedList( LinkedList<T>& list)
{
	//建立空的头结点
	head = new Node<T>();
	//建立空的尾节点
	tail = new Node<T>();
	for(int i = 1; i <= list.Size();i++)
	{
		T  t = list.getNode(i)->t;
		this->add(t);
	}
}

template<class T>
inline void LinkedList<T>::add(const T t)
{
	Node<T> *node = new Node<T>(t);
	//第一个节点
	if (head->next == NULL) {
		head->next = node;
		node->pre = head;
		node->next = tail;
		tail->pre = node;
	}
	else {
		tail->pre->next = node;
		node->pre = tail->pre;
		node->next = tail;
		tail->pre = node;
	}
	//节点数自增一
	this->size++;
}

template<class T>
inline Node<T>* LinkedList<T>::removeNode(int index)
{
	if (index > this->size || index <= 0)
	{
		return NULL;
	}
	Node<T> * temp = this->getNode(index);
	
	temp->pre->next = temp->next;
	temp->next->pre = temp->pre;
	//节点个数减一
	this->size--;
	//返回要删除节点
	return temp;
}

template<class T>
inline bool LinkedList<T>::setNode(int index, const T  t) const
{
	if (index > this->size || index <= 0)
	{
		return false;
	}
	//获得重新设置的节点
	Node<T>* temp = this->getNode(index);
	//更新节点的数据
	temp->t = t;
	return true;
}

template<class T>
inline Node<T>* LinkedList<T>::getNode(int index) const
{    
	if (index <= 0 || index > this->size) {
		return NULL;
	}
	//获取第一个节点
	Node<T> *temp = NULL;
	//要获取的节点位于左半边
	if ((this->size >> 1) > index) {
		temp = head->next;
	for (int i = 1; i < index ; i++)
	{
		if (index == i ) {
			break;
		}
		temp = temp->next;
	}
	}
	//要获取的节点位于右半边
	else {
		temp = tail->pre;
		for (int i = this->size; i >= index;i--) {
			if (index == i) {
				break;
			}
			temp = temp->pre;
		}
	}
	return temp;
}

template<class T>
inline bool LinkedList<T>::insertNode(int index, const  T t)
{
	//索引违法的情况
	if (index <=0 ||index > this->size) {
		return false;
	}
	//获得要插入位置的原节点
	Node<T>* temp = this->getNode(index);
	//实例化新的节点
	Node<T> *newNode = new Node<T>(t);
        //插入节点操作	
		temp->pre->next = newNode;
		newNode->pre = temp->pre;
		newNode->next = temp;
		temp->pre = newNode;
	//节点数自增1
	this->size++;
	return true;
}

template<class T>
inline int LinkedList<T>::Size()
{
	return this->size;
}

template<class T>
inline bool LinkedList<T>::isEmpty()
{
	return this->size == 0;
}

template<class T>
inline void LinkedList<T>::clear()
{
	//释放除了头结点和尾节点之外的节点空间
	Node<T> *p = head->next;
	Node<T> *q = p;
	while (true)
	{
		p = p->next;
		if(p == tail)
		{
			break;
		}
		delete q;
		q = p;
	}
	//将头结点和尾节点的next和pre指针置空,特别重要!
	head->next = tail;
	head->pre = NULL;
	tail->next = NULL;
	tail->pre = head;
	this->size = 0;
}

template<class T>
inline LinkedList<T>& LinkedList<T>::operator=( LinkedList<T>& list)
{
	//先清空原来链表的节点释放空间
	if(this->size != 0)
	{
		this->clear();
	}
	else {
		//建立空的头结点
		head = new Node<T>();
		//建立空的尾节点
		tail = new Node<T>();
		//建立空链表
		head->next = tail;
		tail->pre = head;
	}

	for (int i = 1; i <= list.Size(); i++)
	{
		T  t = list.getNode(i)->t;
		this->add(t);
	}

}

template<class T>
inline LinkedList<T>::~LinkedList()
{
	//释放链表空间
	Node<T> *p = head;
	Node<T> *q = p;
	while (p != NULL)
	{
		p = p->next;
		delete q;
		q = p;
	}
}

这样链表栈我们就实现了,接下来就可以进行测试了:

#include<iostream>
#include"LinkedList.hpp"
using namespace std;

int main()
{
	
	LinkedList<int> *list = new LinkedList<int>();
	list->add(1);
	list->add(2);
	for (int i = 0; i < list->Size(); i++)
	{
		cout << list->getNode(i)->t;
	}
	system("pause");
	return 0;
}

欢迎大家留言,有不懂的可以相互交流学习。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌意随影

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值