队列的链式存储.docx

上传人:b****3 文档编号:10858223 上传时间:2023-05-28 格式:DOCX 页数:9 大小:44.84KB
下载 相关 举报
队列的链式存储.docx_第1页
第1页 / 共9页
队列的链式存储.docx_第2页
第2页 / 共9页
队列的链式存储.docx_第3页
第3页 / 共9页
队列的链式存储.docx_第4页
第4页 / 共9页
队列的链式存储.docx_第5页
第5页 / 共9页
队列的链式存储.docx_第6页
第6页 / 共9页
队列的链式存储.docx_第7页
第7页 / 共9页
队列的链式存储.docx_第8页
第8页 / 共9页
队列的链式存储.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

队列的链式存储.docx

《队列的链式存储.docx》由会员分享,可在线阅读,更多相关《队列的链式存储.docx(9页珍藏版)》请在冰点文库上搜索。

队列的链式存储.docx

队列的链式存储

队列的链式存储结构:

填写图片摘要(选填)

​存储结构:

链式存储队列

类:

填写图片摘要(选填)

函数:

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;

}

实现简单,仅供参考。

欢迎访问我的博客:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2