Stack using Array

 class stackarray:

    def __init__(self):

        self.stack=[]


    def is_empty(self):

        return len(self.stack)==0


    def push(self,item):

        self.stack.append(item)


    def pop(self):

        if self.is_empty():

            return "stack is empty"

        return self.stack.pop()


    def peek(self):

        if self.is_empty():

            return "stack is empty"

        return self.stack[-1]



stack = stackarray()

stack.push(10)

stack.push(20)

stack.push(30)

print(stack.pop())

print(stack.peek())



Output:-

30
20





Post a Comment

0 Comments