Python Tutorial: Stack in Python

Sunday, 21 August 2022

Stack in Python

COMPUTER SCIENCE

Revision : Stack

Q. What is Data Structure?

Ans: Data structure is a way of storing and organizing information in the computer. It well-defined operations, properties and behaviours.

For example- Stack, Queue, Linked List, Array, List, etc.

 

Q. What do you mean by Stack? What is the significance of TOP in the stack? Write two characteristics of Stack?

Ans: Stack- Stack is a linear data structure that stores or retrieves data from one end only. It follows LIFO (Last In First Out  (LIFO).

Significance of Top : A stack is a list where insertion and deletion can take place only at one end called Top.

Characteristics of Stacks:

·      Only one end is open for entry and exit of the data.

·      No element can be access from the middle.

·      It works on the principle LIFO

·      The insertion and deletion happen at one end i.e. from the top of the stack.

 

Q. What are the operations on the stack? Define each.

Ans:  Operations of the Stack are-

  • Push: Add an element to the stack
  • Pop: Remove an element from the stack
  • Peek: Get the topmost element of the stack
  • Traversal – to display/access all the elements from the stack, from top to bottom.


Q. What do you mean by Underflow and Overflow?

Ans: UNDERFLOW: In stack, the UNDERFLOW condition occurs, when the stack is empty and we try to delete an element from the stack.

OVERFLOW :  In stack, the OVERFLOW condition occurs, when the stack is full, and we try to insert and element into stack.

Code Implementation of Stack Operations using List:

Here two important function.

  • 1.      List.append(item)- function is used to push an element into stack.
  • 2.      List.pop()- function is use to remove an element from the stack.
  •       List[-1]- To get top most element in the stack.

Remember: The LIFO nature must be maintained to carry these oparations.

def push(lst,val):                                                       #insert value into stack

    lst.append(val)

    print("\nPushed successfully\n")

def pop(lst):                                                               # remove value from stack

    if len(lst)==0:

        print("\nEmpty Stack\n")

    else:

        print("\nDeleted Stack : ",lst.pop())

def display(lst):                                                         # Traversing values 

    if len(lst)==0:

        print("\nEmpty Stack\n")

    else:

        L=len(lst)

        for i in range(L-1,-1,-1):

            print(lst[i])

def peek(lst):                                                             # Display Top value

    if len(lst)==0:

        print("\nEmpty Stack\n")

    else:

        L=len(lst)

        print("\nTop Stack :", lst[L-1])

 

#__Main__

lst=[]

while True:

    print('1. Push Stack/Insert')

    print('2. Pop Stack/Delete')

    print('3. Peek/Top')

    print('4. Display/Traversing')

    print('5. Exit')

    ch=int(input("Enter your choice"))

    if ch==1:

        val=int(input("Enter a value :"))

        push(lst,val)                                                       #calling push function for insert new value

    if ch==2:

        pop(lst)                                                              # calling pop function for remove top value

    if ch==3:

        peek(lst)                                                             # calling peek function for display to value

    if ch==4:

        display(lst)                                                         # display list of stack

    if ch==5:                                                                

        break

 Q. Write a function in Python PUSH(S, Arr) where S is a stack and Arr is a List of numbers. From this list push all numbers divisible by 5 into stack implemented by using a list. Display the stack if it has at least one element, otherwise displayed appropriate error message.

S=[];Arr=[10,14,15,55,18,25,13,30,26,110]

def PUSH(S,Arr):

     for x in range(0,len(Arr)):

            if Arr[x]%5==0:

                        S.append(Arr[x])

     if (len(S)==0):

            print("Empty Stack")

     else:

            print(S)

PUSH(S,Arr)

Q. Write a function in Python POP(st), where St is a stack implemented by a list of numbers. The function returns the value deleted from the stack.

def popStack(S):

    if len(S)==0:

       print("Underflow")

    else:

       x=S.pop()

      return x

Q. Define a function PUSH(Stack) in Python (where Stack is a list of numbers) to add move numbers into a stack and display the Stack after insertion of every new number.

def PUSH(Stack):

        x=int(input(“Enter number : ”))

        Stack.append(x)

        print(“Stack is: ”)

        a=len(Stack)-1

        while a>=0:

                print(stack[a])

                a-=1

 Q. Define a function POP(stack) in Python(where Stack list of numbers) to remove a number from stack and return the deleted numbers form the function, Before deletion check the status of stack if the stack is empty then print the appropriate message.

 def POP(Stack):

       if len(Stack)==0:

                print(“Stack is underflow”)

      else:

             x=Stack.pop()

             return x  

Q. Define a function in Python PUSH_EVEN(Stack, x) to check and insert ? into a stack, if X is an even element otherwise print an appropriate message that number is not even and also show the current of TOP after pushing an element into STACK.

STACK=[]

def PUSH_EVEN(STACK,X):

    if X%2==0:

         STACK.append(X)

    else:

         print("X is not even")

    top=len(STACK)-1

    print("Current position of Top", top)

Q. Write a function in Python POP_EVEN(STACK) to remove a number from a stack and print the delete element of STACK and also print an appropriate message if the STACK is empty.

 def POP_EVEN(STACK):

    if len(STACK)==0:

       print("Stack is underflow")

    else:

       x=STACK.pop()

       print(x, "is deleted")

Q. Write a function in Python to perform PUSH operation for a stack containing admission number and name of student. Display the status of Stack after each operation.

Stack=[]

def PUSH(Stack):

    adno=int(input("Enter Admission No. : "))

    name = input("Enter Name : ")

    Stack.append([adno, name])

    i=len(Stack)-1

    while i>=0:

        print(Stack[i])

        i-=1

Q. Write a function in Python to remove a data from a Stack and display the remaining elements after every deletion from the stack. Stack is containing  admission no. and student name.

def  POP(Stack):

    if len(Stack)==0:

        print("Stack is empty")

    else:

        print(Stack.pop(), "is deleted")

        print('Remaining elements at : ')

        i =len(Stack)-1

        while i>=0:

            print(Stack[i])

            i-=1

Q. Write two function in Python – PUSH(Package) to add a new Package and POP(Package) to delte a Package from a list of Package Description, considering them to act as push and pop operatopns of the Stack data structure.

def PUSH(Package):

     pack=input(“Enter Package description : ”)

     Pakcage.append(pack)

def POP(Package):

       if len(Package)==0:

            print(“Stack is empty”)

     else:

           x=Package.pop()

           print(x, “ is deleted”)

 Q. Write a function on Python PUSH_NAME(Stack, name) to push name of all employee  (one by one) containing in a list with name variable passed as an argument .Print the stack after pushing all names as well as return the current position of the Top.

Stack=[]

def push(Stack, name):

   for i in name:

             Stack.append(i)

   i=len(Stack)-1

   while i>=0:

      print(Stack[i])

      i-=1

   Top=len(Stack)-1

   return Top

Q. Write a function POP_NAME(Stack) in Python to remove a name from a stack and return the deleted name from the Stack as well as current position of Top. 

def POP_NAME(Stack):

    if len(Stack)==0:

       X="Stack is empty"

    else:

       X= Stack.pop()

    Top=len(Stack)-1

    return X, Top

Q.  Declare a list to create a Stack which will contain the following information:

(i) City name

(ii) Pin code of city

Stack=[CityName, Pin]

Write a function in Python Push(Stack, city,pin), to insert the data into a stack where Stack is alist of information. Display the information of stack after each insertion.

Stack=[]

def PUSH(Stack, city, pin):

    rec=[city, pin]

    Stack.append(rec)

    i=len(Stack)-1

    while i>=0:

       print(Stack[i])

       i -=1

Q. Write a function in Python POP(Stack) to remove the information from a stack. The function must print the deleted information from the stack.

Stack=[[CityName1, Pin1], [CityName2, Pin2]……………N]

def POP(Stack):

    if len(Stack)==0:

      print("Stack is empty")

    else:

      print(Stack. pop(), " is deleted.")

Q. Write a function to perform push and pop with a Stack containing student records. Student details which are required to push and pop are given below:

·         Roll no. of type integer

·         Name of type String

·         Marks of type float

Stack=[]

def PUSH(Stack):

     r=int(input("Enter roll no. : "))

     n=input('Enter name : ')

     m=float(input("Enter marks :"))

     rec=[r,n,m]

     Stack.append(rec)

     print(Stack)

def POP(Stack):

    if len(Stack)==0:

        print("Stack is underflow")

    else:

        x=Stack.pop()

        print(x, "is removed")

Q. Consider a list of employees containing empNo, empName and empSalary. Write a function in Python PUSH(Stack, List), to push only those records from a list whose salary is more than 50000/-. At every insertion return the current location of Top.

Stack=[]

List=[[111,'Gurmeet', 50000],[222,'Jyotsna',54000],[333,'Deepshikha',52000]]

def  PUSH(Stack, List):

    for i in List:

       if i[2]>50000:

           Stack.append(i)

    Top=len(Stack)-1

    return Top

PUSH(Stack, List)

Q. Write a function Push(Stack, Book) in Python to add all book into a stack where a Bookname will be passed as an argument. Print the names of all books after insertion of complete book list into a stack.

def  Push(Stack, Book):

           for I in Book:

                 Stack.append(I)

            print(Stack)

Q. Write a function Pop(Stack)  in Python to remove an existing book from the stack of books passed as an argument as well as print the current location of Top after removing a book from a stack. Function should also print the appropriate message if there is no book left in the book.

def Pop(Stack):

            if len(Stack)==0:

                  print(“Stack is underflow”)

           else:

                x=Stack.pop()

                Top=len(Stack)-1

                print(x, “ is removed”)

                print(“Current location of Top is :”, Top)

 Q. Define  function stackpush() to insert nodes into a Stack and stackpop() to delete nodes from a Stack having  the following structure for each node:

Node=[[Name_1, Age_1], [Name_2, Age_2],…………………..[Name_N, Age_N]]

Stack=[]

List=[['Narain',46],['Eram',40],['Manu',34],['Rashmi',40]]

def Stack_Push(Stack, List):

    for i in range(len(List)):

        Stack.append(List[i])

def Stack_pop(Stack):

    if len(Stack)==0:

       print("Stack is empty")

    else:

       print(Stack.pop(), " is deleted")

Q. Ajay has a list containing integers.  You need to help him create a program with separate user-defiend functions to perform the following operations based on this list.

·      Traverse the content of the list and push all positive numbers into a stack.

·      Pop and displau the content of the stack.

for example:

if the sample Content of the list is as follows :

N=[-2, 131, -34, 56, 21, -379, 98, -22, 35, 38]

Output :

38  35  98  21  56  131

Ans:


N=[-2, 131, -34, 56, 21, -379, 98, -22, 35, 38]

def PUSH(S, N):

     S.append(N)

def POP(S):

   if S!=[]:

        return S.pop()

   else:

        return None


#__main__

ST=[]

for k in N:

   if k>0:

      PUSH(ST,k)

while True:

      if ST!=[]:

         print(POP(ST), end= "\n" )

      else:

         break


 

Q. Ravi has a list containing 10 integers. You need to help him create a program with separate user-defined functions to perform the following operation based on this list.

·      Traverse the content of the list and push the even numbers into a stack

·      Pop and display the content of the stack

For Example:

Sample

N=[12,13,34,56,21,79,98,22,35,38]

Output:

38  22  98  56  34  12

Answer :

N=[12,13,34,56,21,79,98,22,35,38]

def PUSH(S,N):

           S.append(N)

def POP(S):

    if S!=[]:

               Return S.pop()

  else:

      return None

#__main__

ST=[]

for k in N:

       if k%2==0:

             PUSH(ST,N)

while True:

 if ST!=[]

        Print(POP(ST), end= “ “)

else:

   break

 

Q. Narain has message (string) that has an upper case alphabet in it. Write a program, with separate user-defined functions to perform the following operations:

·      Push the upper case alphabets in the string into a stack.

·      Pop and display the content of the stack.

For example:

If the message is “All the Best, for your Best Performance”

The output from the program should be : P B B A

Ans:

def PUSH(S, ch):

        S.append(ch)

def POP(S):

   if S!=[]:

     return S.pop()

   else:

     return None


#__main___


String= "All the Best, for your Best Performance"

ST=[]

for ch in String:

     if ch.isupper():

         PUSH(ST,ch)

while True:

     item=POP(ST)

     if item!= None:

         print(item, end= " ")

     else:

        break


Q. Write a python program to read a line of text and Print it in reverse (Reverse word by word).
Sample
input:

Enter any line of text : My name is Narain Ji Srivastava

Output:

Enter any line of text : My name is Narain Ji Srivastava

My

name

is

Narain

Ji

Srivastava

stack  ['My', 'name', 'is', 'Narain', 'Ji', 'Srivastava']

total words are  6

Srivastava Ji Narain is name My

def push():

    line=input("Enter any line of text : ")

    stack=[]

    for word in line.split():

        print(word)

        stack.append(word)

    print("stack ",stack)

    print("total words are ",len(stack))

    n=len(stack)

    for i in range(n-1,-1,-1):

        print(stack[i],end=' ')

 

 

#__main___

push()

Q.  Write a python program to read a line of text and Print it in reverse.

Enter any line of text : Hello World

H

e

l

l

o

 

W

o

r

l

d

stack  ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

total words are  11

dlroW olleH

def stack(line):

    stack=[]

    for word in line:

        print(word)

        for ch in word:

            stack.append(ch)

    print("stack ",stack)

    print("total words are ",len(stack))

    n=len(stack)

    for i in range(n-1,-1,-1):

        print(stack[i],end='')

#__main__

line=input("Enter any line of text ")

stack(line)

Solve it

Q. (a) Write a PUSH(Stack) function in Python to insert an EmpNo, empName and his age into a stack after verifying the age, which should be more than 18 years otherwise print a message “Employee is under age”. Return the current position of TOP from the function.

(b) Write a POP(Stack) function in Python to remove a data from a stack. Print the deleted data from a stack and also return the position of TOP from a function. If the stack is empty, print the appropriate message on screen.

Stack=[]

List=[[111,'Gurmeet', 13],

      [222,'Jyotsna',19],

      [333,'Shikha',23],

      [444,'Aviral',14],

      [555,'Anchit',20]]

def  PUSH(Stack, List):

    for i in List:

       if i[2]>18:

           Stack.append(i)

       else:

           print("Employee is under age")

    Top=len(Stack)-1

    print ("Top Value ",Stack[Top])

def Stack_pop(Stack):

    if len(Stack)==0:

       print("Stack is empty")

    else:

       print(Stack.pop(), " is deleted")

PUSH(Stack, List)

Que :A list contains following record of course details for a University:

[Course_name, Fees, Duration]

Write the following user defined functions to perform given operations on the stack named 'Univ':

(a) Push_element(CourseStack,newCourse): To push an object containing the Course_name, Fees, and Duration of a course, which has fee greater than 100000 to the stack.

(b) Pop_element(CourseStack): To pop the object from the stack and display it. Also, display "Underflow" when there is no element in the stack.

(c) Peek_element(CourseStack): This function displays the topmost course record from the stack without delete it. If the stack is empty display message “Empty Stack”

 

For Example:

If the lists of courses details are:

["MCA", 200000, 3]

["MBA", 500000, 2]

["BA", 100000, 3]

The stack should contain:

["MCA", 200000, 3]

["MBA", 500000, 2]

Ans:

Course=[["MCA", 200000, 3],

       ["MBA", 500000, 2],

       ["BA", 100000, 3]]

Univ=[]

def Push_element(Univ,Course):

    for L in Course:

        if L[1]>100000:

            Univ.append([L[0],L[1],L[2]])

def Pop_element(Univ):

    if Univ==[]:

        print("Stack Empty")

       

    else:

        print(Univ.pop())

def peep(Univ):

    if Univ==[]:

        print("Stack empty")

    else:

        L=len(Univ)

        print(Univ[-1])

def traverse(Univ):

    if Univ==[]:

        print("Stack empty")

    else:

        for i in range(len(Univ)-1,-1,-1):

            print(Univ[i])

 

Push_element(Univ,Course)

#Pop_element(travel)

peep(Univ)

traverse(Univ)    

OR

Que : Write separate user defined functions for the following:

(a) PUSH(N) : This function accepts a list of names, N as parameter. It then pushes only those names in the stack named OnlyA which contain the letter 'A'

(b) POPA(OnlyA) : This function pops each name from the stack OnlyA and displays it. When the stack is empty, the message "EMPTY" is displayed.

(c) Traverse(OnlyA) : Display all elements.  If the stack is empty display message “Empty Stack”

 

For Example:

If the names in the list N are

['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']

Then the stack OnlyA should store

['ANKITA','ANWAR','HARKIRAT']

And the output should be displayed as

HARKIRAT ANWAR ANKITA EMPTY

Ans:

N=['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']

OnlyA=[]

def Push(OnlyA):

    for L in N:

        if 'A' in L:

            OnlyA.append([L])

def PopA(OnlyA):

    if OnlyA==[]:

        print(" Empty")

       

    else:

        print(OnlyA.pop())

def peep(OnlyA):

    if OnlyA==[]:

        print(" empty")

    else:

        L=len(OnlyA)

        print(OnlyA[-1])

def traverse(OnlyA):

    if OnlyA==[]:

        print("Empty Stack")

    else:

        for i in range(len(OnlyA)-1,-1,-1):

            print(OnlyA[i])

 

Push(OnlyA)

#Pop_element(travel)

peep(OnlyA)

traverse(OnlyA)

Que: A list, NList contains following record as list elements:

[City, Country, distance from Delhi]

Each of these records are nested together to form a nested list. Write the following user defined functions in Pythons to perform the specified operations on the stack named travel.

(a) Push_element(travel,NList): It takes the nested list as an argument and pushes a list object containing name of the city and country, which are not in India and distance is less than 3500 km from Delhi.

(b) Pop_element(travel): It pops the objects from the stack and displays them. Also, the function should display "Stack Empty: when there are no elements in the stack.

(c) peep(travel): This function displays the topmost element of the stack without deleting it. If the stack is empty, the function should display ‘None’.

For example:

If the nested list contains the following data:

NList=[["New York", "USA",11734],

["Naypyidaw","Myanmar", 3219],

["Dubai","UAE",2194],

["London","England",6693],

["Gangtok","India",1580],

["Columbo","Sri Lanka",3405]]

The stack should contain:

["Naypyidaw","Myanmar"]

["Dubai","UAE"]

["Columbo","Sri Lanka"]

The output should be:

["Columbo","Sri Lanka"]

["Dubai","UAE"]

["Naypyidaw","Myanmar"]

Stack Empty

Ans:

NList=[["New York", "USA",11734],

       ["Naypyidaw","Myanmar", 3219],

       ["Dubai","UAE",2194],

       ["London","England",6693],

       ["Gangtok","India",1580],

       ["Columbo","Sri Lanka",3405]]

travel=[]

def Push_element(NList):

    for L in NList:

        if L[1]!='India' and L[2]<3500:

            travel.append([L[0],L[1]])

def Pop_element(travel):

    if travel==[]:

        print("Stack Empty")

       

    else:

        print(travel.pop())

def peep(travel):

    if travel==[]:

        print("Stack empty")

    else:

        L=len(travel)

        print(travel[-1])

Que:Write a function in Python(SItem) where, SItem is a dictionary containing the details of stationary items - {Sname:Price}.

(a)  Push(SItem, Price): The function should push the names of those items in the stack who have price greater than 75. Also display the count of elements pushed into the stack.

(b)  Pop(Price): this function pops the topmost book record from the stack and returns it. If the stack is already empty, the function should display “None”

(c)   Traverse(SItem) : This function is show all element of the stack. If the stack is empty, the function should display “Empty Stack”

 

For example:

If the dictionary contains the following data:

Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}

