#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(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){
//Step1. Space check.
if(paraStackPtr->top >= STACK_MAX_SIZE - 1){
printf("Cannot push element: stack full.\r\n");
return;
}//of if
//Step2. Update the top.
paraStackPtr->top ++;
//Step3. Push element.
paraStackPtr->data[paraStackPtr->top] = paraValue;
}//of push
/**
* Pop an element from the stack.
* @return The poped value.
*/
char pop(CharStackPtr paraStackPtr){
//Step1. Space check.
if (paraStackPtr->top < 0){
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}//of if
//Step2. Update the top.
paraStackPtr->top --;
//Step3. 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 ++){
char ch;
ch = pop(tempStack);
printf("Pop %c.\r\n", ch);
outputStack(tempStack);
}//of for i
printf("---- pushPopTest ends. ----\r\n");
}// of pushPopTest
/**
The entance.
*/
void main(){
pushPopTest();
}//of main
#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;
}