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

Queue implementation without functions

To Maintain elements in the queue

Operations:
Addition of elements
Deletion of elements
Traversal of elements

Creation of empty queue

q=[]

Addition of element

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

Deletion of element

if(q==[]):
    print(“Underflow / Queue 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

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

Source Code:

#Queue implementation
 q=[]
 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):
                 a=input("Enter any element ")
                 q.append(a)
         elif(ch==2):
                 if(q==[]):
                         print("Underflow /  Queue is empty")
                 else:
                         print("poped element is ",q[0])
                         q.pop(0)
         elif(ch==3):
                 n=len(q)
                 if(n==0):
                         print("queue is empty")
                 else:
                         for i in range(0,n):
                                 print(q[i])
         elif(ch==4):
                 print("End")
                 break
         else:
                 print("Invalid choice")