用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

队列基本操作(队列初始化、清空队列、入队、出队等)

2012-09-22 作者: 神马举报

[c++]代码库

#include<iostream.h>
#include<stdlib.h>
const int maxsize=50;
typedef int elemtype;
struct queue
{
	elemtype queue[maxsize];
	int front,rear;
};

void initqueue ( queue &q )
{
	q.front=q.rear=0;
}

void clearqueue ( queue &q )
{
	q.front=q.rear=0;
}
int queueempty ( queue &q )
{
	return q.front==q.rear;
}

elemtype qfront ( queue &q )
{
	if ( q.front==q.rear )
	{
		cerr<<"queue is empty!"<<endl;
		exit ( 1 );
	}
	return q.queue[ ( q.front+1 ) %maxsize];
}

void qinsert ( queue &q,elemtype x )
{
	int k= ( q.rear+1 ) %maxsize;
	if ( k==q.front )
	{
		cerr<<"queue overflowe!"<<endl;
		exit ( 1 );
	}
	q.rear=k;
	q.queue[k]=x;
}

elemtype qdelete ( queue &q )
{
	if ( q.front==q.rear )
	{
		cerr<<"queue is empty!"<<endl;
		exit ( 1 );
	}
	q.front= ( q.front+1 ) %maxsize;
	return q.queue[q.front];
}

int queuefull ( queue &q )
{
	return ( q.rear+1 ) %maxsize==q.front;
}

int queuesize ( queue &q )
{
	return ( q.rear-q.front ) %maxsize;
}

void lookqueue ( queue &q )
{
	if ( q.front==q.rear )
	{
		cerr<<"queue is empty!"<<endl;
		exit ( 1 );
	}
	int k= ( q.front+1 ) %maxsize;
	while ( 1 )
	{
		cout<<q.queue[k]<<"  ";
		if ( k==q.rear )
			break;
		k= ( k+1 ) %maxsize;
	}
	cout<<endl;
}

void main()
{
	queue q;
	initqueue ( q );
	for ( int i=0; i<6; i++ )
	{
		int x=rand() %100;
		int y=rand() %100;
		if ( !queuefull ( q ) )
		{
			qinsert ( q,x );
			cout<<x<<"  in queue,  " ;
		}
		if ( !queuefull ( q ) )
		{
			qinsert ( q,y );
			cout<<y<<"  in queue" ;
		}
		cout<<endl;
		cout<<"queuesize is:  "<<queuesize ( q ) <<endl;
		cout<<qdelete ( q ) <<"  out queue"<<endl;
		cout<<"queuesize is:  "
		    <<queuesize ( q ) <<endl;
	}
	cout<<endl;
	lookqueue ( q );
	while ( !queueempty ( q ) )
	{
		cout<<qdelete ( q ) <<endl;
		cout<<"queuesize is:  "
		    <<queuesize ( q ) <<endl;
	}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...