19CSE212-CSEB-2023-STACK


Submit solution

Points: 20
Time limit: 100.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Python

This is a simple data structures problem. Your goal is to complete the stack ADT and evaluate its functions.
Input:
•   First line will contain $T$, number of testcases. Then the testcases follow.
•   Each testcase contains integers N, M, where N is the size of the stack, and M is the number of operations in the test case. Next line will include the size, and next M lines will include series of operations of the following type Character, value, where character indicates the stack operation $P-push, O-pop, T-top, S-size, I-isempty, F-other function specified$. For push, it is followed by a value, others will not be followed by a value. The goal of delMid is to delete the middle element of the stack, while using only the stack push or pop functions. If even, the mid is the left one of the two middle elements. This function is denoted by F.

Output: For each testcase, output the result of each operation separated by a newline. The output of Push should be the content of the stack (print function given). Pop should output the popped value, top the value on top, size the size of the stack, and isEmpty should return true or false. If stack full when Push, return StackFullException case sensitive. If stack empty when pop or top, return StackEmptyException case sensitive. For delMid, output will be final stack content after the removal of middle element, and will include the content of the stack when stack functions used. Since intermediate outputs are tracked in the test cases, do not modify the driver code. Subtasks Constraints • 1 <= T <= 10 • 10 <= N <= 100 Sample Input: 5 10 S I P 4 P 2 T P 3 P 5 S P 9 P 10 Sample Output: 0 True 4 4 2 2 4 2 3 4 2 3 5 4 4 2 3 5 9 StackFullException EXPLANATION: </pre>

Template:
class MyStack():
    def __init__(self,size):
        self.stack = []

    # this is the stack container called 'stack'
    self.max_stack_size = size
    for i in range(self.max_stack_size):
        self.stack.append(None)
    # define the stack size 'max_stack_size' and initialize it
    self.t = -1
    #print(self.stack)

# define the push operation which  pushes the value into the stack, must throw a stack full exception
def push(self, value):

    return


# returns top element of stack if not empty, else throws stack empty exception
def pop(self):

    return

# returns top element without removing it if the stack is not empty, else throws exception   
def top(self):

    return

# returns True if stack is empty   
def isEmpty(self):
    return 

# returns the number of elements currently in stack 
def size(self):
    return 

# returns the number of elements currently in stack 
def delmid(self):
    return 





def printStack(self):
    if (self.isEmpty()):
        print("Empty")
    else:
        for i in range(self.max_stack_size):
            if self.stack[i]!=None:
                print(self.stack[i],end=" ")
        print()
    return

def teststack(): st1 = MyStack(stacksize) inputs=int(input()) while inputs>0: command=input() operation=command.split() if(operation[0]=="S"): print(st1.size()) elif(operation[0]=="I"): print(st1.isEmpty()) elif(operation[0]=="P"): st1.push(int(operation[1])) st1.printStack() elif(operation[0]=="O"): print(st1.pop()) st1.printStack() elif(operation[0]=="T"): print(st1.top()) st1.printStack() elif(operation[0]=="F"): print(st1.delmid()) st1.printStack()

    inputs-=1

def main(): teststack()

if __name__ == '__main__': main()


Comments

There are no comments at the moment.