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

unit-2

queue

Uploaded by

Dr. S.K. Sajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

unit-2

queue

Uploaded by

Dr. S.K. Sajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES

Stack ADT – Evaluating arithmetic expressions- other applications- Queue ADT –


circular queue implementation – Double ended Queues – applications of queues.

STACK
Stack is a Linear Data Structure that follows Last In First Out (LIFO) principle.
Insertion and deletion can be done at only one end of the stack called TOP of the stack.
Example: - Pile of coins, stack of trays

TOP pointer
It will always point to the last element inserted in the stack.
For empty stack, top will be pointing to -1. (TOP = -1)

Operations on Stack (Stack ADT)


Two fundamental operations performed on the stack are PUSH and POP.
(a) PUSH:
It is the process of inserting a new element at the Top of the stack.
For every push operation:
1. Check for Full stack (overflow).
2. Increment Top by 1. (Top = Top + 1)
3. Insert the element X in the Top of the stack.
(b) POP:
It is the process of deleting the Top element of the stack.
For every pop operation:
1. Check for Empty stack (underflow).
2. Delete (pop) the Top element X from the stack
3. Decrement the Top by 1. (Top = Top - 1)
Exceptional Conditions of stack
1. Stack Overflow
 An Attempt to insert an element X when the stack is full is said to be stack overflow.
 For every Push operation, we need to check this condition.
2. Stack Underflow:
 An Attempt to delete an element when the stack is empty, is said to be stack underflow.
 For every Pop operation, we need to check this condition.

Implementation of Stack
Stack can be implemented in 2 ways.
1. Static Implementation (Array implementation of Stack)
2. Dynamic Implementation (Linked List Implementation of Stack)

Array Implementation of Stack


 Each stack is associated with a Top pointer.
 For Empty stack, Top = -1.
 Stack is declared with its maximum size.
Array Declaration of Stack:
#define ArraySize 5
int S [ Array Size];
or
int S [ 5 ];

(i) Stack Empty Operation:


Initially Stack is Empty.
With Empty stack Top pointer points to – 1.
It is necessary to check for Empty Stack before deleting (pop) an element from the stack.
Routine to check whether a stack is full

int IsFull (Stack S)


{
if (Top = = Arraysize – 1)
return (1);
}

(iii) Push Operation


 It is the process of inserting a new element at the Top of the stack.
 It takes two parameters. Push(X, S) the element X to be inserted at the Top of the Stacks.
 Before inserting an Element into the stack, check for Full Stack.
 If the Stack is already Full, Insertion is not possible.
 Otherwise, Increment the Top pointer by 1 and then insert the element X at the Top of the
stack.
(iv) Pop Operation
 It is the process of deleting the Top element of the stack.
 It takes only one parameter. Pop(X).The element X to be deleted from the Top of the
Stack.
 Before deleting the Top element of the stack, check for Empty Stack.
 If the Stack is Empty, deletion is not possible.
 Otherwise, delete the Top element from the Stack and then decrement the Top pointer by
1.
Linked list implementation of Stack
Stack elements are implemented using SLL (Singly Linked List) concept.
Dynamically, memory is allocated to each element of the stack as a node.
After Deletion

Applications of Stack
The following are some of the applications of stack:
1. Evaluating the arithmetic expressions
 Conversion of Infix to Postfix Expression
 Evaluating the Postfix Expression
2. Balancing the Symbols
3. Function Call
4. Tower of Hanoi
5. 8 Queen Problem

Evaluating the Arithmetic Expression


There are 3 types of Expressions
 Infix Expression
 Postfix Expression
 Prefix Expression

INFIX:
The arithmetic operator appears between the two operands to which it is being applied.

A/B+C
POSTFIX:
The arithmetic operator appears directly after the two operands to which it applies. Also called
reverse polish notation.

PREFIX:
The arithmetic operator is placed before the two operands to which it applies. Also called polish
notation.
Implementation of Queue
Queue can be implemented in two ways.
1. Implementation using Array (Static Queue)
2. Implementation using Linked List (Dynamic Queue)
Initial Configuration of Queue:

(i) Queue Empty Operation:


Initially Queue is Empty.
 With Empty Queue, Front ( F ) and Rear ( R ) points to – 1.
It is necessary to check for Empty Queue before deleting (DeQueue) an element from the Queue
(Q).

(ii) Queue Full Operation


 As we keep inserting the new elements at the Rear end of the Queue, the Queue becomes
full.
 When the Queue is Full, Rear reaches its maximum Array size.
 For every Enqueue Operation, we need to check for full Queue condition.
(iii) Enqueue Operation
 It is the process of inserting a new element at the Rear end of the Queue.
 It takes two parameters, Enqueue(X, Q). The elements X to be inserted at the Rear end of
the Queue Q.
 Before inserting a new Element into the Queue, check for Full Queue.
 If the Queue is already Full, Insertion is not possible.
 Otherwise, Increment the Rear pointer by 1 and then insert the element X at the Rear end
of the Queue.
 If the Queue is Empty, Increment both Front and Rear pointer by 1 and then insert the
element X at the Rear end of the Queue.

(iv) De-Queue Operation


 It is the process of deleting an element from the Front end of the Queue.
 It takes one parameter, De-Queue (Q). Always front element in the Queue will be
deleted.
 Before deleting an Element from the Queue, check for Empty Queue.
 If the Queue is empty, deletion is not possible.
 If the Queue has only one element, then delete the element and represent the empty queue
by updating Front = - 1 and Rear = - 1.
 If the Queue has many Elements, then delete the element in the Front and move the Front
pointer to next element in the queue by incrementing Front pointer by 1.
Linked List Implementation of Queue
 Queue is implemented using SLL (Singly Linked List ) node.
 Enqueue operation is performed at the end of the Linked list and DeQueue operation is
performed at the front of the Linked list.
 With Linked List implementation, for Empty queue Front = NULL & Rear = NULL

Linked List representation of Queue with 4 elements

(i) Queue Empty Operation:

 Initially Queue is Empty.


 With Linked List implementation, Empty Queue is represented as S -> next = NULL.
 It is necessary to check for Empty Queue before deleting the front element in the Queue.

(ii) EnQueue Operation


It is the process of inserting a new element at the Rear end of the Queue.
It takes two parameters, EnQueue ( int X , Queue Q ). The elements X to be inserted
into the Queue Q.
Using malloc ( ) function allocate memory for the newnode to be inserted into the
Queue.
If the Queue is Empty, the newnode to be inserted will become first and last node in the
list. Hence Front and Rear points to the newnode.
Otherwise insert the newnode in the Rear -> next and update the Rear pointer.
(iii) DeQueue Operation
 It is the process of deleting the front element from the Queue.
 It takes one parameter, Dequeue ( Queue Q ). Always element in the front (i.e) element
pointed by Q -> next is deleted always.
 Element to be deleted is made “temp”.
 If the Queue is Empty, then deletion is not possible.
 If the Queue has only one element, then the element is deleted and Front and Rear pointer
is made NULL to represent Empty Queue.
 Otherwise, Front element is deleted and the Front pointer is made to point to next node in
the list.
 The free ( ) function informs the compiler that the address that temp is pointing to, is
unchanged but the data present in that address is now undefined.
Applications of Queue
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order
as they arrive, First come first served.
4. Batch processing in operating system.
5. Job scheduling Algorithms like Round Robin Algorithm uses Queue.

Drawbacks of Queue (Linear Queue)


With the array implementation of Queue, the element can be deleted logically only by
moving Front = Front + 1.
Here the Queue space is not utilized fully.
To overcome the drawback of this linear Queue, we use Circular Queue.

Operations on Circular Queue


Fundamental operations performed on the Circular Queue are
 Circular Queue Enqueue
 Circular Queue Dequeue
(i) Circular Queue Enqueue Operation
 It is same as Linear Queue EnQueue Operation (i.e) Inserting the element at the Rear end.
 First check for full Queue.
 If the circular queue is full, then insertion is not possible.
 Otherwise check for the rear end.
 If the Rear end is full, the elements start getting inserted from the Front end.

FULL QUEUE

Circular Queue DeQueue Operation


 It is same as Linear Queue DeQueue operation (i.e) deleting the front element.
 First check for Empty Queue.
 If the Circular Queue is empty, then deletion is not possible.
 If the Circular Queue has only one element, then the element is deleted and Front and
Rear pointer is initialized to - 1 to represent Empty Queue.
 Otherwise, Front element is deleted and the Front pointer is made to point to next element
in the Circular Queue.
DOUBLE-ENDED QUEUE (DEQUE)
In DEQUE, insertion and deletion operations are performed at both ends of the Queue.
Applications of Queue
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order
as they arrive, First come first served.
4. Batch processing in operating system.
5. Job scheduling Algorithms like Round Robin Algorithm uses Queue.

You might also like