队列的链式存储.docx
《队列的链式存储.docx》由会员分享,可在线阅读,更多相关《队列的链式存储.docx(9页珍藏版)》请在冰点文库上搜索。
队列的链式存储
队列的链式存储结构:
填写图片摘要(选填)
存储结构:
链式存储队列
类:
填写图片摘要(选填)
函数:
template
MyQueue:
:
MyQueue()
{
head=newnode;
rear=newnode;
length=0;
}
template
MyQueue:
:
~MyQueue()
{
deletehead;
deleterear;
}
template
voidMyQueue:
:
MakeNull()
{
head->next=NULL;
head=rear; //表头是第一个节点,也是第二个节点
}
template
boolMyQueue:
:
IsEmpty()
{
if(head==rear)
returntrue;
elsereturnfalse;
}
template
intMyQueue:
:
QueueLength()
{
returnlength;
}
template
voidMyQueue:
:
EnQueue(Tt)
{
rear->next=newnode;
rear=rear->next;
rear->data=t;
rear->next=NULL;
length++;
}
template
voidMyQueue:
:
DeQueue()
{
node*temp=newnode;
if(IsEmpty()) cout<<"Queueisempty!
"<else{
temp=head->next;
head->next=temp->next;
deletetemp;
length--;
if(head->next==NULL)
rear=head;
}
}
template
voidMyQueue:
:
Displayq()
{
node*p=head;
while(p&&p->next)
{
p=p->next;
cout<data<<"";
}
cout<可运行程序:
#include
usingnamespacestd;
template
structnode{
Tdata;
node*next;
};
template
classMyQueue{
public:
MyQueue();
~MyQueue();
void MakeNull(); //置成空队
boolIsEmpty(); //判断空队
intQueueLength(); //返回队列的长度
voidEnQueue(T); //入队
voidDeQueue(); //出队
voidDisplayq(); //输出队
private:
node*head,*rear;
intlength;
};
template
MyQueue:
:
MyQueue()
{
head=newnode;
rear=newnode;
length=0;
}
template
MyQueue:
:
~MyQueue()
{
deletehead;
deleterear;
}
template
voidMyQueue:
:
MakeNull()
{
head->next=NULL;
head=rear; //表头是第一个节点,也是第二个节点
}
template
boolMyQueue:
:
IsEmpty()
{
if(head==rear)
returntrue;
elsereturnfalse;
}
template
intMyQueue:
:
QueueLength()
{
returnlength;
}
template
voidMyQueue:
:
EnQueue(Tt)
{
rear->next=newnode;
rear=rear->next;
rear->data=t;
rear->next=NULL;
length++;
}
template
voidMyQueue:
:
DeQueue()
{
node*temp=newnode;
if(IsEmpty()) cout<<"Queueisempty!
"<else{
temp=head->next;
head->next=temp->next;
deletetemp;
length--;
if(head->next==NULL)
rear=head;
}
}
template
voidMyQueue:
:
Displayq()
{
node*p=head;
while(p&&p->next)
{
p=p->next;
cout<data<<"";
}
cout<}
intmain()
{
MyQueuemyqueue;
myqueue.MakeNull();
inti;
for(inti=0;i<30;i++)
myqueue.EnQueue(i);
myqueue.Displayq();
cout<<"当前队列的长度:
"<cout<<"i入队:
";
cin>>i;
myqueue.EnQueue(i);
myqueue.Displayq();
cout<<"当前队列的长度:
"<cout<<"出队:
";
myqueue.DeQueue();
myqueue.Displayq();
cout<<"当前队列的长度:
"<system("pause");
return0;
}
实现简单,仅供参考。
欢迎访问我的博客: