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

cs12_Data Structures Stack using List

Uploaded by

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

cs12_Data Structures Stack using List

Uploaded by

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

Data Structures using List : Stack

Data Structure
A data structure is the study of logical and mathematical model of named group of data of different
data types.
There are two types of data types of data structure
1. Simple data structure: These data structures are normally built from primitive/basic data types.
E.g.. Array(string, list)
2. Compound data structure: Simple data structures can be combined in various ways to form
more complex structures.

They may further classified into


➢ linear data structure
These are single level data structure whose elements forms sequences. E.g. Stack, queue.
➢ Non linear data structures
These are multi-level structure. E.g. Tree, graph etc.

It a way of organizing and storing data in such a manner so that it can be accessed and work over it
can be done efficiently and less resources are required.
It defines the relationship between the data and the operations over those data.
There are many various types of data structures defined that make it easier for the computer
programmer to concentrate on the main problems rather than getting lost in the details of data
description and access.
STACK
➢ A stack is a linear data structure in which an element will be inserted or deleted only at one
end called TOP of stack.
➢ The element will be removed from the stack in the reverse order of that in which they were
inserted into the stack.
➢ The last item to be added to the stack is the first item to be removed.
➢ So stack is called last-in-first-out(LIFO) list.

The basic operations associated with stacks are


➢ PUSH ( inserting an element into stack )
➢ POP ( deleting an element from stack )
➢ PEEP ( to search for an element)
➢ DISPLAY ( to display all elements in reverse order )

APPLICATIONS OF STACK
• Expression Evaluation: It is used to evaluate prefix, postfix and infix expressions.
• Expression Conversion: It can be used to convert one form of expression(prefix,postfix or
infix) to one another.
• Syntax Parsing: Many compilers use a stack for parsing the syntax of expressions.
• Backtracking: It can be used for back traversal of steps in a problem solution.
• Parenthesis Checking: Stack is used to check the proper opening and closing of
parenthesis.
• String Reversal: It can be used to reverse a string.
• Function Call: Stack is used to keep information about the active functions or subroutines.
Using List as Stack in Python
➢ The concept of Stack implementation is easy in Python , because it support inbuilt functions
(append() and pop()) for stack implementation.
➢ By Using these functions make the code short and simple for stack implementation.
➢ To add an item to the top of the list, i.e., to push an item, we use append() function and to pop
out an element we use pop() function.
➢ These functions work quiet efficiently and fast in end operations.

Code to implement stack as list


stack=[ 1,2,3,4,5]
print(“ Original Stack is “, stack)
a=int(input(“ Enter the element to add “))
stack.append(a) # added at end
print(“ Stack is now “, stack)
A=stack.pop() # last element will be popped out
print(“ Deleted element is “, A)
print(“ Stack is now “, stack)
i=len(stack)-1
print(“ Value obtained after peep “,stack[i])

Menu driven Program for STACK


def display(stk):
if stk == []:
print( "** Stack Empty ** ")
else:
top = len(stk) - 1
print( stk[top], "<-top")
for i in range(top-1,-1,-1):
print(stk[i])

def push(stk,item):
stk.append(item)
def pop(stk):
if stk == []:
return 'Under Flow'
else:
item = stk.pop()
return item

# main program
stack=[] # initially stack is empty
top=None
while True:
print(" \nSTACK OPERATIONS ")
print(" 1. PUSH ")
print(" 2. POP ")
print(" 3. DISPLAY ")
print(" 4. Exit ")
ch= int(input(" Enter your choice (1-4) :"))
if ch == 1:
item=int(input(" Enter element to add "))
push(stack,item)
elif ch == 2 :
item=pop(stack)
if item=="Under Flow":
print(" Under Flow , Stack is empty ")
else:
print(" Popped item is ", item)
elif ch==3:
display(stack)
elif ch==4:
break
else:
print(" Invalid Choice")
APPLICATIONS OF STACK:
➢ Conversion of infix expression into postfix expression
➢ Evaluation of postfix expression

Expression:
Any expression can be represented by any form.
a) Infix a+b
b) Postfix ab+ reverse polish notation
c) Prefix +ab polish notation

Operator precedence( mathematical)


Exponent 3
Multiplication/division 2
Addition/subtraction 1

Operator precedence( logical)


NOT 3
AND 2
OR 1

A. INFIX to POSTFIX CONVERSION


INFIX to POSTFIX CONVERSION ALGORITHM
//assume expression starts with ‘(‘ and end with ‘)’
1. Pick an element from the expression from left to right
2. If element is operand then
Print the element .
goto step 1
3. If element is operator then
While( priority(element) <= priority(stack(top) and stack(top) != ‘(‘ )
Pop and print // pop top most element
Push the element into stack
goto step 1
4. If element is ‘(‘ then Push the element into stack
goto step 1
5. If element is ‘)’ then
While( stack(top) <> ‘(‘ )
Pop and print
if( stack(top)==‘(‘ then
Cancel ‘(‘
goto step 1
6. If no element then
Stop

Scan the Infix expression left to right

If the character x is an Output the character into the Postfix Expression


operand

If the character is “(“ Push it into the stack

If the character is “)” Repeatedly pop and output all the operators until “(“ is
popped from the stack.

If the character x is a is Step 1: Check the character y currently at the top of


a regular operator the stack.
Step 2: If Stack is empty or y=‘(‘ or y is an operator of
lower precedence than x, then push x into stack.
Step 3: If y is an operator of higher or equal
precedence than x, then pop and output y and push x
into the stack.

When all characters in infix expression are processed repeatedly pop the
character(s) from the stack and output them until the stack is empty

B. EVALUATION OF POSTFIX EXPRESSION:


EVALUATION OF POSTFIX EXPRESSION ALGORITHM
1. pick an element from the expression
2. If element is operand then
Push the element into stack
Goto step 1
3. If element is operator then
Pop one element as op1
Pop another as op2
Evaluate op2 operator op1
Push result into stack
Goto step 1
4. If stack empty then
Pop and print
Stop
(Only one operand will be popped if operator is NOT)

You might also like