[数据结构] 队列的指针实现

#include <iostream>
using namespace std;

typedef int element_type;

struct celltype
{
	element_type element;
	struct celltype *next;
};

struct QUEUE
{
	celltype *front;
	celltype *rear;
};

void MakeNull(QUEUE *Q)
{
	Q->front = new celltype;//建立表头结点 
	Q->front->next = NULL;
	Q->rear = Q->front;//表头既是第一个结点也是最后一个结点 
}/*MakeNull*/

bool Empty(QUEUE *Q)
{
	if(Q->front == Q->rear)
	  return true;
    else 
      return false;
}//Empty

void EnQueue(element_type x,QUEUE *Q)//将元素x插入队列Q的后端 
{
    Q->rear->next = new celltype;//在队列的后端增加一个新节点
	Q->rear = Q->rear->next;//将新节点作为表尾
	Q->rear->element = x;
	Q->rear->next = NULL;	
}/*EnQueue*/

void DeQueue(QUEUE *Q)//删除队列Q的第一个元素 
{
    celltype *tmp;
	if( Empty(Q) )
	  cout << "Queue is empty!" << endl;
	else
	{
		/*也可以 
		tmp = Q->front->next;
		Q->front->next = tmp->next;
		delete tmp;
		*/
		Q->front->next = Q->front->next->next;
		if(Q->front->next == NULL)
		  Q->rear = Q->front;
	} 
} 

element_type Front(QUEUE *Q)//返回队列的第一个元素 
{
	if(Q->front->next)
	  return (Q->front->next->element);
    else
      cout << "Queue is empty!" << endl;
}

int main()
{
	QUEUE *Q;
	int m,n;
	MakeNull(Q);
	EnQueue(1,Q);
	EnQueue(2,Q);
	m = Front(Q);
	cout << m << endl;
	DeQueue(Q);
	n = Front(Q);
	cout << n << endl;
		
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值