栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
先附上老师的代码
#include <stdio.h>
#include <malloc.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of integers. The key is data.
*/
typedef struct CharStack {
int top;
int data[STACK_MAX_SIZE]; //The maximum length is fixed.
} *CharStackPtr;
/**
* Output the stack.
*/
void outputStack(CharStackPtr paraStack) {
for (int i = 0; i <= paraStack->top; i ++) {
printf("%c ", paraStack->data[i]);
}// Of for i
printf("\r\n");
}// Of outputStack
/**
* Initialize an empty char stack. No error checking for this function.
* @param paraStackPtr The pointer to the stack. It must be a pointer to change the stack.
* @param paraValues An int array storing all elements.
*/
CharStackPtr charStackInit() {
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
resultPtr->top = -1;
return resultPtr;
}//Of charStackInit
/**
* Push an element to the stack.
* @param paraValue The value to be pushed.
*/
void push(CharStackPtr paraStackPtr, int paraValue) {
// Step 1. Space check.
if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
printf("Cannot push element: stack full.\r\n");
return;
}//Of if
// Step 2. Update the top.
paraStackPtr->top ++;
// Step 3. Push element.
paraStackPtr->data[paraStackPtr->top] = paraValue;
}// Of push
/**
* Pop an element from the stack.
* @return The poped value.
*/
char pop(CharStackPtr paraStackPtr) {
// Step 1. Space check.
if (paraStackPtr->top < 0) {
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}//Of if
// Step 2. Update the top.
paraStackPtr->top --;
// Step 3. Push element.
return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop
/**
* Test the push function.
*/
void pushPopTest() {
printf("---- pushPopTest begins. ----\r\n");
// Initialize.
CharStackPtr tempStack = charStackInit();
printf("After initialization, the stack is: ");
outputStack(tempStack);
// Pop.
for (char ch = 'a'; ch < 'm'; ch ++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}//Of for i
// Pop.
for (int i = 0; i < 3; i ++) {
ch = pop(tempStack);
printf("Pop %c.\r\n", ch);
outputStack(tempStack);
}//Of for i
printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest
/**
The entrance.
*/
void main() {
pushPopTest();
}// Of main
运行结果
---- pushPopTest begins. ----
After initialization, the stack is:
Pushing a.
a
Pushing b.
a b
Pushing c.
a b c
Pushing d.
a b c d
Pushing e.
a b c d e
Pushing f.
a b c d e f
Pushing g.
a b c d e f g
Pushing h.
a b c d e f g h
Pushing i.
a b c d e f g h i
Pushing j.
a b c d e f g h i j
Pushing k.
Cannot push element: stack full.
a b c d e f g h i j
Pushing l.
Cannot push element: stack full.
a b c d e f g h i j
Pop j.
a b c d e f g h i
Pop i.
a b c d e f g h
Pop h.
a b c d e f g
---- pushPopTest ends. ----
Press any key to continue
我的代码
#include<bits/stdc++.h>
using namespace std;
#define MaxSize 100 //定义栈中元素的最大个数
typedef struct SqStack{
int data[MaxSize]; //存放栈中的元素
int top; //栈顶指针
}SqStack;
//初始化
void InitStack(SqStack &S){
S.top = -1;
}
//判栈空
bool Empty(SqStack S){
if(S.top == -1){
return true;
}else{
return false;
}
}
//入栈
void Push(SqStack &S, int x){
if(S.top == MaxSize-1){
cout<<"栈满"<<endl;
return;
}
S.data[++S.top] = x;
}
//出栈
void Pop(SqStack &S, int &x){
if(S.top == -1){
cout<<"栈空"<<endl;
return;
}
x = S.data[S.top--];
}
//读栈顶元素
int GetTop(SqStack S){
if(S.top == -1){
cout<<"栈空"<<endl;
return -1;
}else{
return S.data[S.top];
}
}
//遍历栈
void PrintStack(SqStack S){
while(S.top != -1){
cout<<S.data[S.top--]<<" ";
}
cout<<endl;
}
//销毁栈
void DestroyStack(SqStack &S){
S.top = -1;
}
int main(){
SqStack S;
InitStack(S);
Push(S,1);//入栈
Push(S,2);
Push(S,3);
Push(S,4);
cout<<"栈顶元素为:"<<GetTop(S)<<endl;
cout<<"出栈顺序为:";
PrintStack(S);
int x;
Pop(S,x);
cout<<x<<"出栈"<<endl;
cout<<"栈中剩余元素:";
PrintStack(S);
Pop(S,x);
cout<<x<<"出栈"<<endl;
cout<<"栈中剩余元素:";
PrintStack(S);
if(!Empty(S)){
cout<<"当前栈不为空"<<endl;
}else{
cout<<"当前栈为空"<<endl;
}
return 0;
}
运行结果
栈顶元素为:4
出栈顺序为:4 3 2 1
4出栈
栈中剩余元素:3 2 1
3出栈
栈中剩余元素:2 1
当前栈不为空