0% found this document useful (0 votes)
9 views

Data Structure

The document contains Python code for implementing a Linked List, Stack, and Queue data structures, along with their respective operations. It includes methods for appending, displaying, and deleting elements in the linked list, as well as pushing, popping, and displaying elements in the stack, and enqueueing, dequeueing, and displaying elements in the queue. A main function provides a menu-driven interface for users to interact with these data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Data Structure

The document contains Python code for implementing a Linked List, Stack, and Queue data structures, along with their respective operations. It includes methods for appending, displaying, and deleting elements in the linked list, as well as pushing, popping, and displaying elements in the stack, and enqueueing, dequeueing, and displaying elements in the queue. A main function provides a menu-driven interface for users to interact with these data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

class Node:

def __init__(self, data):


self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

def append(self, data):


new_node = Node(data)
if not self.head:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node

def display(self):
current = self.head
if not current:
print("List is empty.")
return
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
def delete(self, key):
current = self.head
if current and current.data == key:
self.head = current.next
current = None
return

prev = None
while current and current.data != key:
prev = current
current = current.next

if current is None:
print("Element not found.")
return

prev.next = current.next
current = None

class Stack:
def __init__(self):
self.stack = []

def push(self, data):


self.stack.append(data)

def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
print("Stack is empty.")
return None

def display(self):
if self.is_empty():
print("Stack is empty.")
else:
print("Stack elements: ", end="")
print(*self.stack)

def is_empty(self):
return len(self.stack) == 0

class Queue:
def __init__(self):
self.queue = []

def enqueue(self, data):


self.queue.append(data)

def dequeue(self):
if not self.is_empty():
return self.queue.pop(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(*self.queue)

def is_empty(self):
return len(self.queue) == 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: "))
stack.push(data)
elif stack_choice == 2:
popped_data = stack.pop()
if popped_data is not None:
print(f"Popped: {popped_data}")
elif stack_choice == 3:
stack.display()
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: "))
queue.enqueue(data)
elif queue_choice == 2:
dequeued_data = queue.dequeue()
if dequeued_data is not None:
print(f"Dequeued: {dequeued_data}")
elif queue_choice == 3:
queue.display()
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()

You might also like