用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

顺序队列 c数据结构

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

[c]代码库

/*顺序队列
*/

#define maxsize 100
typedef struct
{
	int data[maxsize];
	int front;
	int rear;
} sqqueue;
int sqinit ( sqqueue *p ) //队列初始化
{
	p->front=0;
	p->rear=0;
	return 1;
}
int enqueue ( sqqueue *q, int e ) //入队
{
	if ( ( q->rear+1 ) %maxsize==q->front )
		return 0;
	else
		q->data[q->rear]=e;
	q->rear= ( q->rear+1 ) %maxsize;
	return 1;
}
int dequeue ( sqqueue *q ) //出队
{
	int e;
	if ( q->front==q->rear )
		return 0;
	e=q->data[q->front];
	q->front= ( q->front+1 ) %maxsize;
	return e;
}
int empty ( sqqueue *q )  //判空
{
	int v;
	if ( q->front==q->rear )
		v=1;
	else
		v=0;
	return v;
}
int gethead ( sqqueue *q )  //取得头元素
{
	int e;
	if ( q->front==q->rear )
		e=-1;
	else
		e=q->data[q->front];
	return e;
}
void display ( sqqueue *q ) //显示所有元素
{
	int s;
	s=q->front;
	printf ( "the sequeue is display:\n" );
	if ( q->front==q->rear )
		printf ( "the sequeue is empty!" );
	else
	{
		while ( s<q->rear )
		{
			printf ( "->%d", q->data[s] );
			s= ( s+1 ) %maxsize;
		}
		printf ( "\n" );
	}
}
main ( sqqueue *head )  //函数使用样例
{
	int n,i,m,x,y,select,xq;
	printf ( "create a empty sequeue\n" );
	sqinit ( head );
	printf ( "please input the sequeue length:\n" );
	scanf ( "%d",&n );
	for ( i=0; i<n; i++ )
	{
		printf ( "please input a sequeue value:\n" );
		scanf ( "%d",&m );
		enqueue ( head,m );
	}
	printf ( "head->rear:%d\n",head->rear );
	printf ( "head->front:%d\n",head->front );
	display ( head );
	printf ( "select 1 **** enqueue() \n" );
	printf ( "select 2 **** dequeue() \n" );
	printf ( "select 3 **** empty () \n" );
	printf ( "select 4 **** gethead() \n" );
	printf ( "select 5 **** display() \n" );
	printf ( "please select (1--5):" );
	scanf ( "%d",&select );
	switch ( select )
	{
	case 1:
	{
		printf ( "please input a value :\n " );
		scanf ( "%d",&x );
		enqueue ( head,x );
		display ( head );
		break;
	}
	case 2:
	{
		dequeue ( head );
		display ( head );
		break;
	}
	case 3:
	{
		if ( empty ( head ) )
			printf ( "the sequeue is empty" );
		else
			printf ( "the sequeue is full" );
	}
	case 4:
	{
		y=gethead ( head );
		printf ( "output head value:%d\n",y );
		break;
	}
	case 5:
	{
		display ( head );
		break;
	}
	}
}
}


网友评论    (发表评论)


发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...