#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
struct SqStack {
int * pTop;
int * pBase;
int size;//存储当前已经分配的存储空间
};
void show();
bool InitStack(struct SqStack s);
bool DestroyStack(struct SqStack s);
bool ClearStack(struct SqStack s);
bool StackEmpty(struct SqStack s);
int StackLength(struct SqStack s);
int GetTop(struct SqStack s);
bool Push(struct SqStack s,int e);
int Pop(struct SqStack s);
bool StackTraverse(struct SqStack s);
bool InputElem (struct SqStack s );
int main () {
int i;
int init_num = 0;
struct SqStack stack;
show();
do{
printf("请选择:\n");
scanf("%d",&i);
switch (i) {
case 1 :
bool InitStack(struct SqStack s);
printf("初始化完成!");
init_num = 1;
break;
case 2 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
DestroyStack(stack);
init_num = 0;
}
break;
case 3 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 4 :
if(init_num == 0) {
printf("请先初始化!");
}
else{
}
break;
case 5 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 6 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 7 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
int elem;
printf("请输入一个元素:");
scanf("%d",&elem);
Push(stack,elem);
}
break;
case 8 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 9 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 10 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
InputElem(stack);
}
break;
case 11 :
if(init_num == 0) {
printf("请先初始化!");
}
break;
default:
break;
}
}
while(i != 11);
return 0;
}
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
struct SqStack {
int * pTop;
int * pBase;
int size;//存储当前已经分配的存储空间
};
void show();
bool InitStack(struct SqStack s);
bool DestroyStack(struct SqStack s);
bool ClearStack(struct SqStack s);
bool StackEmpty(struct SqStack s);
int StackLength(struct SqStack s);
int GetTop(struct SqStack s);
bool Push(struct SqStack s,int e);
int Pop(struct SqStack s);
bool StackTraverse(struct SqStack s);
bool InputElem (struct SqStack s );
int main () {
int i;
int init_num = 0;
struct SqStack stack;
show();
do{
printf("请选择:\n");
scanf("%d",&i);
switch (i) {
case 1 :
bool InitStack(struct SqStack s);
printf("初始化完成!");
init_num = 1;
break;
case 2 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
DestroyStack(stack);
init_num = 0;
}
break;
case 3 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 4 :
if(init_num == 0) {
printf("请先初始化!");
}
else{
}
break;
case 5 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 6 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 7 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
int elem;
printf("请输入一个元素:");
scanf("%d",&elem);
Push(stack,elem);
}
break;
case 8 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 9 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
}
break;
case 10 :
if(init_num == 0) {
printf("请先初始化!");
}
else {
InputElem(stack);
}
break;
case 11 :
if(init_num == 0) {
printf("请先初始化!");
}
break;
default:
break;
}
}
while(i != 11);
return 0;
}
void show(){
printf("************ 1.初始化为空栈 ************\n");
printf("************ 2.销毁栈 ************\n");
printf("************ 3.将栈置空 ************\n");
printf("************ 4.判断栈是否为空 ************\n");
printf("************ 5.返回栈的长度 ************\n");
printf("************ 6.求栈顶元素 ************\n");
printf("************ 7.插入元素,并使其成为栈顶元素 ************\n");
printf("************ 8.删除栈顶元素,并返回其值 ************\n");
printf("************ 9.输出栈内元素 ************\n");
printf("************ 10.创建并输入栈元素 ************\n");
printf("************ 11.退出 ************\n");
printf("************ 1.初始化为空栈 ************\n");
printf("************ 2.销毁栈 ************\n");
printf("************ 3.将栈置空 ************\n");
printf("************ 4.判断栈是否为空 ************\n");
printf("************ 5.返回栈的长度 ************\n");
printf("************ 6.求栈顶元素 ************\n");
printf("************ 7.插入元素,并使其成为栈顶元素 ************\n");
printf("************ 8.删除栈顶元素,并返回其值 ************\n");
printf("************ 9.输出栈内元素 ************\n");
printf("************ 10.创建并输入栈元素 ************\n");
printf("************ 11.退出 ************\n");
}
//初始化栈
bool InitStack(struct SqStack s){
s.pBase =(int *) malloc(STACK_INIT_SIZE *sizeof(STACKINCREMENT));
if(NULL == s.pBase) {
printf("内存分配失败!\n");
exit(-1);
}
s.pTop = s.pBase;
s.size = STACK_INIT_SIZE;
return true;
};
//销毁栈
bool DestroyStack(struct SqStack s) {
free(s.pBase);
s.pBase = NULL;
s.pTop = NULL;
s.size = 0;
return true;
}
//清空栈
bool ClearStack(struct SqStack s) {
s.pTop = s.pBase;
return true;
}
//初始化栈
bool InitStack(struct SqStack s){
s.pBase =(int *) malloc(STACK_INIT_SIZE *sizeof(STACKINCREMENT));
if(NULL == s.pBase) {
printf("内存分配失败!\n");
exit(-1);
}
s.pTop = s.pBase;
s.size = STACK_INIT_SIZE;
return true;
};
//销毁栈
bool DestroyStack(struct SqStack s) {
free(s.pBase);
s.pBase = NULL;
s.pTop = NULL;
s.size = 0;
return true;
}
//清空栈
bool ClearStack(struct SqStack s) {
s.pTop = s.pBase;
return true;
}
//判断栈是否为空
bool StackEmpty(struct SqStack s) {
if(s.pBase == s.pTop) {
return true;
} else {
return false;
}
}
bool StackEmpty(struct SqStack s) {
if(s.pBase == s.pTop) {
return true;
} else {
return false;
}
}
//返回栈的长度
int StackLength(struct SqStack s) {
return s.pTop - s.pBase;
}
int StackLength(struct SqStack s) {
return s.pTop - s.pBase;
}
//求栈顶元素
int GetTop(struct SqStack s) {
int e;
if(s.pTop == s.pBase)
{
return 0;
}
e = *(s.pTop - 1 );
return e;
int GetTop(struct SqStack s) {
int e;
if(s.pTop == s.pBase)
{
return 0;
}
e = *(s.pTop - 1 );
return e;
}
//压栈
bool Push(struct SqStack s,int e){
if(s.pTop - s.pBase >= s.size) {
s.pBase = (int *) realloc(s.pBase,(s.size + STACK_INIT_SIZE)*sizeof(int));
if(NULL == s.pBase) {
exit(-1);
}
s.pTop = s.pBase + s.size;
s.size += STACKINCREMENT;
bool Push(struct SqStack s,int e){
if(s.pTop - s.pBase >= s.size) {
s.pBase = (int *) realloc(s.pBase,(s.size + STACK_INIT_SIZE)*sizeof(int));
if(NULL == s.pBase) {
exit(-1);
}
s.pTop = s.pBase + s.size;
s.size += STACKINCREMENT;
}
*(s.pTop)++ = e;
return true;
}
*(s.pTop)++ = e;
return true;
}
//出栈
int Pop(struct SqStack s) {
if(s.pTop == s.pBase) {
return 0;
}
return *--s.pTop;
}
//遍历栈
bool StackTraverse(struct SqStack s) {
while(s.pTop > s.pBase) {
printf("%d",*s.pBase++);
}
printf("\n");
return true;
}
//创建栈并输入栈元素
bool InputElem (struct SqStack s ) {
int num;
int elem;
printf("请输入元素个数:");
scanf("%d",&num);
printf("请输入元素:");
for(int i = 0 ;i < num ; i++) {
scanf("%d",&elem);
printf("hhhh");
*(s.pTop)++ = elem;
}
return true;
}
bool StackTraverse(struct SqStack s) {
while(s.pTop > s.pBase) {
printf("%d",*s.pBase++);
}
printf("\n");
return true;
}
//创建栈并输入栈元素
bool InputElem (struct SqStack s ) {
int num;
int elem;
printf("请输入元素个数:");
scanf("%d",&num);
printf("请输入元素:");
for(int i = 0 ;i < num ; i++) {
scanf("%d",&elem);
printf("hhhh");
*(s.pTop)++ = elem;
}
return true;
}