闲来没事,自己使用链表实现的队列
有不足的地方还请指正
#pragma once
#include<stdlib.h>
struct _QUEUE
{
struct _QUEUE *pf;
struct _QUEUE *pb;
int Date;
};
class QUEUE
{
private:
_QUEUE *Head,*Button,*CpyButton,*Next,*Ahead,*Centrol;
public:
QUEUE(int Num = 15)
{
for (int i = 0; i < Num+1; ++i)
{
Next = (struct _QUEUE*)malloc(sizeof(struct _QUEUE));
if (i == 0)
{
Head = Ahead = Next;
//Head->pf = nullptr;
}
else
{
Ahead->pb = Next;
Next->pf = Ahead;
Ahead = Next;
}
CpyButton=Centrol = Button = Next;
}
}
void Push(int Num); //Push a number into the QUEUE
int PopBotton(); //Get a number from the Button of QUEUE
bool IsEmpty(); //To judge whether the QUEUE is empty .If so,return true,else,return false.
};
void QUEUE::Push(int Num) //Push a number into the QUEUE
{
if (Centrol != Head)
{
Centrol->Date = Num;
Centrol = Centrol->pf;
}
}
int QUEUE::PopBotton() //Get a number from the Button of QUEUE
{
Centrol = Centrol->pb;
int ButtonNumber = Button->Date;
while (true)
{
if (CpyButton == Centrol)
{
CpyButton->Date = 0;
CpyButton = Button;
break;
}
else
{
CpyButton->Date = CpyButton->pf->Date;
CpyButton = CpyButton->pf;
} }
return ButtonNumber;
}
bool QUEUE::IsEmpty() //To judge whether the QUEUE is empty .If so,return true,else,return false.
{
if (Centrol == Button || Centrol != Head)
return true;
else
return false;
}