(a). STACK IMPLEMENTATION USING LIST
EX No: 1(a)
DATE:
AIM:
To write a python program for stack implementation using listin python.
ALGORITHM:
Step 1 : Start
Step 2 : Push elements into a stack
Step 3 : Pop elements from a stack and issue a warning if it's empty
Step 4 : Get the size of the stack to print it
Step 5 : Stop
PROGRAM:
class Stack:
def __init__(self):
self.stack = list()
def push(self,data):
if data not in self.stack:
self.stack.append(data)
return True
return False
def pop(self):
if len(self.stack)<=0:
return ("Stack Empty!")
return self.stack.pop()
, def size(self):
return len(self.stack)
myStack = Stack()
print(myStack.push(5))
print(myStack.push(6))
print(myStack.push(9))
print(myStack.push(5))
print(myStack.push(3))
print(myStack.size())
print(myStack.pop())
print(myStack.pop())
print(myStack.pop())
print(myStack.pop())
print(myStack.size())
print(myStack.pop())
SAMPLE OUTPUT:
True
True
True
False
True
4
3
9
6