0% found this document useful (0 votes)
10 views10 pages

UNIT 4 - Stack

This document provides an overview of stacks, including their definition, representation, primitive operations (push, pop, isEmpty, isFull, display), and applications such as expression evaluation and memory management. It details the process of converting infix expressions to postfix and prefix forms using stack data structures. Additionally, it includes algorithms and examples for converting and evaluating expressions.

Uploaded by

shraddhavinod1
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)
10 views10 pages

UNIT 4 - Stack

This document provides an overview of stacks, including their definition, representation, primitive operations (push, pop, isEmpty, isFull, display), and applications such as expression evaluation and memory management. It details the process of converting infix expressions to postfix and prefix forms using stack data structures. Additionally, it includes algorithms and examples for converting and evaluating expressions.

Uploaded by

shraddhavinod1
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/ 10

UNIT 4: Stack

4.1 Introduction

4.2 Representation of Stacks

4.3 Primitive Operations on Stacks

4.4 Applications of Stacks

4.5 Conversion of Infix, Prefix,Postfix Evaluation of Postfix and Prefix

1.1 Introduction What is Stack?

1 )Stack is an ordered list of the same type of elements.

2)It is a linear list where all insertions and deletions are permitted only at one end of the list.

3)Stack is a LIFO (Last In First Out) structure.

4)In a stack, when an element is added, it goes to the top of the stack.

Definition

“Stack is a collection of similar data items in which both insertion and deletion operations are
performed based on LIFO

principle”.

Primitive Operations on Stack


Primitive Operation on Stack

1. Create :-

This operation create a stack, which is empty

2. Push :-

The push operation adds a new element to the stack. As stated above, any element added to the
stack goes at the top, so push adds an element at the top of a stack

push(value) - Inserting value into the stack

In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is
always inserted at top position.

Push function takes one integer value as parameter and inserts that value into the stack.
We can use the following steps to push an element on to the stack..

Step 1 - Check whether stack is FULL. (top == SIZE-1)

Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.

Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).

3 Pop :-

The pop operation removes and also returns the top-most (or most recent element) from the stack.

pop() - Delete a value from the Stack

In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top

position. Pop function does not take any value as parameter.

We can use the following steps to pop an element from the stack...

Step 1 - Check whether stack is EMPTY. (top == -1)

Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the
function.

Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).

3. isEmpty() :-

1)This operation checks whether a stack is empty or not i.e., if there is any element present in the
stack or not.

2)When a stack is completely full, it is said to be Overflow state and if stack is completely empty, it is
said to be Underflow state.
4. Isfull() :-

This operation checks whether the stack isfull. It returns TRUE if stack is full and false otherwise

5. display() - Displays the elements of a Stack

We can use the following steps to display the elements of a stack...

Step 1 – Check whether stack is EMPTY. (top == -1)

Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.

Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value and
decrement i value by one (i--).

Step 4 - Repeat above step until i value becomes '0'

Representation / Implementation of Stack (Static Implementaton) :-

1)The below diagram represents a stack insertion and deletion operation.

2)In a stack, inserting and deleting of elements is performed at a single position which is known as,
Top.

3)Insertion operation can be performed using Push() function and deletion operation can be
performed using Pop() function .

4)New element is added and deleted at top of the stack

5)Delete operation is based on LIFO principle


Following table shows the Position of Top which indicates the status of stack

4.4 Applications of Stack :

Following are some of the important applications of a Stack data structure:

1. Stacks can be used for expression evaluation.

2. Stacks can be used to check parenthesis matching /correctness of nested parenthesis.

3. Reversing a string.

4. Check whether string is palindrome or not.

5. Stacks can be used for Conversion from one form of expression to another.

A. Infix to Postfix or Infix to Prefix Conversion :-

The stack can be used to convert some infix expression into its postfix equivalent, or prefix
equivalent.

B. Postfix or Prefix Evaluation :-

These postfix or prefix notations are used in computers to express some expressions.

6. Stacks can be used for Memory Management.

7. Stack data structures are used in backtracking problems.


Backtracking can be defined as a general algorithmic technique that considers searching every
possible combination in order to solve a computational problem.

backtracking is, that is solving all sub-problems one by one in order to reach the best possible
solution.

Decision Problem –In this, we search for a feasible solution.

Optimization Problem – In this, we search for the best solution.

Enumeration Problem – In this, we find all feasible solutions.

Functions of Stack:

void push()

int val;

if(top==MAX-1) {

printf("\nStack is full!!"); }

else {

printf("\nEnter element to push:");

scanf("%d",&val);

top=top+1;

stack[top]=val;

void pop()

if(top==-1)

printf("\nStack is empty!!"); }

else {

printf("\nDeleted element is %d",stack[top]);

top=top-1; }

void pop()

int i;
if(top == -1) {

printf("\n\n Stack is Empty."); }

else

for(i=top; i>=0; i--) {

printf("\n%d", stack[i]); }

4.5 Expression Evaluation and Conversion :-


How to convert an Infix expression to a Postfix expression?

To convert infix expression to postfix expression, use the stack data structure. Scan the infix
expression from left to right. Whenever we get an operand, add it to the postfix expression and if we
get an operator or parenthesis add it to the stack by maintaining their precedence.

Below are the steps to implement the above idea:

1.​ Scan the infix expression from left to right.

2.​ If the scanned character is an operand, put it in the postfix expression.

3.​ Otherwise, do the following

●​ If the precedence of the current scanned operator is higher than the precedence of
the operator on top of the stack, or if the stack is empty, or if the stack contains a ‘(‘,
then push the current operator onto the stack.

●​ Else, pop all operators from the stack that have precedence higher than or equal to
that of the current operator. After that push the current operator onto the stack.

4.​ If the scanned character is a ‘(‘, push it to the stack.

5.​ If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.

6.​ Repeat steps 2-5 until the infix expression is scanned.


7.​ Once the scanning is over, Pop the stack and add the operators in the postfix expression until
it is not empty.

8.​ Finally, print the postfix expression.

1.​ Infix to Postfix Conversion Following table shows the evaluation of Infix to Postfix:

EXAMPLE to convert infix expression to postfix A+(B*C-(D/E-F)*G)*H


Postfix expression ABC*DE/F-G*-H*+

Q.1)Convert infix to prefix:-


1)A+B*C

2)A/(B^C)+D

3)(A-CB/C)*(D*E-F)

4)A/B^C-D

Q.2)Convert the following expression into prefix


i)​ p*q–rls
Ans:- *pq-rls
-*pqrls

ii) (A+B) / (C+D*E)

Q.4)Write an algorithm to convert given infix expression to postfix expression.

Q.5)Convert the following expression into postfix.

i)​ (A + B) * C – D
ii)​ A + B * C – D/E * F
Practice above questions for SPPU Examination

You might also like