The stack should contain

Notebook

Pen

The output should be

The count of elements in the stack is 2

Ans:

Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}

SItem=[]

def Push(SItem,Price):

    count=0

    for k in Ditem:

        if (Ditem[k]>=Price):

            print(k)

            SItem.append(k)

            count=count+1

           

    print("The count of elements in the stack is : ", count)

def Pop(SItem):

    if len(SItem)==0:

        print("None")

    else:

        print(SItem.pop())

def traverse(SItem):

    if len(SItem)==0:

        print("None")

    else:

        for i in range(len(SItem)-1,-1,-1):

            print(SItem[i])

 Que: A list contains following records of students: [Adm_no,SName,DOB,Mobile]

Write the following user defined function to perform given operations on the stack named 'Admission':

i)      PUSH() To insert an object containing admission number,student name and Mobile of the students who's birth year is after 2008 to the stack. If stack holds more than 3 elements                       show "stack is full"

ii)             POP() To pop the object from the stack and display them in stack one by one. For Example:

If the lists of students detail are : ["20221017","Guru","20-7-2008","9988779988"]

["20221018","Ragunath","06-8-2009","9977889988"]

["20221019","Jenifer","31-12-2007","9988996677"]

["20221020","Priya","21-10-2009","9966557864"]

 The stack should contain ["20221017","Guru","9988779988"]

  ["20221018","Ragunath","9977889988"]

