cs12_Data Structures Stack using List
cs12_Data Structures Stack using List
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.
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.
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.
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
If the character is “)” Repeatedly pop and output all the operators until “(“ is
popped from 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