一. 堆栈(Stack)
堆栈(Stack) 是一种可以存取的数据结构,其主要特征是先进后出(Last-in-first-out, LIFO)
栈顶(Top) 是最后入栈的数据
堆栈可分为静态堆栈(Static Stack)和动态堆栈(Dynamic Stack)两类:
- Static Stack: 栈的大小固定,可以用数组实现;
- Dynamic Stack: 栈的大小随需要改变,可以用链表实现
二. 基本操作(Basic Operation)
- 入栈(Push):Equivalent to an insert
- 出栈(Pop): Deletes the most recently inserted element
- 查看栈顶(Top): Examines the most recently inserted element Data Structures
三. C++代码
- 静态堆栈(Static Stack):
#include <iostream>
using namespace std;
class Stack
{
public:
Stack(int size);// constructor
~Stack();
bool IsEmpty() { return top == -1; }
bool IsFull() { return top == maxTop; }
double Top();
void Push(const double x);
double Pop();
void DisplayStack();
private:
int maxTop;// max stack size = size -1
int top;// current top of stack
double* values;// element array
};
Stack::Stack(int size)
{
maxTop = size -1;
values = new double[size];
top = -1;
}
Stack::~Stack()
{
delete[] values;
}
void Stack::Push(const double x)
{
if (IsFull())
cout<< "Error: the stack is full." << endl;
else
values[++top] = x;
}
double Stack::Pop()
{
if (IsEmpty())
{
cout<< "Error: the stack is empty." << endl;
return -1;
}
else
{
return values[top--];
}
}
double Stack::Top()
{
if (IsEmpty())
{
cout<< "Error: the stack is empty." << endl;
return -1;
}
else
return values[top];
}
void Stack::DisplayStack()
{
cout<< "top -->";
for (int i= top; i>= 0; i--)
cout<< "\t|\t" << values[i] << "\t|" << endl;
cout<< "\t|---------------|" << endl;
}
int main(void)
{
Stack stack(3);
stack.Push(5.0);
stack.Push(6.5);
stack.Push(-3.0);
stack.Push(-8.0);
stack.DisplayStack();
cout<< "Top: " << stack.Top() << endl;
stack.Pop();
cout<< "Top: " << stack.Top() << endl;
while (!stack.IsEmpty())
stack.Pop();
stack.DisplayStack();
return 0;
}
- 动态堆栈(Dynamic Stack):
#include <iostream>
using namespace std;
class Node {
public:
double data; // data
Node* next; // pointer to next
};
class List {
public:
List(void);// constructor
~List(void);// destructor
bool IsEmpty();
int InsertNode(int index, double x);
int DeleteNode(double x);
void DeleteAll();
void DisplayList();
private:
Node* head;
friend class Stack;
};
class Stack : public List
{
public:
Stack() {}// constructor
~Stack() {}// destructor
double Top();
void Push(double x);
double Pop();
void DisplayStack();
};
List::List(void) {
head = NULL;
}
List::~List(void) {
delete head;
}
bool List::IsEmpty() {
if (head->next == NULL) {
return true;
}
return false;
}
int List::InsertNode(int index, double x) {
if(index < 0)
return -1;
int currIndex = 1;
Node* currNode = head;
while(currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if(index > 0 && currNode == NULL)
return -1;
Node* newNode = new Node;
newNode->data = x;
if(index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return 0;
}
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while(currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if(currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else{
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DeleteAll()
{
Node* currNode = head->next;
Node* newNode = new Node;
while (currNode != NULL)
{
newNode = currNode;
currNode = currNode->next;
head->next = currNode;
newNode->next = NULL;
delete newNode;
}
head = NULL;
}
void List::DisplayList() {
int num = 0;
Node* currNode = head;
while(currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout<< "Number of nodes in the list: " << num << endl;
}
double Stack::Top()
{
if (head == NULL)
{
cout<< "Error: the stack is empty." << endl;
return -1;
}
else
return head->data;
}
void Stack::Push(double x)
{
InsertNode(0, x);
}
double Stack::Pop()
{
if (head == NULL)
{
cout<< "Error: the stack is empty." << endl;
return -1;
}
else
{
double val = head->data;
DeleteNode(val);
return val;
}
}
void Stack::DisplayStack()
{
DisplayList();
}
int main(void)
{
Stack stack;
stack.Push(5.0);
stack.Push(6.5);
stack.Push(-3.0);
stack.Push(-8.0);
stack.DisplayStack();
cout<< "Top: " << stack.Top() << endl;
stack.Pop();
cout<< "Top: " << stack.Top() << endl;
while (!stack.IsEmpty())
stack.Pop();
stack.DisplayStack();
return 0;
}