["20221020","Priya","9966557864"]

The output should contain ["20221017","Guru","9988779988"] ["20221018","Ragunath","9977889988"]

["20221020","Priya","9966557864"]

Stack is full

Ans:

students=[["20221017","Guru","20-7-2008","9988779988"],

["20221018","Ragunath","06-8-2009","9977889988"],

["20221019","Jenifer","31-12-2007","9988996677"],

["20221020","Priya","21-10-2009","9966557864"]]

Admission=[]

def Push():

    for i in students:

        if int(i[2][-4:])>2008:

             Admission.append(i)

    else:

        print("Stack is full")

def Pop():

    if Admission==[]:

        print("Empty Stack")

    else:

        print(Admission.pop())

def traverse():

    if Admission==[]:

        print("Empty Stack")

    else:

        for i in range(len(Admission)-1,-1,-1):

            print(Admission[i])

Push()

traverse()

Pop()

Que: Vikram has a list containing 10 integers. You need to help him to create a program with  separate user defined functions to perform the following operations based on this list.

   pushnum() to Traverse the content of the list and push the numbers that are divisible by 5  into a stack.

   popnum() to Pop and display the content of the stack and display “Stack underflow” if  the stack is empty.

For Example:

If the sample Content of the list is as follows:

N=[10, 13, 34, 55, 21, 79, 98, 22, 35, 38]

