用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c代码库

顺序栈 c数据结构

2012-08-28 作者: 神马举报

[c]代码库

/*顺序栈
*/

#define m 100
typedef struct
{
	int stack[m];
	int top;
} stackstru;
init ( stackstru *s ) /*装入栈*/
{
	s->top=0;
	return 1;
}
int push ( stackstru *s,int x ) /*入栈操作*/
{
	if ( s->top==m )
		printf ( "the stack is overflow!\n" );
	else
	{
		s->top=s->top+1;
		s->stack[s->top]=x;
	}
}
void display ( stackstru *s ) /*显示栈所有数据*/
{
	if ( s->top==0 )
		printf ( "the stack is empty!\n" );
	else
	{
		while ( s->top!=0 )
		{
			printf ( "%d->",s->stack[s->top] );
			s->top=s->top-1;
		}
	}
}
int pop ( stackstru *s ) /*出栈操作并返回被删除的那个记录*/
{
	int y;
	if ( s->top==0 )
		printf ( "the stack is empty!\n" );
	else
	{
		y=s->stack[s->top];
		s->top=s->top-1;
		return y;
	}
}

int gettop ( stackstru *s ) /*得到栈顶数*/
{
	int e;
	if ( s->top==0 )
		return 0;
	else
		e=s->stack[s->top];
	return e;
}
main ( stackstru *p ) //函数使用演示
{
	int n,i,k,h,x1,x2,select;
	printf ( "create a empty stack!\n" );
	init ( p );
	printf ( "input a stack length:\n" );
	scanf ( "%d",&n );
	for ( i=0; i<n; i++ )
	{
		printf ( "input a stack value:\n" );
		scanf ( "%d",&k );
		push ( p,k );
	}
	printf ( "select 1:display()\n" );
	printf ( "select 2:push()\n" );
	printf ( "select 3:pop()\n" );
	printf ( "select 4:gettop()\n" );
	printf ( "input a your select(1-4):\n" );
	scanf ( "%d",&select );
	switch ( select )
	{
	case 1:
	{
		display ( p );
		break;
	}
	case 2:
	{
		printf ( "input a push a value:\n" );
		scanf ( "%d",&h );
		push ( p,h );
		display ( p );
		break;
	}
	case 3:
	{
		x1=pop ( p );
		printf ( "x1->%d\n",x1 );
		display ( p );
		break;
	}
	case 4:
	{
		x2=gettop ( p );
		printf ( "x2->%d",x2 );
		break;
	}
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...