0% found this document useful (0 votes)
39 views

CSC 2105: Data Structure Queue: Mushfiqur Rahman

This document discusses queues as a data structure. A queue is a first-in, first-out (FIFO) structure where elements can only be added to the rear and removed from the front. Basic queue operations include checking if it is empty or full, adding elements to the rear (enqueue), removing elements from the front (dequeue), retrieving the front element, and printing all elements. The document provides examples of implementing these operations on a queue and manipulating elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

CSC 2105: Data Structure Queue: Mushfiqur Rahman

This document discusses queues as a data structure. A queue is a first-in, first-out (FIFO) structure where elements can only be added to the rear and removed from the front. Basic queue operations include checking if it is empty or full, adding elements to the rear (enqueue), removing elements from the front (dequeue), retrieving the front element, and printing all elements. The document provides examples of implementing these operations on a queue and manipulating elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CSC 2105: DATA STRUCTURE

QUEUE

Mushfiqur Rahman
Lecturer, Department of Computer Science
Faculty of Science & Information Technology
American International University-Bangladesh (AIUB)
[email protected]
QUEUE
 A queue is a waiting line – seen in daily life
 A line of people waiting for a bank teller
 A line of cars at a toll both
 Queue data structure is like a container with both end open.
 An end called rear
 Another end is front
 This mechanism is called First-In-First-Out (FIFO).
 Some of the applications are :
 Device queue, printer queue, keystroke queue, etc.

Mushfiqur Rahman CSC 2015: Data Structures Queue  2


QUEUE IN COMPUTER LANGUAGE
front
 A queue is a sequence of data elements
1 4 3
 In the sequence
 Items can be added only at the rear rear

 Items can be removed only at the other end, front


 Basic operations
 Check IsEmpty
 Check IsFull
 EnQueue (add element to back i.e. at the rear)
 DeQueue (remove element from the front)
 FrontValue (retrieve value of element from front)
 ShowQueue (print all the values of queue from front to rear)
Mushfiqur Rahman CSC 2015: Data Structures Queue  3
 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull
rear
 EnQueue (add element to back i.e. at
the rear)
 DeQueue (remove element from the
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  4


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue 3
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at
the rear)
 DeQueue (remove element from the
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  5


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue 3 6
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  6


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue 3 6 2
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  7


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue 3 6 2 5
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  8


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue 3 6 2 5
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  9


front
 Queue[4],MaxSize=4;

QUEUE - 0 1 2 3 4

OPERATION Queue 6 2 5
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)  DeQueue()  3

Mushfiqur Rahman CSC 2015: Data Structures Queue  10


front
 Queue[4],MaxSize=4;

QUEUE - 0 1 2 3 4

OPERATION Queue 2 5
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)  DeQueue()  3
 DeQueue()  6

Mushfiqur Rahman CSC 2015: Data Structures Queue  11


front
 Queue[4],MaxSize=4;

QUEUE - 0 1 2 3 4

OPERATION Queue 5
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0 rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)  DeQueue()  3
 DeQueue()  6
 DeQueue()  2

Mushfiqur Rahman CSC 2015: Data Structures Queue  12


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0
rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)  DeQueue()  3
 DeQueue()  6
 DeQueue()  2
 DeQueue()  5, front=rear=-1

Mushfiqur Rahman CSC 2015: Data Structures Queue  13


 Queue[4],MaxSize=4; front

QUEUE - 0 1 2 3 4

OPERATION Queue
 Check IsEmpty
 Initialize( )  front=rear=-1
 Check IsFull  EnQueue( 3 )  front=rear=0
rear
 EnQueue (add element to back i.e. at  EnQueue( 6 )
the rear)
 DeQueue (remove element from the  EnQueue( 2 )
front)
 EnQueue( 5 )
 FrontValue (retrieve value of element
from front)  EnQueue( 9 )  Queue Full, (rear==(MaxSize-1))
 ShowQueue (print all the values of
queue from front to rear)  DeQueue()  3
 DeQueue()  6
 DeQueue()  2
 DeQueue()  5, front=rear=-1
 DeQueue()  Queue Empty, (front==-1) && (rear==-1)
