栈的顺序存储

# include <stdio.h>
# include <stdlib.h>
# define MAXSIZE 10
typedef struct node{
	int data[MAXSIZE];
	int top;
}SEQSTACK;

void init_stack(SEQSTACK *S)
{
	S->top = -1;
}

void stack_push(SEQSTACK *S,int n)
{
	if(S->top==MAXSIZE-1){
		printf("full stack!\n");
		return;
	}
	S->data[++(S->top)] = n;
}

int getTop_data(SEQSTACK *S)
{
	if(!isEmpty(S))
		return S->data[S->top];
	return;
}

int isEmpty(SEQSTACK *S)
{
	int isEmpty = S->top==-1?1:0;
	return isEmpty;
}
int pop_stack(SEQSTACK *S)
{
	if(isEmpty(S))
	{
		printf("empty stack!\n");
		return;
	}
	//	出栈先出栈顶
	int data = S->data[S->top];
	S->top--;
	return data; 	
}

void clear_stack(SEQSTACK *S)
{
	S->top = -1;
}

int stack_length(SEQSTACK *S)
{
	return S->top+1;
}
int main()
{
	SEQSTACK *s = (SEQSTACK*)malloc(sizeof(SEQSTACK));
	init_stack(s);
//	printf("%d",s->top);
	int i;
	for(i = 0;i<MAXSIZE;i++){
		stack_push(s,i);
	}
//	stack_push(s,1);
	int topData = getTop_data(s);
	printf("topdata:%d\n",topData);
	printf("length:%d\n",stack_length(s));
	int popData = pop_stack(s);
	printf("topdataNow:%d,popData:%d\n",getTop_data(s),popData);
}

虽然比较简单,但也还是记录下吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值