栈是最常见的数据结构,其特点是后进先出(Last In First Out)也是链表的特殊形式,所以和链表一样,有两种存储方式,第一是顺序存储的栈,方便快速读写数据,但是栈的长度必须先固定;第二种是链式存储的栈,可以不用定义栈的长度,可以大量插入数据,如果不是物理内存使用完的话,可以存储大量的数据。
首先,顺序存储的栈的实现,代码如下:
#pragma once
#define MAXSIZE 10
template<typename EleType>
class OrderStack
{
public:
OrderStack();
~OrderStack();
bool GetTop(EleType& e);
bool Push(const EleType& e);
bool Pop(EleType& e);
void Show()const;
private:
EleType data[MAXSIZE];
int top;
bool Empty()const;
bool Full()const;
int StackLength()const;
};
#include "OrderStack.h"
#include <iostream>
using namespace std;
template<typename EleType>
bool OrderStack<EleType>::Pop(EleType& e)
{
if (Empty())
{
cout << "The stack is empty!\n";
return false;
}
e = data[top];
--top;
return true;
}
template<typename EleType>
bool OrderStack<EleType>::Push(const EleType& e)
{
if (Full())
{
return false;
}
++top;
data[top] = e;
return true;
}
template<typename EleType>
bool OrderStack<EleType>::GetTop(EleType& e)
{
if (Empty())
{
return false;
}
e = data[top];
return true;
}
template<typename EleType>
OrderStack<EleType>::~OrderStack()
{
}
template<typename EleType>
OrderStack<EleType>::OrderStack() :top(-1)
{
}
template<typename EleType>
int OrderStack<EleType>::StackLength() const
{
return top + 1;
}
template<typename EleType>
bool OrderStack<EleType>::Full() const
{
return (top == MAXSIZE - 1);
}
template<typename EleType>
bool OrderStack<EleType>::Empty() const
{
return (top == -1);
}
template<typename EleType>
void OrderStack<EleType>::Show() const
{
if (Empty())
{
cout << "The stack is Empty!\n";
return;
}
cout << "The stack length is: " << StackLength() << endl;
cout << "The stack is: ";
for (int i = top; i >= 0;--i)
{
cout << data[i] << " ";
}
cout << endl;
}
第二,栈的链式存储,代码如下:
#pragma once
template<typename EleType>
class ChainStack
{
public:
ChainStack();
~ChainStack();
bool GetTop(EleType& e);
bool Push(const EleType& e);
bool Pop(EleType& e);
void Show()const;
private:
bool Empty()const;
//bool Full()const;
int StackLength()const;
struct Node
{
EleType data;
Node* next;
Node(){ next = nullptr; }
};
Node* top;
int length;
};
#include "ChainStack.h"
#include <iostream>
using namespace std;
template<typename EleType>
int ChainStack<EleType>::StackLength() const
{
return length;
}
// template<typename EleType>
// bool ChainStack<EleType>::Full() const
// {
//
// }
template<typename EleType>
bool ChainStack<EleType>::Empty() const
{
return (length == 0);
}
template<typename EleType>
void ChainStack<EleType>::Show() const
{
if (Empty())
{
cout << "The stack is empty!\n";
return;
}
cout << "The stack length is " << length << endl;
cout << "The stack is ";
Node* temp=top;
for (int i = 1; i <= length;++i)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
template<typename EleType>
bool ChainStack<EleType>::Pop(EleType& e)
{
if (Empty())
{
cout << "The stack is empty!\n";
return false;
}
e = top->data;
Node* temp = top;
top = top->next;
--length;
delete temp;
return true;
}
template<typename EleType>
bool ChainStack<EleType>::Push(const EleType& e)
{
Node* temp = new Node;
temp->data = e;
if (Empty())
{
top = temp;
}
else
{
temp->next = top;
top = temp;
}
++length;
return true;
}
template<typename EleType>
bool ChainStack<EleType>::GetTop(EleType& e)
{
if (Empty())
{
cout << "The stack is empty!\n";
return false;
}
e = top->data;
return true;
}
template<typename EleType>
ChainStack<EleType>::~ChainStack()
{
Node* temp = top;
while (top)
{
temp = top;
top = temp->next;
delete temp;
}
}
template<typename EleType>
ChainStack<EleType>::ChainStack() :length(0), top(nullptr)
{
}