Write a python program to implement a stack or queue using list data structure|Stack and stacks operations in python|CBSE CLASS 12 CS PROGRAMS|List data structure in python
Introduction
In this blog you will learn to implement a stack or queue using List data structure. These practical programs are part of the 2021 Computer Science Syllabus (CBSE).
List
You can learn about List and Data Structures in detail by using the link: -https://learntechpython.blogspot.com/2021/07/data-structures.html
Stack
A stack is a linear data structure that follows a particular sequence of LIFO (Last In First Out) or FILO (First In Last Out). We can do many things in this data structure.
Some real life examples are :- Stack of plates, stack of books one by one.
Some of the basic operations of the stack are push() and pop() .
Push():- It is used to add an element to the stack.
Pop():- Used to remove an element from the stack.
Queue
A Queue is a linear data structure that follows a specific order of first in first out (FIFO).
Some real life examples are:- Any queue of customers in a shop where the customer who comes first is served first.
The only difference between Stack and Queue is in the removal of elements or data. In the stack we remove the element which is recently added but in the queue, we always remove the first added element.
The functions used for both the purposes are same just the difference is in the implementation.
Souce Code for implementing a queue is:-
queue=[]
choice='yes'
while choice=='yes':
print('1.INSERT')
print('2.DELETE')
print('3.Display elements of stack')
userchoice=int(input("Enter what you want to perform:"))
if userchoice==1:
a=int(input("Enter the element which you want to push : "))
queue.append(a)
elif userchoice==2:
if queue==[]:
print("Stack is empty")
else:
print("deleted element is :",queue.pop(0))
elif userchoice==3:
for i in range(len(queue)):
print(queue[i])
else :
print("invalid input")
choice=input("want perform more functions? Yes or no:")
if choice=='no':
break
#Code ended
Output:-
stack=[]
choice='yes'
while choice=='yes':
print('1.PUSH')
print('2.POP')
print('3.Display elements of stack')
userresponse=int(input("what do you want to perform?:"))
if userresponse==1:
a=int(input("Enter the element which you want to push : "))
stack.append(a)
elif userresponse==2:
if stack==[]:
print("Stack is empty..... Underflow")
else:
print("deleted element is :",stack.pop())
elif userresponse==3:
for i in range(len(stack)-1,-1,-1):
print(stack[i])
else :
print("invalid input")
choice=input("want perform more functions?Yes or no")
if choice=='no':
break
#code ended
Other Cbse practical programs:-
Text file programs-https://learntechpython.blogspot.com/2021/07/cbse-class-12-computer-science-python.html
Random_number_generator(रैंडम_संख्या_जनरेटर):-https://learntechpython.blogspot.com/2021/05/write-python-program-as-random-number.html
Binary_files_program:-https://learntechpython.blogspot.com/2021/05/python-program-to-create-binary-file.html
Printing_given_patterns:-https://learntechpython.blogspot.com/2021/05/python-program-to-print-pattern-of.html
"Hope it helped"
Have anything in mind? Let me
know in the comment section👇
Have anything in mind? Let me
know in the comment section👇