Data Structure
Data Type vs Data Structure
Data Type
Type of data on which we can perform operations.
Example – Integer, float, string, boolean
Data Structure
It is a specific way of storing and managing
data/information in our computer system so that we
can retrieve data very easily and effectively.
Types
Inbuilt – String, List, Tuple, Dictionaries, Set
User defined – Stack, Queue, Linked list, Tree, Graph
Stack Data Structure
It is a data structure that works on the principle of
LIFO (Last In First Out).
Insertion operation is called Push operation
and Deletion operation is called Pop operation.
Insertion and deletion operation in stack can be
only done from Top of stack.
Peek
Investigating the top of stack without removing it.
Stack Overflow
When size/length of stack exceeds the given limit.
Stack Underflow
When we try to delete from an empty stack
Example of Stack
Printing from a printer.
Stack of chair.
Stack of Notebooks.
Stack of coins.
Function call.
Bangles worn on wrist.
Plates placed one above other.
Pile of cloths in almirath.
Application of stack
1. Undo in Text editors
2. Reversing a word or line
Operations can be performed on stack
1. Push
2. Pop
3. Peek/ Visiting Top
4. Display elements/Traversing
Program for Stack for 10 Elements
stack=[]
def push(item):
if len(stack)==10:
print("Stack Overflow (Max limit 10)")
return
else:
[Link](item)
print("Element inserted ",item)
def pop():
if stack==[]:
print("Stack Underflow")
return
else:
print("Element poped ",[Link]())
def peek():
if stack==[]:
print("Stack underflow")
return
else:
print("Top is ",stack[-1])
def display():
if stack==[]:
print("Stack is Empty")
else:
print("Elements in stack")
for x in range(len(stack)-1,-1,-1):
print(stack[x])
while True:
print("Select your Choice")
print("1 - Push")
print("2 - Pop")
print("3 - Peek")
print("4 - Display Stack")
print("5 - Exit")
choice=int(input("Enter your Choice"))
if choice==1:
data=input("Enter the data ")
push(data)
elif choice==2:
pop()
elif choice==3:
peek()
elif choice==4:
display()
elif choice==5:
print("Program Terminated")
break
else:
print("Invalid Choice - Try again\n")
Based on Condition
For example we have given a dictionary of Roll number
and marks and we have to insert only those student’s
records whose marks are more than 90.
a={1:91,8:42,81:99}
for x in a:
if a[x]>90:
push(a[x])