DSA - Unit II
DSA - Unit II
Stacks & Queues: Stack – Definitions – Concepts – Operations on Stacks – Infix, postfix &
prefix conversions - evaluations of expressions using stack - Applications of stacks –
Representation of Queue – Insertion and Deletion Operation – Applications of Queue.
STACK
Definition
A stack is an Abstract Data Type (ADT), commonly used in most programming
languages.
Stack ADT allows all data operations at one end only. At any given time, we can only
access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last, is accessed first. In stack
terminology, insertion operation is called PUSH operation and removal operation is
called POP operation.
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack
can either be a fixed size one or it may have a sense of dynamic resizing.
Basic features of Stack
1. Stack is an ordered list of similar data type.
2. Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3. push() function is used to insert new elements into the Stack and pop() function is
used to remove an element from the stack. Both insertion and removal are allowed at
only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
OPERATIONS OF STACK
A stack is used for the following two primary operations −
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. A Pop
operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Example
int peek() {
return stack[top];
}
isfull()
Algorithm of isfull() function −
begin procedure isfull
if top equals to MAXSIZE
return true
else
return false
endif
end procedure
Example
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
begin procedure isempty
if top less than 1
return true
else
return false
endif
end procedure
Example
bool isempty() {
if(top == -1)
return true;
else
return false;
}
Prefix Notation
The prefix notation places the operator before the operands. This notation was introduced by
the Polish mathematician and hence often referred to as polish notation.
Example: + A B, -CD etc.
All these expressions are in prefix notation because the operator comes before the operands.
Postfix Notation
The postfix notation places the operator after the operands. This notation is just the reverse of
Polish notation and also known as Reverse Polish notation.
Example: AB +, CD+, etc.
All these expressions are in postfix notation because the operator comes after the operands.
EXPRESSION EVALUATION
In expression evaluation problem, we have given a string s of length n representing an
expression that may consist of integers, balanced parentheses, and binary operations ( +, -, *,
/ ). Evaluate the expression. An expression can be in any one of prefix, infix, or
postfix notation.
Operand1 Operand2 Operator
Example
2. Backtracking
Backtracking is another application of Stack. It is a recursive algorithm that is used for
solving the optimization problem.
3. Delimiter Checking
The common application of Stack is delimiter checking, i.e., parsing that involves analyzing a
source program syntactically. It is also called parenthesis checking.
{ ( a + b) - c } { ( a + b) - c
4. Reverse a Data:
To reverse a given set of data, we need to reorder the data so that the first and last elements
are exchanged, the second and second last element are exchanged, and so on for all other
elements.
Example: Suppose we have a string Welcome, then on reversing it would be Emoclew.
There are different reversing applications:
o Reversing a string
o Converting Decimal to Binary
Reverse a String
A Stack can be used to reverse the characters of a string. This can be achieved by simply
pushing one by one each character onto the Stack, which later can be popped from the Stack
one by one.
REPRESENTATION OF QUEUE
We can easily represent queue by using linear arrays. There are two variables i.e. front and
rear, that are implemented in the case of every queue. Front and rear variables point to the
position from where insertions and deletions are performed in a queue.
Initially, the value of front and queue is -1 which represents an empty queue. Array
representation of a queue containing 5 elements along with the respective values of front and
rear, is shown in the following figure.
The above figure shows the queue of characters forming the English word "HELLO". Since,
No deletion is performed in the queue till now, therefore the value of front remains -1 .
However,
the value of rear increases by one every time an insertion is performed in the queue.
After inserting an element into the queue shown in the above figure, the queue will look
something like following. The value of rear will become 5 while the value of front remains
same.
After deleting an element, the value of front will increase from -1 to 0. however, the queue
will look something like following.
APPLICATIONS OF QUEUE
Due to the fact that queue performs actions on first in first out basis which is quite fair for the
ordering of actions. There are various applications of queues discussed as below.
1. Queues are widely used as waiting lists for a single shared resource like printer, disk,
CPU.
2. Queues are used in asynchronous transfer of data (where data is not being transferred
at the same rate between two processes) for eg. pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
4. Queue are used to maintain the play list in media players in order to add and remove
the songs from the play-list.
5. Queues are used in operating systems for handling interrupts.
QUEUE USING AN ARRAY
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],front=0,rear=0;
void main()
{
int ch;
clrscr();
void enqueue();
void dequeue();
void display();
while(1)
{
cout<<"\n 1. add element";
cout<<"\n 2. remove element";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
}
void enqueue()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}
void dequeue()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{
no=q[front];
front++;
cout<<"\n"<<no<<" -removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}
}