Home Page class 12 @ Python

CBSE Class 12 : Data Structures

Introduction to Data Structures

CBSE Class 12 : Searching

Creation And Traversal
What is Searching?
Linear/Sequential Search
Binary Search
Insertion Of Element
Deletion Of Element

CBSE Class 12 : Sorting

What is sorting?
Bubble Sort
Selection Sort
Insertion Sort

CBSE Class 12 : Stacks

Stacks
Applications of Stacks
Implementation of Stacks

CBSE Class 12 : Queues

Queues
Applications Of Queues
Implementation of Queues

Class 12 Data Structures | Stacks 6

Example: 3 (Maintaing employee details like empno,name and salary using stacks)

To maintain employee details like empno, name and salary using a stack

Operations:
Addition of elements (PUSH)
Deleteion of elements (POP)
Traversal of elements (DISPLAY)

Creation of empty stack

employee=[]

Push operation

empno=input(“Enter empno “)
name=input(“Enter name “)
sal=input(“Enter sal “)
emp=(empno,name,sal)
employee.append(emp)

Pop operation

if(employee==[]):
print(“Underflow / Employee Stack in empty”)
else:
empno,name,sal=employee.pop()
print(“poped element is “)
print(“empno “,empno,” name “,name,”

To check stack is empty or not

if(employee==[]):
print(“stack is empty”)
else:
print(“stack is not empty”)

Traversal operation

if not (employee==[]):
n=len(employee)
for i in range(n-1,-1,-1):
print(employee[i])
else:
print(“Empty , No employee to display”)

Source Code:

#stack implementation using functions
#program to create a stack of employee(empno,name,sal).
"""
push
pop
traverse
"""
employee=[]
def push():
        empno=input("Enter empno  ")
        name=input("Enter name ")
        sal=input("Enter sal ")
        emp=(empno,name,sal)
        employee.append(emp)
def pop():
        if(employee==[]):
                print("Underflow / Employee Stack in empty")
        else:
                empno,name,sal=employee.pop()
                print("poped element is ")
                print("empno ",empno," name ",name," salary ",sal)
def traverse():
        if not (employee==[]):
                n=len(employee)
                for i in range(n-1,-1,-1):
                        print(employee[i])
        else:
                print("Empty , No employee to display")
while True:
        print("1. Push")
        print("2. Pop")
        print("3. Traversal")
        print("4. Exit")
        ch=int(input("Enter your choice "))
        if(ch==1):
                push()
        elif(ch==2):
                pop()
        elif(ch==3):
                traverse()
        elif(ch==4):
                print("End")
                break
        else:
                print("Invalid choice")