Sample Output of the code should be:

35

55

10

Ans:

N=[10, 13, 34, 55, 21, 79, 98, 22, 35, 38]

stack=[]

def pushnum():

    for i in N:

        if i%5==0:

            stack.append(i)

    print("Stack Elements")

    for i in range(len(stack)-1,-1,-1):

        print(stack[i])

def pop():

    if len(stack)==0:

        print("Stack Underflow")

    else:

        print("Popped element : ",stack.pop())

   

pushnum()

pop()

Que :Write a function in Python, Push(Ride) where , Ride is a dictionary containing the details           of Cab ride , Ride={driver: billamt }.

The function should push the names of those driver names in the stack, INCENTIVE who               have billed greater than 400. Also display the count of names pushed into the stack

For example:

If the dictionary contains the following data: Ride={‘Raghu’:389,’Anbu’:560,’Siraj’:768,’Syed’:450 }. The stack INCENTIVE should contain [‘Anbu’,’Siraj’,’Syed’]

The output should be:

The count of Drivers in the stack is 3

Ans:

Ride={'Raghu':389,'Anbu':560,'Siraj':768,'Syed':450 }

INCENTIVE=[]

def push(Ride):

    c=0

    for i in Ride:

        if Ride[i]>400:

            INCENTIVE.append(i)

            c=c+1

    else:

        print("Stack Values :")

        for i in range(len(INCENTIVE)-1,-1,-1):

            print(INCENTIVE[i])

        print("The count of Drivers in the stack is ",c)

   push(Ride)

