0% found this document useful (0 votes)
3K views

Practical Implementation of Stack Using List

The document discusses implementing stack operations using lists in Python. It provides functions to push and pop student names, numbers, and hostel records to and from lists, treating the lists as stacks. It also provides sample questions and answers for adding and removing items from stacks using push and pop operations.

Uploaded by

Saara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Practical Implementation of Stack Using List

The document discusses implementing stack operations using lists in Python. It provides functions to push and pop student names, numbers, and hostel records to and from lists, treating the lists as stacks. It also provides sample questions and answers for adding and removing items from stacks using push and pop operations.

Uploaded by

Saara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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):

sn=input("Enter name of student")

st.append(sn)

def pop(st):

if(st==[]):

print("Stack is empty")

else:

print("Deleted student name :",st.pop())

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):

sn=input("Enter any Number")

st.append(sn)

def pop(st):

if(st==[]):

print("Stack is empty")

else:

print("Deleted Number is :",st.pop())

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):

hn=int(input("Enter hostel number"))

ts=int(input("Enter Total students"))


tr=int(input("Enter total rooms"))

temp=[hn,ts,tr]

host.append(temp)

def pop(host):

if(host==[]):

print("No Record")

else:

print("Deleted Record is :",host.pop())

def display(host):

l=len(host)

print("Hostel Number\tTotal Students\tTotal Rooms")

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("1. Add Record\n")

print("2. Delete Record\n")

print("3. Display Record\n")

print("4. Exit")

op=int(input("Enter the Choice"))

if(op==1):

push(host)

elif(op==2):

pop(host)

elif(op==3):

display(host)

elif(op==4):

break

ch=input("Do you want to enter more(Y/N)")


1. Write a function Push() which takes number as argument and add in a stack "MyValue"

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):

if len(MyStack) < StackSize:

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.")

Q5. Write push(rollno) and pop() method in python:

push(rollno) --add roll number in Stack

pop() --- remove roll number from Stack.

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

bookname considering them to act as push() and pop() operations in stack.


Hide Answer

Ans.

MyStack=[]

def add(bname):

MyStack.append(bname)

def delete(MyStack):

if len(MyStack) > 0:

MyStack.pop()

else:

print("Stack is empty. There is no book name")

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

operations of the stack.

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.

You might also like