#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;
}
【数据结构作业】队列
最新推荐文章于 2022-08-15 23:15:26 发布