Queue PPT

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Linear Data Structures

By
Kalyani C. Waghmare
Linear data structures
• The elements in it are adjacent to each other
• Each element has exactly two neighbours (next,
previous)except first and last
• Graphical representation is linear
• Array
A B C D E F G

• Linked list
A C F D Null

• Stack Insert
A|B|C|D|E|
Delete
• Queue
Insert |E|D|C|B|A Delete
Queue
• Queue
– Works as First in first out
– Maintain front & rear variable
– Have operations add, delete, empty, full
– add is inserting data item in queue at rear
– delete is deleting data item from front.
– Empty checks emptiness of queue
– Full checks maximum data items are occupied or not.
QUEUE ADT
ADT Queue is
// Value Definition
Objects: A finite ordered list with zero or more elements and front pointer pointing to first
element (one end of the queue) and rear pointer pointing to last element (other end of the
queue)
//Operator Definition
Functions:
For all Qє Queue and item є element createQ() : Q
//creates the empty queue

addQ(Q,item) :void
//adds item to Q from rear
deleteQ(Q):void
//deletes an element from front if q is not empty
isemptyQ(Q):boolean
//if q is empty returns TRUE else returns FALSE
frontQ(Q): item
//if q is not empty returns the front element else returns error
rearQ(Q): item
//if q is not empty returns the rear element else returns error
End Queue
Queue ADT as class
ADT for Queue is
Value definition
eltype q[20] or *q;
Int front, rear;
Operator Definition
queue() ; //does initialization
add(eltype) ; //perform addition of new dataitem in
queue at rear
eltype delete() ; //deletes data item at the front
}
Queue operations
Queue :: Queue()
{ front = rear = -1; }
Queue :: add(int val)
{
If(rear ==19)
Print “Queue is full”;
Else
{rear++ ; if (rear ==0) then front = rear;
q[rear] = val; }
}
Int Queue :: delete()
{
If(front ==-1)
Print “Queue is empty”;
Else
X = q[front];
Front++; if(front ==19|| front==rear+1) then front =rear =-1
Return(x);
Types of Queue
• Types of Queue
– Circular Queue
– Double ended Queue
– Priority Queue
• Applications of Queue
– Job scheduling, process scheduling– priority Queue, normal queue
– Josephus problem - CQ
– Palindrome checking - DQ
– Sorting - PQ
Circular Queue
• Q(1:n ) or Q[0:n-1] is considered as circular
• Elements are added from 0 to n-1
• If rear is at n-1, next element is added at 0 if it is free
• Front will point one position counterclockwise from
first element
• Rear will point to last element, clockwise from front
element
• Front=Rear when Queue is empty
• Addition and deletion can be done in O(1)
• Modulo operator is used get next position in Queue
20
Maximum
elements =
4
Rear=

1 3
3

0 0
Front=
0
Circular Queue Operations
void Queue::caddQ(ele item)
// add an item to rear of Queue begin
if (front==(rear+1)% max)
cout<<“Queue is full”
else
rear=(rear+1)%max
list[rear]= item;
end if
end caddQ
Circular Queue Operations
ele Queue::cdelete()
// delete item from front of Queue
ele x;
If(front==-1)
cout<<“Queue is empty”;
else
X = list[front];
if(front==rear)
front = -1, rear=-1;
else
front=(front+1)% max
end if
End if
End caddQ
Double Ended Queue(DQ)
• Adds element at both front and rear
• Deletes elements from both front and
rear
• Can be linear or circular
DQueue ADT as class
ADT for DQueue is
Value definition
eltype q[20] or *q;
Int front, rear;
Operator Definition
DQueue() ; //does initialization front=-1,rear=20
addRear(eltype) ; //perform addition of new dataitem in
queue at rear
addFront(eltype) ; //perform addition of new dataitem in
queue at front
eltype deleteRear() ; //deletes data item at the rear
eltype deleteFront() ; //deletes data item at the front
DQ Operations
void DQAddFront(ele item)
// add an item to front of Queue begin
if (front==rear+1) cout<<“Queue is full”
Else
Front++;
list[front]= item;
endif
end DQAddFront

void DQAddRear(ele item)


// add an item to front of Queue begin
if (front==rear+1) cout<<“Queue is full”
Else
Rear --;
list[Rear]= item;
endif
end DQAddRear
DQ Operations
ele DQDelFront(ele item)
// add an item to front of Queue begin
ele x;
if (front==-1) cout<<“Queue is empty from front side”
else
x = list[front];
front--;
return(x)
endif
end DQDelFront
ele DQDelRear(ele item)
// add an item to front of Queue begin
if (rear==20) cout<<“Queue is empty from rear side”
else
x = list[Rear]
Rear ++;
endif
end DQDelRear
Priority Queue
• Priority queue supports
– Inserting element with a key
– Deleting min key element or Deleting max key
element
• Delete is to be with minimum complexity
• Can be implemented with sorted array
• Insertion and Deletion takes O(n)
• Min or Max heap supports these insert
and delete operations
• It has O(log n ) time for insertion and
deletion
Priority Queue ADT
//Value Definition
Objects: A List with n>0 elements such that element is inserted with
priority and deletes an element with max/min priority
//Operator Definition
Functions:
•create(int size): PriorityQ
•//creates an empty PriorityQ isFull: boolean
•//returns true if elements equal to max size of Q else returns false insert
( ele item): void
•//inserts an element with priority if not full else error isEmpty():
boolean
•//returns true if Q is empty else returns false delete(): ele
•//returns largest element/smallest priority element if Q is not empty else
returns 0
Dynamic implementation of stack
Class stack; Class stack;
Class node Class node
{ {
Int data; Int data;
Node * next; Node * next;
Friend class stack; Friend class stack;
}; };
Class stack Class stack
{ {
Int top; int f, r Node *top;
Node *s[20]; Stack();
Stack(); Push (int val);
Push (int val); Node * pop();
Node * pop(); }
}
Stack :: stack() Stack :: stack()
{ top = -1; { top = NULL;
} }

Stack :: push(int val) add Stack :: push(int val) // inser_start


{ { node * temp;
if(top = 19) temp = new node(val);
print ‘stack is full’ top ->next = temp;
else top = temp;
top++; r++ }
node * p;
p = new node(val); Node *Stack :: pop() //dele_start
s[top] = p; {
} if(top ==NULL)
Node * stack :: pop() print ‘stack is empty’
{ else
node * temp; node * temp;
if(top ==-1) temp = top->next;
print ‘stack Is empty’ top = top->next;
else return(temp);
temp = s[top]; }
top --;
return(temp);
}

You might also like