码迷,mamicode.com
首页 > 其他好文 > 详细

【数据结构】顺序栈

时间:2015-02-10 15:22:12      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:c语言   数据结构   struct   算法   

#include <STDIO.H>
#include <STRING.H>
#include <STDLIB.H>
typedef struct SeqStack
{
	int length;
	int top;
	char *data;
}seqstack;

seqstack* CreatStack(seqstack *s,int n)
{
	s=(seqstack *)malloc(sizeof(seqstack)+n*sizeof(char));
	if(s==NULL) return NULL;
	memset(s,0,sizeof(seqstack)+n*sizeof(char));
	s->length=n;
	s->top=0;
	s->data=(char *)(s+1);
	return s;
}

int StackEmpty(seqstack *s)
{
	if(s->top==0)
		return 1;
	else 
		return 0;
}

int GetTop(seqstack *s,char *e)
{
	if(s->top==0)
		return -1;
	*e=s->data[(s->top)-1];
	return 0;
}

int PushStack(seqstack *s,char e)
{
	if(s->top==s->length)
		return -1;
	s->data[s->top]=e;
	s->top+=1;
	return 0;
}

int PopStack(seqstack *s,char *e)
{
	*e=s->data[s->top-1];
		s->top-=1;
	return 0;
}

int StackLength(seqstack *s)
{
	return s->top;
}

void InitStack(seqstack *s)
{
	s->top=0;
}

void destroyStack(seqstack *s)
{
	free(s);
}


int main()
{
	seqstack *s=NULL;
	int i;
	char e=65;
	s=CreatStack(s,10);
	printf("%d\n",StackEmpty(s));
	for(i=0;i<s->length;i++)
	{
		PushStack(s,65+i);
		GetTop(s,&e);
		printf("%c,",e);
	}
	printf("\n");	
	printf("%d\n",StackEmpty(s));
	for(i=0;i<s->length;i++)
	{
		PopStack(s,&e);
		printf("%c,",e);
	}
	printf("\n");
	printf("%d\n",StackEmpty(s));
	destroyStack(s);
	return 0;
}

【数据结构】顺序栈

标签:c语言   数据结构   struct   算法   

原文地址:http://blog.csdn.net/a781558066/article/details/43702889

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!