class Node:
def __init__(self, data):
[Link] = data
[Link] = None
class LinkedList:
def __init__(self):
[Link] = None
def append(self, data):
new_node = Node(data)
if not [Link]:
[Link] = new_node
return
last = [Link]
while [Link]:
last = [Link]
[Link] = new_node
def display(self):
current = [Link]
if not current:
print("List is empty.")
return
while current:
print([Link], end=" -> ")
current = [Link]
print("None")
def delete(self, key):
current = [Link]
if current and [Link] == key:
[Link] = [Link]
current = None
return
prev = None
while current and [Link] != key:
prev = current
current = [Link]
if current is None:
print("Element not found.")
return
[Link] = [Link]
current = None
class Stack:
def __init__(self):
[Link] = []
def push(self, data):
[Link](data)
def pop(self):
if not self.is_empty():
return [Link]()
else:
print("Stack is empty.")
return None
def display(self):
if self.is_empty():
print("Stack is empty.")
else:
print("Stack elements: ", end="")
print(*[Link])
def is_empty(self):
return len([Link]) == 0
class Queue:
def __init__(self):
[Link] = []
def enqueue(self, data):
[Link](data)
def dequeue(self):
if not self.is_empty():
return [Link](0)
else:
print("Queue is empty.")
return None
def display(self):
if self.is_empty():
print("Queue is empty.")
else:
print("Queue elements: ", end="")
print(*[Link])
def is_empty(self):
return len([Link]) == 0
def main():
linked_list = LinkedList()
stack = Stack()
queue = Queue()
while True:
print("\nMenu:")
print("1. Linked List Operations")
print("2. Stack Operations")
print("3. Queue Operations")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
print("\nLinked List Operations:")
print("1. Append to Linked List")
print("2. Display Linked List")
print("3. Delete from Linked List")
print("4. Back to Main Menu")
ll_choice = int(input("Enter your choice: "))
if ll_choice == 1:
data = int(input("Enter data to append: "))
linked_list.append(data)
elif ll_choice == 2:
linked_list.display()
elif ll_choice == 3:
data = int(input("Enter data to delete: "))
linked_list.delete(data)
elif ll_choice == 4:
continue
else:
print("Invalid choice. Try again.")
elif choice == 2:
print("\nStack Operations:")
print("1. Push to Stack")
print("2. Pop from Stack")
print("3. Display Stack")
print("4. Back to Main Menu")
stack_choice = int(input("Enter your choice: "))
if stack_choice == 1:
data = int(input("Enter data to push: "))
[Link](data)
elif stack_choice == 2:
popped_data = [Link]()
if popped_data is not None:
print(f"Popped: {popped_data}")
elif stack_choice == 3:
[Link]()
elif stack_choice == 4:
continue
else:
print("Invalid choice. Try again.")
elif choice == 3:
print("\nQueue Operations:")
print("1. Enqueue to Queue")
print("2. Dequeue from Queue")
print("3. Display Queue")
print("4. Back to Main Menu")
queue_choice = int(input("Enter your choice: "))
if queue_choice == 1:
data = int(input("Enter data to enqueue: "))
[Link](data)
elif queue_choice == 2:
dequeued_data = [Link]()
if dequeued_data is not None:
print(f"Dequeued: {dequeued_data}")
elif queue_choice == 3:
[Link]()
elif queue_choice == 4:
continue
else:
print("Invalid choice. Try again.")
elif choice == 4:
print("Exiting the program.")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()