0% found this document useful (0 votes)
3 views3 pages

Python Stack and Queue Implementation

This assignment focuses on implementing Stack and Queue data structures using Python. It covers basic operations for both structures, including push, pop, enqueue, and dequeue, along with sample code and output. The conclusion emphasizes the importance of these fundamental structures as a foundation for more complex data structures.

Uploaded by

adamjibrilhassan
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)
3 views3 pages

Python Stack and Queue Implementation

This assignment focuses on implementing Stack and Queue data structures using Python. It covers basic operations for both structures, including push, pop, enqueue, and dequeue, along with sample code and output. The conclusion emphasizes the importance of these fundamental structures as a foundation for more complex data structures.

Uploaded by

adamjibrilhassan
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

Assignment on Data Structures

Title:
Implementation of Stack and Queue Using Python

Objective:
To understand and implement two fundamental linear data structures — Stack and Queue
— and perform their basic operations using Python.

Introduction:
A data structure is a specialized way of organizing and storing data so it can be accessed
and modified efficiently. Two of the most common linear data structures are Stack (LIFO)
and Queue (FIFO).

Theory:
Stack operations include push(), pop(), peek(), and is_empty(). Queue operations include
enqueue(), dequeue(), and display().

Algorithm:
1. Initialize an empty list. 2. Perform stack or queue operations using list methods. 3.
Display contents accordingly.

Python Code:
# Stack Implementation
class Stack:
def __init__(self):
[Link] = []

def push(self, item):


[Link](item)
print(f"{item} pushed to stack")

def pop(self):
if [Link]:
print(f"{[Link]()} popped from stack")
else:
print("Stack is empty!")

def display(self):
print("Current Stack:", [Link][::-1])

# Queue Implementation
class Queue:
def __init__(self):
[Link] = []

def enqueue(self, item):


[Link](item)
print(f"{item} added to queue")

def dequeue(self):
if [Link]:
print(f"{[Link](0)} removed from queue")
else:
print("Queue is empty!")

def display(self):
print("Current Queue:", [Link])

# Driver Code
if __name__ == "__main__":
print("STACK OPERATIONS")
s = Stack()
[Link](10)
[Link](20)
[Link](30)
[Link]()
[Link]()
[Link]()

print("\nQUEUE OPERATIONS")
q = Queue()
[Link](1)
[Link](2)
[Link](3)
[Link]()
[Link]()
[Link]()

Sample Output:
STACK OPERATIONS
10 pushed to stack
20 pushed to stack
30 pushed to stack
Current Stack: [30, 20, 10]
30 popped from stack
Current Stack: [20, 10]

QUEUE OPERATIONS
1 added to queue
2 added to queue
3 added to queue
Current Queue: [1, 2, 3]
1 removed from queue
Current Queue: [2, 3]

Conclusion:
In this assignment, we implemented Stack and Queue using Python lists. Both are
fundamental linear data structures: Stack follows LIFO and Queue follows FIFO. These
structures form the basis for advanced data structures like trees, graphs, and heaps.

You might also like