Q. Write a function in Python PUSH_EMP(Stack, EMP_list) to push records of those employees whose deptno is either 333 or 444 containing in a EMP_list passed as an argument and return the current position of the Top.

Note: Push the data by considering  the structure of Emp_list.

Emp_list=[[333, “Priyanshi”], [111, “Soumya”], [444, “Avin”]………………….[999,N]]

Q. Write a function in Python POP_EMP(Stack) to remove a data from a stack and print the deleted data and return the current location of Top

Note: Pop the data by considering  the structure of Emp_list.

Emp_list=[[333, “Priyanshi”], [111, “Soumya”], [444, “Avin”]………………….[999,N]]

Q. Write a function in Python Push_std_rec(Stack) to accept name and age of student and push it into s Stack if student’s age is less than 18 otherwise print an appropriate message. Also return the status of Top from a function at the time of function termination.

Q. Write a function in Python pop_std(Stack) to remove a student record from a Stack and print the deleted content on  screen and also display  message “Stack is Undeflow”  if  stack is empty. Consider the containing student’s roll no name and marks .

Stud=[roll, name , marks]

Q. Consider the list of employee [Eno, Ename, Esalary]. Write a function in Python PUSH(Stack, name), to insert only those records into a list whose name begins with ‘A’ or ‘a’. At every insertion return the current location of Top.

No comments:

Post a Comment