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

Stack and Queue

Uploaded by

Vikas Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Stack and Queue

Uploaded by

Vikas Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Stacks

Introduction
• A Stack is an important data structure which is widely used in many
computer applications.
• A very simple illustration of a stack is pile of books where one book is
placed on top of another, now when we want to remove a book we
remove the topmost book 1st.
• Hence, we can add or remove an element only at or from one
position which is the topmost position.
• Thus, a stack can be described as a LIFO( last in first out) data
structure that is the element which is inserted last Will be the first
one to be taken out.
Definition of a stack
• A stack is a linear collection of data elements in which the element
inserted last will be the element taken out first (LIFO ). The stack is an
abstract data structure.
• The stack is a linear data structure in which the insertion as well as
deletion of an element is done only from the end called top. One end
is always closed and the other end is used to insert and remove data.
Overflow and underflow in stacks
• The overflow condition is one in a stack which occurs when we try to
insert the elements in the stack, but the stack is already full. Hence, in
that case, the new elements cannot be inserted in stacks. This is
known as an overflow condition in a stacks.
• If TOP=MAX-1, Where MAX is the size of the stack.
Underflow in stacks
• The under flow condition is one in a stack which occurs when we try
to remove the elements from the stack, but the stack is already
empty.
• Hence, in that case no deletions can take place from the stack. This is
known as an under flow condition in stacks.
• If TOP=NULL ,where MAX is the size of the stack.
Operations on Stacks
• The three basic operations that can be performed on stacks are:
• 1.PUSH
• Push operation is the process of adding new elements in the stack.
• Before inserting any new element in the stack, we must always check
for the overflow condition, which occurs when we try to insert an
element in the stack which is already full.
• An overflow condition can be checked as follows, if TOP=MAX-1,
where MAX is the size of the stack.
Algorithm for a push operation in a stack
• Step 1: START
• Step 2: IF TOP= MAX-1
• Print OVERFLOW
• Go to Step 5
• [End of if]
• Step 3:Set TOP=TOP+1
• Step 4: Set STACK [TOP]=ITEM
• Step 5: EXIT
POP
• The pop operation is the process of removing elements from the
stack.
• Before deleting an element from the stack, we must always check for
the underflow condition ,which occurs when we try to delete an
element from the stack which is already empty.
• An under flow condition can be checked as follows,If TOP=NULL.
Algorithm for Pop operation
• Step 1: Start
• Step2:IF TOP =NULL
• Print under flow
• Go to step 5
• [end of if]
• Step3: set ITEM=Stack[TOP]
• Step 4: Set TOP=TOP-1
• Step 5: Exit
Applications of stacks
• Polish and Reverse Polish Notations and their need
• Conversion from Infix Expression to Postfix Expression
• Conversion from Infix Expression to Prefix Expression
• Evaluating of Postfix Expression
• Evaluation of Prefix Expression
• Parenthesis Balancing
Polish Notations
• Polish Notations
• 1. Prefix Form- In an expression, if the operator is placed before the
operands, that is +XY, then it is said to be in prefix form.
• Infix Form- In an expression, if the operator is placed in the middle of
operands, that is ,X+Y, then it is said to be in infix form.
• Postfix form- In an expression, if the operator is placed after the
operands, that is , XY+, then it is said to be in postfix form.
Evaluation of postfix expression
• Suppose P is an arithmetic expression written in postfix expression
the following algorithm which uses a stack To hold the operands
evaluate of P.
• Algorithm
• Evaluate- postfix (stack, top, P)
• //Stack-It is a linear array
• // top- top of the stack where top=-1
• //P: The given postfix expression
• Step 1: i=0
• Step 2: while (P[i]!=null)
• (a). If (P[i]==operand)
• {push operand to the stack}
• (i). Top=top+1
• (ii). Stack [top]=P[i]
• [end if]
• (b). If (P[i]==operator)
• [pop 2 top must operands]
• (i). A=stack[top]
• (ii). Top=top-1
• (iii). B=stack[top]
• [evaluate B*A]
• (iv). Stack [top]=B*A
• [end if]
• ( c). i=i+1
• [end while]
• Step3: [display the result]
• Print the stack [top]
• Step 4: exit
Queues
• A queue is an important data structure which is widely used in many
computer applications. A queue can be visualised with many
examples from our day to day life with which we are already familiar.
A very simple illustration of Queue is a line of people standing outside
to the enter a movie theatre. The first person standing in the line will
enter the movie theatre first. A queue can be described as a FIFO (
first in first out) data structure.
Definition of a Queue
• A queue is a linear collection of data element in which the element
inserted first will be the element taken out first (i.e, a queue is a FIFO
Data structure).
• A Queue is an abstract data structure.
• A Queue is open from both ends.
• A Queue is a linear data structure, in which the first element is
inserted from one end called the rear end and the deletion of the
element takes place from the other end called the front end.
Operations on queues
• Insertion
• insertion is the process of adding new elements in the queue. however
before inserting any new element in the queue we must always check for
the overflow condition, which occurs when we try to insert an element in a
queue which is already full.
• An overflow condition can be checked as follows:
• if REAR= MAX-1, where max is the size of Queue. Hence, if the overflow
condition is true, then an overflow messages is displayed on the screen
;otherwise, the element is inserted into the Queue.
• Insertion is always done at the rear end. insertion is also known as En-
queue.
Algorithm for inserting a new element in a
queue
• Step 1: start
• Step 2: if rear=Max-1
• Print overflow error
• (End of if)
• Step3 : if front =-1 && rear =-1
• Set front =0
• Set rear=0
• Else
• Rear= rear+1
• [end of if]
• Step 4: queue[rear]=item
• Step 5: exit
Deletion
• Deletion is the process of removing elements from the Queue.
• However before deleting any element from the queue we must
always check for the underflow condition which occurs when we try
to delete an element from the Queue which is empty.
• If front=-1 && rear=-1.
• Hence, if the underflow condition is true, then the underflow
message is displayed on the screen ;otherwise the element is deleted
from the queue. Deletion is always done at the front end. Deletion is
also known as De-queue.
Algorithm for deleting an element from a
Queue
• Step 1: start
• Step 2: if front=-1 &&rear=-1
• Print underflow error
• [end of if]
• Step 3: Set item =que[front]
• Step 4: set front =front+1. Or for(i=front ;i<rear ;i++)
• {Queue[i]= queue[i+1];
• }
• Rear- -
• Step 5:exit
Types of queues
• Circular queue
• priority queue
• double ended queue(De-queue)
• Linear queue
Circular queue
• A circular queue is a special type of Queue which is implemented in a
circular fashion rather than in a straight line.
• A circular Queue is a linear data structure in which the operations are
performed based on the FIFO (first in first out) principle and the last
position is connected to the first position to make a circle. It is also
called a ring buffer.
Limitation of linear queues
• In linear queues, we studied how insertion and deletion take place.
We discussed that while inserting a new element in a queue, it is only
done at the rear end. Similarly while deleting an element from the
Queue it is only done at the front end.
Advantages of a circular Queue over a simple
queue
• We can see that even after the deletion of three elements from the
Queue, the Queue is still full as rear= max-1. We still cannot insert
any new element in it as there is no is space to store new elements.
Therefore ,this is the major drawback of the linear queue.
• To overcome this problem we can shift all the elements of the left so
that the new elements can be inserted from the rear end but shifting
all the elements of the queue can be a very time consuming
procedure, as the practical queues are very large in size. Another
solution of this problem is a circular queue.
Priority queue
• A priority queue is a collection of elements such that each element
has been assigned a priority and such that the order in which
elements are deleted and processed comes from the following rules:
1.An element of higher priority is processed before any element of
lower priority.
• 2. Two elements with same priority are processed according to the
order in which they were added to the queue.
• The Array elements in the priority queue can have the following
structure:
• Struct data
•{
• Int item;
• Int priority;
• Int order;
• };
De-queues(double ended queues)
• A double ended queue(de-queue) is a special type of data structure in
which insertion and deletion of elements is done at either end, that
is, either at the front end or at the rear end of the queue.
• It is often called a head tail linked list because elements are added or
removed from either the head (front) end or tail( end) .
• There are two types of double ended queues:
• 1. Input restricted De-queue- In this, the deletion operation can be
performed at both ends( i.e both front and rear end )while the
insertion operation can be performed only at one end (i.e rear end).
• 2. Output restricted De-queue- In this the insertion operation can be
performed at both ends while the deletion operation can be
performed only at one end(i.e front end).
Applications of queues
• In real life, call Centre phone system use queues to hold people
calling them in an order until a service representative is free.
• The handling of interruptions in real-time systems uses the concept of
queues. The interruptions are handled in the same order as they
arrive, that is, first come first served.
• The round robin technique for processor scheduling is implemented
using queues.

You might also like