Python Exp6
Python Exp6
Experiment No- 6
Aim: To Study and implement Data Structure using built in function for Linked list,
Stack and Queue.
Theory:
Data structures are ways of organizing and storing data efficiently. Python provides built-in data
structures that help in managing and manipulating data effectively.
1) Linked list: A linked list is a linear data structure where elements (nodes) are connected using
pointers. Unlike arrays, linked lists do not have a fixed size and can dynamically grow or shrink.
ii) Doubly Linked List - Each node points to both the previous and next nodes.
Each node contains:
• Data
• Pointer to the next node
• Pointer to the previous node
Syntax- class DNode:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
# Add node at the end
pass
def display(self):
# Traverse and print the list
pass
def delete(self, key):
# Delete a node by value
Pass
iii) Circular Linked List - The last node connects back to the first node.
Structure:
• The last node points to the first node, forming a circle.
• Can be singly or doubly linked.
Syntax- class CNode:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def append(self, data):
# Add node at the end
Pass
def display(self):
# Traverse and print the circular list
pass
2) Stack: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This
means that the last element added is the first one to be removed.
Operations in a Stack-
a. Push – Add an element to the top.
b. Pop – Remove the top element.
c. Peek (Top) – Get the top element without removing it.
d. isEmpty – Check if the stack is empty.
Syntax:
# 1. Using List
stack = []
stack.append(data) # Push
stack.pop() # Pop
top = stack[-1] # Peek
is_empty = len(stack) == 0 # Check if empty
# 2. Using collections.deque
from collections import deque
stack = deque()
stack.append(data) # Push
stack.pop() # Pop
top = stack[-1] # Peek
is_empty = len(stack) == 0 # Check if empty
3) Queue: A queue is a linear data structure that follows the First In, First Out (FIFO) principle.
This means that the first element added is the first one to be removed.
Types of Queues:
a) Simple Queue (FIFO) – Elements are added at the rear and removed from the front.
b) Circular Queue – The rear connects back to the front for efficient memory usage.
c) Deque (Double-Ended Queue) – Insertions and deletions can happen at both ends.
Syntax-
queue = []
queue.append(data) # Enqueue
queue.pop(0) # Dequeue
queue = deque()
queue.append(data) # Enqueue
queue.popleft() # Dequeue
q = Queue()
q.put(data) # Enqueue
q.get() # Dequeue
pq = PriorityQueue()
# Menu Function
def menu():
while True:
print("\nData Structure Operations:")
print("1. Linked List")
print("2. Stack")
print("3. Queue")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
linked_list_operations()
elif choice == '2':
stack_operations()
elif choice == '3':
queue_operations()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice! Try again.")
# Stack Operations
def stack_operations():
while True:
print("\nStack Operations:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Back to Main Menu")
choice = input("Enter your choice: ")
if choice == '1':
data = input("Enter element: ")
stack.append(data)
elif choice == '2':
if stack:
print("Popped element:", stack.pop())
else:
print("Stack is empty!")
elif choice == '3':
if stack:
print("Top element:", stack[-1])
else:
print("Stack is empty!")
elif choice == '4':
print("Stack:", list(stack))
elif choice == '5':
break
else:
print("Invalid choice! Try again.")
# Queue Operations
def queue_operations():
while True:
print("\nQueue Operations:")
print("1. Enqueue")
print("2. Dequeue")
print("3. Peek")
print("4. Display")
print("5. Back to Main Menu")
choice = input("Enter your choice: ")
if choice == '1':
data = input("Enter element: ")
queue.append(data)
elif choice == '2':
if queue:
print("Dequeued element:", queue.popleft())
else:
print("Queue is empty!")
elif choice == '3':
if queue:
print("Front element:", queue[0])
else:
print("Queue is empty!")
elif choice == '4':
print("Queue:", list(queue))
elif choice == '5':
break
else:
print("Invalid choice! Try again.")
Output:
PS C:\C Programming> & C:/Users/asha/anaconda3/python.exe "c:/C
Programming/.vscode/Python/ds.py"
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 1
Enter element: 67
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 1
Enter element: 83
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 2
Popped element: 83
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 3
Top element: 67
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 4
Stack: ['50', '67']
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 5
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 1
Enter element: 66
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 1
Enter element: 83
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 1
Enter element: 72
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 2
Dequeued element: 66
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 3
Front element: 83
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice: 4
Queue: ['83', '72']
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Back to Main Menu
Enter your choice:5