栈数组实现

如果有什么错误请告知

最近刚学的数据结构.练习练习

​
#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);
}

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值