用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

链栈 c数据结构

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

[c]代码库

/*链栈
*/

# define null 0

typedef struct stacknode
{
	int data;
	struct stacknode *next;
} stacklink;
typedef struct
{
	stacklink *top;
	int stacksize;
} stackk;

initlink ( stackk *s )
{
	s->top= ( stacklink * ) malloc ( sizeof ( stacklink ) );
	s->top->data=0;
	s->top->next=null;
}

int poplink ( stackk *s )
{
	stackk *p;
	int v;
	if ( s->top->next==null ) printf ( "the stackis empty\n" );
	else
	{
		v=s->top->next->data;
		p=s->top->next;
		s->top=s->top->next;
	}
	free ( p );
	return v;
}
}
int pushlink ( stackk *s,int x )
{
	stackk *p;
	p= ( stacklink * ) malloc ( sizeof ( stacklink ) );
	p->data=x;
	p->next=s->top->next;
	s->top->next=p;
}
int gettop ( stackk *s )
{
	int e;
	if ( s==null ) printf ( "the stack is empty!\n" );
	e=s->top->next->data;
	return e;
}

display ( stackk *s )
{
	stackk *p;
	p=s->top->next;
	printf ( "display the stacklink:\n" );
	if ( s->top=null ) printf ( "the stacklink is empty!\n" );
	else
	{
		while ( p )
		{
			printf ( "->%d",p->data );
			p=p->next;
		}
	}
}

main ( stacklink *p )
{
	int n,k,i,select,h,x1,x2;
	printf ( "create a empty stacklink!\n" );
	initlink ( p );
	printf ( "input a stacklink length:\n" );
	scanf ( "%d",&n );
	for ( i=1; i<=n; i++ )
	{
		printf ( "input a stacklink value:\n" );
		scanf ( "%d",&k );
		pushlink ( p,k );
	}
	printf ( "select 1:display()\n" );
	printf ( "select 2:pushlink()\n" );
	printf ( "select 3:poplink()\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 );
		pushlink ( p,h );
		display ( p );
		break;
	}
	case 3:
	{
		x1=poplink ( 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、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

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

加载中,请稍后...