Basic Stack Implementation
Submit solution
C++, Java, Python
Points:
20 (partial)
Time limit:
10.0s
Memory limit:
64M
Authors:
Problem type
Allowed languages
Problem Statement
You are expected to implement the basic functionality of a stack data structure. The operations involved are as follows:
I - checks for stack empty condition
S - gives the current size of the stack
P 1 - pushes 1 onto the stack
O - pops an element from the stack
T - returns the top of the stack
and so on.
Input format:
First line contains the size of the stack and second line reads the number of operations on the stack followed by commands in each line.
Output format:
The output for each command should be displayed.
Sample Input
5
10
I
S
P 6
P 5
P 4
P 3
P 2
P 1
T
S
Sample Output
True
0
6
6 5
6 5 4
6 5 4 3
6 5 4 3 2
Stack Full Exception
6 5 4 3 2
2
5
"""The code has been developed by Dr.Vidhya Balasubramanian as part of the CSE230 Data Structures Course, ASE Coimbatore.
The code is being constantly improved, so please bring to notice any bugs to dsdaa.amrita@gmail.com
Modified and updated by: M. Vamsee Krishna Kiran.
"""
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
# define the push operation which pushes the value into the stack, must throw a stack full exception
def push(self, value):
if (self.size() == self.max_stack_size):
print("Stack Full Exception")
else:
#your code here.
return
# returns top element of stack if not empty, else throws stack empty exception
def pop(self):
if (self.size() == 0):
print("Stack Empty Exception")
return
else:
#your code here
return toret
# returns top element without removing it if the stack is not empty, else throws exception
def top(self):
if (self.size() == 0):
print ("Stack Empty Exception")
return
else:
return self.stack[self.t]
# returns True if stack is empty
def isEmpty(self):
return (self.t<0)
# returns the number of elements currently in stack
def size(self):
return self.t+1
def printStack(self):
if (self.isEmpty()):
print ("Stack Empty Exception")
else:
for i in range(self.max_stack_size):
if self.stack[i]!=None:
print(self.stack[i],end=" ")
print(" ")
def flushstack(self):
while(not self.isEmpty()):
self.pop()
# Driver code.---------------------------------------------
def teststack():
testcases=int(input())
stacksize=int(input())
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]))
elif(operation[0]=="O"):
print(st1.pop())
st1.printStack()
elif(operation[0]=="T"):
print(st1.top())
elif(operation[0]=="D"):
print(st1.stack)
elif(operation[0]=="F"):
st1.delMid()
inputs-=1
def main():
teststack()
if __name__ == '__main__':
main()
Comments