You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.1 KiB
68 lines
1.1 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define StackSize 100
|
|
typedef char DataType;
|
|
typedef struct {
|
|
DataType data[StackSize];
|
|
int top;
|
|
} SeqStack;
|
|
|
|
|
|
//置空栈
|
|
void initStack(SeqStack *S) {
|
|
S->top = -1;
|
|
}
|
|
|
|
//判栈空
|
|
int stackEmpty(SeqStack *S) {
|
|
return S->top == -1;
|
|
}
|
|
|
|
//判栈满
|
|
int stackFull(SeqStack *S) {
|
|
return S->top == StackSize - 1;
|
|
}
|
|
|
|
//进栈(入栈)
|
|
void push(SeqStack *S, DataType x) {
|
|
if (stackFull(S)) {
|
|
} else {
|
|
S->top = S->top + 1;
|
|
S->data[S->top] = x;
|
|
}
|
|
}
|
|
|
|
|
|
//退栈(出栈)
|
|
|
|
DataType pop(SeqStack *S) {
|
|
if (stackEmpty(S)) {
|
|
printf("stack underflow");
|
|
exit(0);
|
|
} else {
|
|
return S->data[S->top--];
|
|
}
|
|
}
|
|
|
|
//取栈顶元素
|
|
DataType getTop(SeqStack *S) {
|
|
if (stackEmpty(S)) {
|
|
printf("stack empty");
|
|
exit(0);
|
|
} else {
|
|
return S->data[S->top];
|
|
}
|
|
}
|
|
|
|
// 遍历并输出栈中的所有元素
|
|
void printStack(SeqStack *S) {
|
|
int i;
|
|
printf("top = %d\n",S->top);
|
|
printf("Stack contents: ");
|
|
for (i = S->top; i >= 0; i--) {
|
|
printf("%c", S->data[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
} |