0% found this document useful (0 votes)
10 views2 pages

Program - 8 Compiler

Uploaded by

kingadvert097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Program - 8 Compiler

Uploaded by

kingadvert097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q#8. Write a program to show the operations of stack.

class Stack:

def __init__(self):

[Link] = []

def push(self, item):

[Link](item)

def pop(self):

return [Link]() if [Link] else "Stack is empty"

def peek(self):

return [Link][-1] if [Link] else "Stack is empty"

def display(self):

return [Link]

# Demonstrate stack operations

stack = Stack()

[Link](1)

[Link](2)

[Link](3)

print("Stack after pushing 1, 2, 3:", [Link]())

print("Peek top item:", [Link]())

print("Popped item:", [Link]())

print("Stack after popping an item:", [Link]())

print("Popped item:", [Link]())

print("Popped item:", [Link]())

print("Popped item (should be empty):", [Link]())

print("Stack after popping all items:", [Link]())


Output:
Stack after pushing 1, 2, 3: [1, 2, 3]

Peek top item: 3

Popped item: 3

Stack after popping an item: [1, 2]

Popped item: 2

Popped item: 1

Popped item (should be empty): Stack is empty

Stack after popping all items: []

You might also like