1. 程式人生 > >C棧的基本操作

C棧的基本操作

seqstack.c部分:
#include"seqstack.h"

seqstack_t *create_seqstack(void)   //建立
{
	seqstack_t *s = (seqstack_t *)malloc(sizeof(seqstack_t));

	s->top = -1;
	return s;
}

int is_full_seqstack(seqstack_t *s)  //判滿
{
	return s->top == N - 1;
}

int is_empty_seqstack(seqstack_t *s)   //判空
{
	return s->top == -1;
}

int push_seqstack(seqstack_t *s,int x)   //入棧
{
	if(is_full_seqstack(s))
	{
		puts("s is full");
		exit(1);
	}
	s->data[s->top + 1] = x;
	s->top++;
	//s->data[++(s->top)] = x;
	return 1;
}

int pop_seqstack(seqstack_t *s)   //出棧
{
	if(is_empty_seqstack(s))
	{
		puts("s is empty");
		exit(1);
	}
	//return s->data[s->top--];
	int ret = s->data[s->top];
	s->top--;
	return ret;
}

void show(seqstack_t *s)   //遍歷
{
	int i = s->top;

	while(1)
	{
		if(i < 0)
		{
			break;
		}
		printf("%d\n",s->data[i]);
		i--;
	}
}