Data Structure Test 3


Data Structure Test 3

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

Q2. Write a function Push that takes "name" as 
    argument and add in a stack named "MyStack"
 
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"

Q4. Write a function pop() which remove name
    from stack named "MyStack".
 
Q5. Write push(rollno) and pop() method in python:
    push(rollno) --add roll number in Stack
    pop() --- remove roll number from Stack.
 
 
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.
 
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.

 
SOLUTIONS

Q1. Write a function Push() which takes number as argument and add in a  stack "MyValue"
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"
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"
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".
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.
 
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.
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.

Ans. 
clientdetail=[]
def addclient(cn):
   clientdetail.append(cn)

def remove():
   if len(clientdetail)>0:
      clientdetail.pop()
   else:
      print("Stack is empty")

   
 
 
 
 
 
 

 
 
 

No comments:

Post a Comment

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet