Vous êtes sur la page 1sur 9

Queue

Logically a queue is a FIFO structure and physically it can be implemented either as an array or as a linked list. Insertions take place at the REAR end and deletions at the FRONT end.

Queue As An Array
When a queue is created as an array, its number of elements is declared before processing. The beginning of the array becomes its front end and the end of the array becomes its rear end. FR !T" stores the inde# of the first element in the queue and R$%R" stores the inde# of the last element in the queue. %t any gi&en time, the no. of elements in a queue ' rear(front)*

Algorithm for the insertion in a queue // This algorithms inserts a new element ITEM at the REAR end of // the Queue[N whi!h has si"e#N $% '% .% 0% 3% 5% 6% 8% 9% if rear#N&$ then (rint )queue full*o+erflow,,else if rear # &$ then / rear#front#12 queue[1 # item 4 else / rear#rear 7$ queue[rear #item 4 EN:

Algorithm for deleting an item in an array queue ; linear queue<

$% '% .% 0% 3% 5% 6% 8% 9% $1%

if front #&$ then (rint )queue em(ty* underflow,,else / item # queue[front (rint item if front#rear then // only one element front#rear#&$ else front#front 7$ end

Q% =rite a menu dri+en (rogram to insert and delete > dis(lay elements from an array & queue

++pgm to implement queue using array

,include -conio.h. ,include -iostream.h. const int si/e '012 int front'(*,rear'(*2 int main34 5 int ch2 char choice2

int queue6si/e72 &oid insert3int queue6742 &oid remo&e3int queue6742 &oid display3int queue6742 do 5 clrscr342 cout--89n9n9t9t((((((((((:$!;((((((((((82 cout--89n*(((.insert82 cout--89n<(((.remo&e82 cout--89n=(((.display82 cout--89n9n$nter your choice > 82 cin..ch2 s?itch3ch4 5 case * > insert3queue42 break2 case < > remo&e3queue42 break2 case => display3queue42 break2 default> cout--89n9nWrong choice82 @ cout--89n9nAo u ?ant to continue > 82 cin..choice2 @?hile 3choice''ByB CCchoice '' BDB42 getch342 return 12 @ &oid insert3int queue674 5 int no2 if 3rear''si/e(*4 cout--89n &er flo?82 else

5 cout--8$nter the number> 82 cin..no2 if 3rear''(*4 front'rear'12 else rear))2 queue6rear7'no2 @ @

&oid remo&e3int queue674 5 if3front''(*4 cout--89n;nder flo?82 else 5 cout--8$lement deleted is 8--queue6front72 if 3front''rear4 front'rear'(*2 else front))2 @ @ &oid display3int queue674 5 if 3front''(*4 cout--89n9nEueue is emptyFFF82 else 5 cout--89n9nThe content of queue is ...8--endl2 for3int i'front2i-'rear 2i))4 cout--queue6i7--8 82 @

?in@ed queue
Linked queues are the queues ha&ing links among its elements.T?o pointers are maintained to store the FR !T" position and the R$%R" position.
FR !T R$%R

Insertion in a lin@ed queue


Insertion in a linked queue takes place only at rear end and rear gets modified ?ith e&ery insert.

Algorithm *. ne?ptr ' ne? node <. ne?ptr(.info ' item =. ne?ptr(.link ' !;LL G. if rear ' !;LL then 0. front'rear'ne?ptr else 5 H. rear(.link ' ne?ptr I. rear' ne?ptr @ J. end ++allocate space for the item

:eletion from a lin@ed queue


Aeletion takes place at the front end and front" gets modified ?ith e&ery deletion Algorithm *. If front'!;LL then <. Krint LEueue emptyM else 5 =. item 'front(.info G. print item ++ display the item deleted 0. if front'rear then H. front'rear'!;LL else I. front'front (.link @ J. end

Q%=rite a (rogram to insert*delete*and dis(lay elements of a lin@ed queue%

++ queue implemented using linked list ,include -conio.h. ,include -iostream.h. struct ! A$ 5 int num 2 ! A$ NLink 2 @Nfront,Nrear2 int main34 5 int ch2

char choice2 front'rear'!;LL2 &oid insert342 &oid remo&e342 &oid display342 do 5 clrscr342 cout--89n9n9t9t((((((((((:$!;((((((((((82 cout--89n*(((.insert82 cout--89n<(((.remo&e82 cout--89n=(((.display82 cout--89n9n$nter your choice > 82 cin..ch2 s?itch3ch4 5 case * > insert342 break2 case < > remo&e342 break2 case => display342 break2 default> cout--89n9nWrong choice82 @ cout--89n9nAo u ?ant to continue > 82 cin..choice2 @?hile 3choice''ByB CCchoice '' BDB42 getch342 return 12 @ &oid insert34 5 ! A$N!e?KTR2 !e?KTR' ne? ! A$2 if3!e?KTR''!;LL4 cout--8 &erflo? F82 else 5

cout--8$nter a number >82 cin..!e?KTR(.num2 !e?KTR(.Link'!;LL2 if3rear''!;LL4 front'rear'!e?KTR2 else 5 rear(.Link ' !e?KTR2 rear ' !e?KTR2 @ @ @

&oid remo&e34 5 ! A$NTemp2 if3front''!;LL4 cout--8;nderflo? , queue is $mpty F 82 else 5 Temp'front2 if 3front''rear4 front'rear'!;LL2 else front'Temp(.Link2 cout--8The deleted element ' 8 --Temp(.num--endl2 delete Temp 2 @ @ &oid display34 5 ! A$ Nptr2 ptr'front2 if 3ptr''!;LL4

cout--89n The queue is empty F 9n82 else 5 cout--89n The numbers stored in Eueue are >9n82 ?hile3ptrF'!;LL4 5 cout--ptr(.num--8 82 ptr'ptr(.Link2 @ @ @

Vous aimerez peut-être aussi