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 | Queues 5

Queue implementation with functions

To maintain number/elements in the form of a Queue

Creation of empty queue

q=[]

Addition of element

def add():
    a=input(“Enter any element “)
    q.append(a)

Deletion of element

def delete():
      if(q==[]):
           print(“Underflow / queus is empty”)
      else:
           print(“poped element is “,q[0])
           q.pop(0)

To check Queue is empty or not

if(q==[]):
print(“Queue is empty”)
else:
print(“Queue is not empty”)

Travesal operation

def traverse():
      n=len(q)
      if(n==0):
            print(“queue is empty”)
     else:
           for i in range(0,n):
                  print(q[i])

Source Code:

#Queue implementation (using functions)
 q=[]
 def add():
         a=input("Enter any element ")
         q.append(a)
 def delete():
         if(q==[]):
                 print("Underflow /  queus is empty")
         else:
                 print("poped element is ",q[0])
                 q.pop(0)
 def traverse():
         n=len(q)
         if(n==0):
                 print("queue is empty")
         else:
                 for i in range(0,n):
                         print(q[i])
 while True:
         print("1. Insert");
         print("2. Delete");
         print("3. Display All/ Traversal")
         print("4. Exit")
         ch=int(input("Enter your choice "))
         if(ch==1):
                 add()
         elif(ch==2):
                 delete()
         elif(ch==3):
                 traverse()
         elif(ch==4):
                 print("End")
                 break
         else:
                 print("Invalid choice")

Maintaining employee details like empno, name and salary using Queues (Using Functions)

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