Mushfiqur Rahman CSC 2015: Data Structures Queue  14
 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 DeQueue (remove element from the
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  15


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 3
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  16


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 3 6
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = (rear + 1)%MaxSize;
front)
 FrontValue (retrieve value of element
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  17


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 3 6 2
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = (rear + 1)%MaxSize;
from front)
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  18


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 6 2
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = (front+1)%MaxSize;
 ShowQueue (print all the values of
queue from front to rear)

Mushfiqur Rahman CSC 2015: Data Structures Queue  19


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 2
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = 1;
 ShowQueue (print all the values of
queue from front to rear)  DeQueue( )  6, front = (front+1)%MaxSize;

Mushfiqur Rahman CSC 2015: Data Structures Queue  20


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 2 5
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = 1;
 ShowQueue (print all the values of
queue from front to rear)  DeQueue( )  6, front = 2;
 EnQueue( 5 )  rear = (rear + 1)%MaxSize;

Mushfiqur Rahman CSC 2015: Data Structures Queue  21


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 9 2 5
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = 1;
 ShowQueue (print all the values of
queue from front to rear)  DeQueue( )  6, front = 2;
 EnQueue( 5 )  rear = 3;
 EnQueue( 9 )  rear = (rear + 1)%MaxSize;

Mushfiqur Rahman CSC 2015: Data Structures Queue  22


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 9 5
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = 1;
 ShowQueue (print all the values of
queue from front to rear)  DeQueue( )  6, front = 2;
 EnQueue( 5 )  rear = 3;
 EnQueue( 9 )  rear = 0;
 DeQueue( )  2, front = (front+1)%MaxSize;

Mushfiqur Rahman CSC 2015: Data Structures Queue  23


 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue 9
 Check IsEmpty
 Check IsFull  Initialize( )  front=rear=-1;
rear
 EnQueue (add element to back i.e. at
the rear)
 EnQueue( 3 )  front=rear=0;
 DeQueue (remove element from the  EnQueue( 6 )  rear = 1;
front)
 FrontValue (retrieve value of element
 EnQueue( 2 )  rear = 2;
from front)  DeQueue( )  3, front = 1;
 ShowQueue (print all the values of
queue from front to rear)  DeQueue( )  6, front = 2;
 EnQueue( 5 )  rear = 3;
 EnQueue( 9 )  rear = 0;
 DeQueue( )  2, front = 3;
 DeQueue( )  5, front = (front+1)%MaxSize;
Mushfiqur Rahman CSC 2015: Data Structures Queue  24
 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION Queue
 Check IsEmpty
 Check IsFull
 Initialize( )  front=rear=-1;
 EnQueue (add element to back i.e. at  EnQueue( 3 )  front=rear=0; rear
the rear)

 EnQueue( 6 )  rear = 1;
DeQueue (remove element from the
front)  EnQueue( 2 )  rear = 2;
 FrontValue (retrieve value of element
from front)
 DeQueue( )  3, front = 1;
 ShowQueue (print all the values of  DeQueue( )  6, front = 2;
queue from front to rear)
 EnQueue( 5 )  rear = 3;
 EnQueue( 9 )  rear = 0;
 DeQueue( )  2, front = 3;
 DeQueue( )  5, front = 0;
Mushfiqur Rahman  DeQueue( Structures
CSC 2015: )Data 9, front=rear=-1; Queue  25
 Queue[4],MaxSize=4; front

CIRCULAR QUEUE 0 1 2 3 4
- OPERATION  Initialize( )  front=rear=-1;
Queue
 Check IsEmpty

 EnQueue( 3 )  front=rear=0;
Check IsFull
 EnQueue (add element to back i.e. at
 EnQueue( 6 )  rear = 1; rear
the rear)  EnQueue( 2 )  rear = 2;
 DeQueue (remove element from the
front)  DeQueue( )  3, front = 1;
 FrontValue (retrieve value of element  DeQueue( )  6, front = 2;
from front)
 EnQueue( 5 )  rear = 3;
 ShowQueue (print all the values of
queue from front to rear)  EnQueue( 9 )  rear = 0;
 DeQueue( )  2, front = 3;
 DeQueue( )  5, front = 0;
 DeQueue( )  9, front=rear=-1;
 DeQueue( )  Queue Empty, (front==-1) && (rear==-1);
Mushfiqur Rahman CSC 2015: Data Structures Queue  26

You might also like