Practical Implementation of Stack Using List
Practical Implementation of Stack Using List
Q1. Write a function push(student) and pop(student) to add a new student name and remove a
student name from a list student, considering them to act as PUSH and POP operations of stack
Data Structure in Python.
st=[ ]
def push(st):
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
Q2. Write a function push(number) and pop(number) to add a number (Accepted from the user)
and remove a number from a list of numbers, considering them act as PUSH and POP operations of
Data Structure in Python.
st=[ ]
def push(st):
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
Q3. Write a menu based program to add, delete and display the record of hostel using list as stack
data structure in python. Record of hostel contains the fields : Hostel number, Total Students and
Total Rooms
host=[ ]
ch='y'
def push(host):
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
def display(host):
l=len(host)
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("4. Exit")
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
Hide Answer
Ans.
MyValue=[]
def Push(value):
MyValue.append(value)
Q2. Write a function Push that takes "name" as argument and add in a stack named "MyStack"
Hide Answer
Ans
Mynames=[]
def Push(Value):
Mynames.append(Value)
Push("Amit")
Q3. Write a function Push() which takes "name" as argument and add in a stack named
"MyStack". After calling push() three times, a message should be displayed "Stack is
Full"
Hide Answer
Ans.
MyStack=[]
StackSize=3
def Push(Value):
MyStack.append(Value)
else:
print("Stack is full!")
Q4. Write a function pop() which remove name from stack named "MyStack".
Hide Answer
Ans.
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")
Hide Answer
Ans.
MyStack=[]
def Push(rollno):
MyStack.append(rollno)
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")
Q6. Write add(bookname) and delete() method in python to add bookname and remove
Ans.
MyStack=[]
def add(bname):
MyStack.append(bname)
def delete(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
Q7. Write addclient(clientname) and remove() methods in python to add new client and
delete existing client from a list "clientdetail", considering them to act as push and pop
Hide Answer
Ans.
clientdetail=[]
def addclient(cn):
clientdetail.append(cn)
def remove():
if len(clientdetail)>0:
clientdetail.pop()
else:
print("Stack is empty")
Question 1:
Define a data structure.
Answer:
A data structure is a group of data which can be processed as a single unit. This group of
data may be of similar or dissimilar data types. Data Structures are very useful while
programming because they allow processing of the entire group of data as a single unit.