摘要: 咋一看栈很怪异, 为啥要搞一个比顺序表更简单的结构? 知道它的一些应用之后才懂得其强大.
1. 代码 (2022 版)
先上代码, 再说废话.
#include <stdio.h>
#include <malloc.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of integers. The key is data.
*/
typedef struct CharStack {
int top;
char 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(struct 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. Pop element.
return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop
/**
* Test the push function.
*/
void pushPopTest() {
char ch;
int i;
printf("---- pushPopTest begins. ----\r\n");
// Initialize.
CharStackPtr tempStack = charStackInit();
printf("After initialization, the stack is: ");
outputStack(tempStack);
// Pop.
for (ch = 'a'; ch < 'm'; ch ++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}//Of for i
// Pop.
for (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
2. 运行结果
---- 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
3. 代码说明
- 初始化为一个空栈. top 指向栈顶元素, 所以它的初始值为 − 1 -1 −1.
- push 的时候, 需要先改变 top 再放元素.
- pop 的时候, 需要先改变 top 再 return.
附 1: 2021 代码
辜果同学的代码.
#include "stdio.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 //最大存储空间
/* 顺序栈结构 */
typedef struct
{
int data[MAXSIZE];
int top;
} SqStack;
/* 构造一个空栈S */
int InitStack(SqStack *S)
{
S->top=-1;
return OK;
}
// 若栈S为空栈,则返回TRUE,否则返回FALSE
int StackEmpty(SqStack S)
{
if (S.top==-1)
return TRUE;
else
return FALSE;
}
//返回S的元素个数,即栈的长度
int StackLength(SqStack S)
{
return S.top+1;
}
//栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
int GetTop(SqStack S,int *e)
{
if (S.top==-1)
return ERROR;
else
*e=S.data[S.top];
return OK;
}
// 插入元素e为新的栈顶元素
int Push(SqStack *S,int e)
{
if(S->top == MAXSIZE -1)
{
return ERROR;
}
S->top++;
S->data[S->top]=e;
return OK;
}
//若栈不空,则删除S的栈顶元素
int Pop(SqStack *S,int *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top];
S->top--;
return OK;
}
// 从栈底到栈顶依次对栈中每个元素显示
int StackPrint(SqStack S)
{
int i;
i=0;
while(i<=S.top)
{
printf("%d ",S.data[i++]);
}
printf("\n");
return OK;
}
//把S置为空栈
int ClearStack(SqStack *S)
{
S->top=-1;
return OK;
}
int main()
{
int j;
SqStack s;
int e;
if(InitStack(&s)==OK)
printf("创建空栈成功!请继续操作。\n");
printf("请输入需要添加的数据个数:\n");
scanf("%d",&j);
printf("请输入需要添加的数据:");
for(int m = 1; m<=j; m++)
{
scanf("%d",&e);
Push(&s,e);
}
printf("数据添加完成!\n");
printf("栈中元素依次为:");
StackPrint(s);
Pop(&s,&e);
printf("弹出的栈顶元素 e=%d\n",e);
printf("栈中元素依次为:");
StackPrint(s);
//判断是否为空栈
printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));
GetTop(s,&e);
printf("栈顶元素 e=%d 栈的长度为%d\n",e,StackLength(s));
ClearStack(&s);
printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
return 0;
}
欢迎留言!