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;
}
欢迎大家留言,有不懂的可以相互交流学习。
版权声明:本文为u011870022原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。