数据结构(十) 链队列的基本操作 ----入队列,出队列,判断队列是否为空

时间:2021-05-14 10:24:51
//链队列的基本操作
 
#include <iostream>

using namespace std;

//节点的结构体
struct Node
{
	int data;
	struct Node *next;
};
//队列的结构体
struct queue
{
	struct Node * front;
	struct Node *rear;	
}; 
//队列的初始化操作
void initQueue(struct queue &Q)
{
	Q.front = new Node();
	Q.rear = Q.front;
	Q.front->next = NULL;
}
//队列的入队操作
void enQueue(struct queue &Q)
{
	struct Node *p;
	int data;
	cout<<"请输入入队列的数据:\n";
	cin>>data;
	p = new Node();
	p->data = data;
	p->next = NULL;
	Q.rear->next= p;
	Q.rear=p;
} 
//队列的出队操作
void DeQueue(struct queue &Q)
{
	struct Node *p;
	p = Q.front->next;
	cout<<"队列的数据为:\n";
	cout<<p->data;
	if(p==Q.rear)
	{
		Q.front=Q.rear;
	}
	else
	{
		Q.front->next = p->next;	
	}
	delete(p);
} 
//判断队列是否为空
void isEmpty(struct queue Q)
{
	if(Q.front==Q.rear)
	{
		cout<<"对列为空\n";
		exit(1);
	}
} 
int main()
{
	struct queue Q;
	initQueue(Q);
	enQueue(Q);
	DeQueue(Q);	
	isEmpty(Q);	
	return 0;
}