栈(stack)是一个后进先出(LIFO, last in first out)的表,限制在栈顶位置对元素插入和删除。
栈的基本操作主要有:Push(进栈)、Pop(出栈)。
栈主要有两种实现方式:链表和数组。
栈的链表实现
说明:利用带头指针的单链表实现:指定头指针为栈顶,入栈和出栈操作头指针及其后面的元素。
代码实现(C/C++)
#include<iostream>usingnamespacestd;
typedefint ElemType;
typedefstruct Node {
ElemType date;
Node *next;
}*Stack;
void initStack(Stack St) // 把头节点看作是栈顶
{
St->next = nullptr;
}
int isEmpty(Stack St) // 判断栈是否为空
{
return St->next == nullptr;
}
void Push(Stack St, ElemType x) // 每次从将元素插入到头节点的位置,头节点上移
{
Node* q = new Node;
q->date = x;
q->next = St->next; // q的next域指向St(头节点)的后继结点
St->next = q; // St(头节点/栈顶)next域指向q,实现栈往上移
}
void Pop(Stack St) // 出栈
{
Node* p = St->next; // 临时结点指向头结点(栈顶)的后继结点
St->next = p->next; // 栈顶下移delete p; // 释放空间
}
ElemType Top(Stack St) // 取栈顶元素
{
return St->next->date;
}
void destroyStack(Stack St) // 销毁栈
{
while (St->next)
Pop(St);
}
void print(Stack St) // 打印栈
{
Node *p = St->next;
cout << "Now the Stack contents: " << endl;
while (p)
{
cout << p->date << " ";
p = p->next;
}
cout << endl;
}
int main()
{
const ElemType date[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Stack myStack = new Node;
initStack(myStack); // 初始化栈if (isEmpty(myStack))
cout << "Empty Stack, now Push some values... " << endl;
for (int i = 0;i < 9;i++)
{
Push(myStack, date[i]);
}
print(myStack);
cout << "Push a number to the Stack: \n";
ElemType number;
cin >> number;
Push(myStack, number);
print(myStack);
cout << "Pop twice from the Stack...\n";
Pop(myStack);
Pop(myStack);
cout << "The Top of the Stack now is: " << Top(myStack)<< endl;
cout << "Destroy the Stack..." << endl;
delete myStack;
system("pause");
return0;
}
运行操作结果
Empty Stack, now Push some values...
Now the Stack contents:
987654321
Push a numbertothe Stack:
13
Now the Stack contents:
13987654321
Pop twice fromthe Stack...
The Top ofthe Stack now is: 8
Destroy the Stack...
栈的数组实现
#include<iostream>usingnamespacestd;
typedefint ElemType;
constint MAXSIZE = 50; // 设置栈的最大容量typedefstruct {
int stacksize;
int top;
ElemType elemList[MAXSIZE];
}Stack;
Stack initStack() // 初始化栈
{
Stack St;
St.top = 0;
St.stacksize = 0;
return St;
}
int isEmpty(Stack St) // 判断栈是否为空
{
return St.top == 0;
}
int isFull(Stack St) // 判断栈是否满
{
return St.top == MAXSIZE;
}
void Push(Stack &St, ElemType x) // 入栈,St状态被改变,故要传引用
{
St.elemList[St.top++] = x;
St.stacksize++;
}
ElemType Pop(Stack &St) // 出栈,St状态被改变,故要传引用
{
St.stacksize--;
return St.elemList[--St.top];
}
ElemType Top(Stack St) // 取栈顶元素
{
return St.elemList[St.top - 1];
}
void print(Stack St) // 打印栈
{
cout << "Now the Stack contents: " << endl;
for (int i = St.stacksize-1;i >= 0;i--)
{
cout << St.elemList[i]<<" ";
}
cout << endl;
}
int main()
{
const ElemType date[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Stack myStack = initStack(); // 初始化栈if (isEmpty(myStack))
cout << "Empty Stack, now Push some values... " << endl;
for (int i = 0;i < 9;i++)
{
Push(myStack, date[i]);
}
print(myStack);
cout << "Push a number to the Stack: \n";
ElemType number;
cin >> number;
Push(myStack, number);
print(myStack);
cout << "Pop twice from the Stack...\n";
Pop(myStack);
Pop(myStack);
cout << "The Top of the Stack now is: " << Top(myStack) << endl;
cout << "Destroy the Stack..." << endl;
system("pause");
return0;
}
运行操作结果
Empty Stack, now Push some values...
Now the Stack contents:
987654321
Push a numbertothe Stack:
13
Now the Stack contents:
13987654321
Pop twice fromthe Stack...
The Top ofthe Stack now is: 8
Destroy the Stack...