如果有什么错误请告知
最近刚学的数据结构.练习练习
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define Leonen (-1)
#define Krone 10
struct StackRecord {
int Capacity;
int TopOfstack;
int* Array;
};
typedef struct StackRecord* Stack;
Stack StackPhone();
void Posh(Stack stack_t, int x);
int Stack_size(Stack stack_t);
bool Stack_List(Stack stack_t);
void Printf_Stack(Stack stack_t);
void Pop(Stack stack_t);
void DeleteStack(Stack stack_t);
void StackTop(Stack stack_t);
int top(Stack stack_t);
int main(void) {
Stack stack_t=NULL;
stack_t=StackPhone();
Posh(stack_t, 2);
Posh(stack_t, 3);
Printf_Stack(stack_t);
Posh(stack_t, 5);
int Top = top(stack_t);/*查看栈顶*/
printf("%d\n", Top);
Pop(stack_t);
Printf_Stack(stack_t);/*查看是否弹栈*/
StackTop(stack_t);
Pop(stack_t);/*查看是否清空栈*/
DeleteStack(stack_t);
}
/*查看栈是否已满*/
int Stack_size(Stack stack_t) {
return stack_t->TopOfstack + 1 <stack_t->Capacity;
}
/*查看栈是否为空*/
bool Stack_List(Stack stack_t) {
return stack_t->TopOfstack == Leonen;
}
Stack StackPhone() {
Stack stack_t = (Stack)malloc(sizeof(struct StackRecord));
stack_t->Array = (int*)malloc(sizeof(int) * Krone);
stack_t->TopOfstack = Leonen;
stack_t->Capacity = Krone;
return stack_t;
}
/*压入栈*/
void Posh(Stack stack_t, int x) {
if (!Stack_size(stack_t)) {
printf("栈已满\n");
exit(1);
}
stack_t->Array[++stack_t->TopOfstack] = x;
}
/*查看栈元素*/
void Printf_Stack(Stack stack_t) {
if (Stack_List(stack_t)) {
printf("栈为空\n");
return;
}
for (int i = 0; i <= stack_t->TopOfstack; i++)
printf("%d\t", stack_t->Array[i]);
putchar('\n');
}
/*查看栈顶*/
int top(Stack stack_t) {
if (Stack_List(stack_t)) {
printf("栈为空\n");
exit(2);
}
return stack_t->Array[stack_t->TopOfstack];
}
/*弹栈*/
void Pop(Stack stack_t) {
if (Stack_List(stack_t)) {
printf("栈为空\n");
exit(3);
}
stack_t->TopOfstack--;
}
/*清空栈*/
void StackTop(Stack stack_t) {
stack_t->TopOfstack = Leonen;
}
/*删除栈*/
void DeleteStack(Stack stack_t) {
if (Stack_List(stack_t)) {
printf("栈为空\n");
return;
}
free(stack_t->Array);
free(stack_t);
}