1.参考教材P61-63完成顺序栈的设计,设顺序表元米的类型为char类型,栈的容量为6.肝写如下的上丽数测试。上丽数中调用顺序校的基木操11完成如下操作: (1)初始化顺序栈L(2)判断找是否为空 (3)依次压入字母A、B、C、D (4)判断栈是否为空,是否为满 (5)再依次压入字时E、F、G,同时判断是香压入成功 (6)判断栈是否为满 (7)取栈项元素井输山
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
#define SUCCESS 1
#define FAILURE 0
#define MAXSIZE 6
typedef struct {
char data[MAXSIZE];
int top;
}seqStack;
seqStack *init_seqStack(); // 置空栈
int empty_seqStack(seqStack *s); // 判栈空
int full_seqStack(seqStack *s); // 判栈满
int push_seqStack(seqStack *s, char x); // 入栈
int pop_seqStack(seqStack *s, char *x); // 出栈
int gettop_seqStack(seqStack *s, char *x); // 取栈顶元素
void show_seqStack(seqStack *s); // 输出
void free_seqStack(seqStack **s); // 释放空间
void main() {
seqStack *s;
char x;
s = init_seqStack();
printf("判断栈是否为空:%s\n",empty_seqStack(s)?"为空":"不为空");
printf("A压入%s\n",push_seqStack(s,'A')?"成功":"失败");
printf("B压入%s\n",push_seqStack(s,'B')?"成功":"失败");
printf("C压入%s\n",push_seqStack(s,'C')?"成功":"失败");
printf("D压入%s\n",push_seqStack(s,'D')?"成功":"失败");
printf("判断栈是否为空:%s\n",empty_seqStack(s)?"为空":"不为空");
printf("判断栈是否为满:%s\n",full_seqStack(s)?"为满":"不为满");
printf("E压入%s\n",push_seqStack(s,'E')?"成功":"失败");
printf("F压入%s\n",push_seqStack(s,'F')?"成功":"失败");
printf("G压入%s\n",push_seqStack(s,'G')?"成功":"失败");
printf("判断栈是否为满:%s\n",full_seqStack(s)?"为满":"不为满");
gettop_seqStack(s,&x);
printf("取栈项元素:%c\n", x);
}
seqStack *init_seqStack() {
seqStack *s;
s = (seqStack *)malloc(sizeof(seqStack));
s->top = -1;
return s;
}
int empty_seqStack(seqStack *s) {
if (s->top == -1) {
return TRUE;
}
return FALSE;
}
int full_seqStack(seqStack *s) {
if (s->top == MAXSIZE - 1) {
return TRUE;
}
return FALSE;
}
int push_seqStack(seqStack *s, char x) {
if (full_seqStack(s)) {
return FAILURE;
}
s->top++;
s->data[s->top] = x;
return SUCCESS;
}
int pop_seqStack(seqStack *s, char *x) {
if (empty_seqStack(s)) {
return FAILURE;
}
*x = s->data[s->top];
s->top--;
return SUCCESS;
}
int gettop_seqStack(seqStack *s, char *x) {
if (empty_seqStack(s)) {
return FAILURE;
}
*x = s->data[s->top];
return SUCCESS;
}
void show_seqStack(seqStack *s) {
int i;
if (empty_seqStack(s)) {
printf("The stack is empty!\n");
_getch();
return;
}
for (i = s->top; i >= 0; i--) {
printf("%d\t", s->data[i]);
}
printf("\nshow finished!\n");
_getch();
}
void free_seqStack(seqStack **s) {
if (*s == NULL) {
return;
}
free(*s);
*s = NULL;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!