数据结构队列Queues.docx

上传人:b****7 文档编号:16654796 上传时间:2023-07-16 格式:DOCX 页数:5 大小:15.39KB
下载 相关 举报
数据结构队列Queues.docx_第1页
第1页 / 共5页
数据结构队列Queues.docx_第2页
第2页 / 共5页
数据结构队列Queues.docx_第3页
第3页 / 共5页
数据结构队列Queues.docx_第4页
第4页 / 共5页
数据结构队列Queues.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

数据结构队列Queues.docx

《数据结构队列Queues.docx》由会员分享,可在线阅读,更多相关《数据结构队列Queues.docx(5页珍藏版)》请在冰点文库上搜索。

数据结构队列Queues.docx

数据结构队列Queues

Queues

ImportantpointstorememberaboutaQueue:

Queue–FIFO–First-In-First-OutDS

AQueueisaDataStructurewhereyoucaninsertfromoneendcalled“REAR”anddeletefromtheotherendcalled“FRONT”.

ImplementationofaQueue:

a)Asanarray–staticDS

i)LinearQueueusesLineararray

ii)CircularQueueusesCirculararray

b)AsaLinkedList–dynamicDS

a)Implementationofaqueue:

Asanarray

WhentheQueueisempty:

Front=-1

Rear=-1

WhentheQueuehasonlyoneitem:

Front=0

Rear=0

WhentheQueuehasmorethanoneitem:

Front=0

Rear>0

1.QueueOverflow:

TryingtoInsertanelementinaqueuewhenthequeueisFULL.

Thisconditionshouldnotarise.Toavoidthiserrorcondition,alwayscheckforthepositionofREARbeforeinsertinganewvalueinthequeue.IfREAR=max-1(thelastindexpositionofthearray)---->thismeanstheREARhasreachedthemaximumsizeofthearrayandthereisnomorespaceforthenewdata.

2.QueueUnderflow:

Tryingtodeleteanitemfromanemptyqueue.

InordertoavoidthiserrorsituationalwayscheckforFront=-1beforetryingtodeleteanyvalue.

AlgorithmforinsertingdatainaLinearQueue(asanarray):

1.Ifrear=max-1

//maxisthemaximumsizeofthearrayandwecheckwithmax-1ifrearispointingtothelastpositioninthearray

Display“queuefull”

Exit

2.IfFront=Rear=-1//Qisempty

a.front=front+1

b.Gotostep3

3.Rear=Rear+1

//dataisinsertedattherear

4.Queue[rear]=newdata

Algorithmfordeletingdatafromalinearqueue(asanarray):

1.iffront=-1

//thereisnodatainthequeue

Display“queueempty”

Exit

2.iffront=rear

front=rear=-1

3.deletethevalueatqueue[front]

4.front=front+1

Note:

Alwaysremember:

InaQueue

ForeveryInsert:

Rear=Rear+1

AftereveryDelete:

Front=Front+1

Thevaluesare‘neverdecremented’inthecaseofaninsertoradeleteinaqueue

Implementationofaqueueasacircularqueue(CQ):

FortheemptyCQ:

Front=-1

Rear=-1

FortheCQwithonlyonedata:

Front=Rear

FortheCQtobefull:

Condition1:

Front=0andRear=max-1

OR

Condition2:

Front=rear+1

AlgorithmforinsertingdatainaQueue(asacircularqueue/array):

1.If(Front=0ANDRear=Max-1)ORfront=rear+1

display“queuefull”

exit

2.iffront=-1//Qisempty

front=0

rear=0

gotostep5

//insertthe1stdataintheCQ

3.ifrear=max-1

setrear=0

gotostep5

4.rear=rear+1

5.Queue[rear]=newdata

AlgorithmfordeletingdatainaQueue(asacircularqueue/array):

1.IfFront=-1//nodataintheQ

display“queueempty”

exit

2.Iffront=rear

//thereisonlyonedataintheCQueue

front=-1

rear=-1

exit

3.ifFront=max-1

//deletingthedatafromthelastindexpositionofthecirculararray

front=0

exit

4.front=front+1

ImplementationofaQUEUEasalinkedlist:

Ifthequeueisempty:

front=rear=null

Ifthereisonlyonenode:

front=rear

QueueOverflow:

ThisconditioncannotariseinaLinkedListasthereisnosizelimit.

Exception:

Memoryrunsout–thereisnoenoughspaceinthememorytostorethedata

QueueUnderflow:

toavoidthissituationalwayscheckfor‘Front=Null’(whichmeansthelistisempty)

AlgorithmforinsertinganewdatainaqueuewhichisaLinkedlist/LinkedQueue:

Thenewnodewillalwaysbeinsertedintheendofthelinkedlistasalastnode!

1.createnewnode

2.newnode.data=data

3.newnode.next=null

4.iffront=null//ifthequeueisempty

front=newnode

rear=newnode

5.rear.next=newnode

6.rear=newnode

 

AlgorithmfordeletingadatafromthequeueasaLinkedlist/LinkedQueue:

alwaysbeginsfromthe1stnodeofthelist

1.iffront=null//thequeueisempty

display“queueempty”

exit

2.iffront=rear//thereisonlyonenode

front=rear=null

exit

3.current=front

4.front=current.next

5.releasecurrent

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

当前位置:首页 > 经管营销 > 经济市场

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

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