【数据结构作业】队列

1 篇文章 0 订阅
1 篇文章 0 订阅
#include <iostream>
#include <queue> 
using namespace std;

class LinkQueue;				//连队列前视说明

class LinkQueueNode
{
	friend class LinkQueue;		
public:
	LinkQueueNode(int &e, LinkQueueNode *p = NULL):elem(e), next(p){};			//节点类的构造函数
private:
	int elem;									//数据域
	LinkQueueNode *next;					//指针域	
};

class LinkQueue
{
public:
	LinkQueue():front(NULL), rear(NULL){};			//构造一个空队列
	~LinkQueue();								//析构函数,销毁队列
	int IsEmpty() const 						//判断是否为空
	{
		return front == NULL;	
	}
	void LinkQueueClear();						//请空链队列
	int LinkQueueLength() const;				//求链队列长度
	int Getfront();								//返回链头元素值
	void InQueue(int &e);						//入队
	int OutQueue();								//出队,并返回值
private:
	LinkQueueNode *front, *rear;				//定义队头队尾指针
};

LinkQueue::~LinkQueue()			
{
	LinkQueueNode *p;
	while(front != NULL)			
	{
		p = front;
		front = front->next;
		delete p;
	}
}

int LinkQueue::LinkQueueLength() const
{
	LinkQueueNode *p = front;
	int i = 0;
	while(p)
	{
		i++;
		p = p->next;
	}
	return i;
}

int LinkQueue::Getfront()
{
	if(IsEmpty())
	{
		cout << "The Queue is Empty!" << endl;
		exit(0);									
	}
	else
		return front->elem;
}

void LinkQueue::InQueue(int &e)
{
	if(front == NULL)
		front = rear = new LinkQueueNode (e, NULL);			
	else
		rear = rear->next = new LinkQueueNode(e, NULL);
}

int LinkQueue::OutQueue()
{
	if(IsEmpty())
	{
		cout << "The Queue is Empty!" << endl;
		exit(0);					
	}
	LinkQueueNode *p = front;
	int e = p->elem;
	front = front->next;
	if(front == NULL)
		rear = NULL;
		delete p;
		return e;
}

int main()
{
	int n, m;
	cin >> n >> m;
	LinkQueue Q; 
	for(int i = 0; i < n; i++)
	{
		Q.InQueue(i);
	}
	for (int j = 0; j < m; j++)
	{
		Q.OutQueue(); 
	} 
	int a = Q.Getfront();
	int b = Q.LinkQueueLength();
	cout << a << " " << b << endl;
	Q.~LinkQueue();
	return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值