New Text Document
New Text Document
def __init__(self):
self.items = []
def pop(self):
"""Remove and return the top ite of the stack ."""
if not self.is_empty():
return self.items.pop()
else:
return None
def peek(self):
"""Return the top item of the stack without removing it."""
if not self.is_empty():
return self.items+[-1]
else:
return None
def is_empty(self):
"""check if the stack is empty ."""
return len(self.items) == 0
def display(self):
"""Display the stack items ."""
return self.items[:: -1]
def search_element(array,element):
"""Search for an element in the array and
return its index or a not found message."""
if element in array:
return array.index(element)
else:
return -1
class Queue:
def __init__(self):
self.items = []
def dequeue(self):
"""Remove and return the front item of the queue. """
if not self.is_empty():
return self.items.pop(0)
return None
def is_empty(self):
"""check if the queue is empty."""
return len(self.items) == 0
def display(self):
"""display the queue items.1"""
return self.items
class Node:
"""Class to represent a node in the linked list .
"""
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None # Initialize the head of the linked list
prev = None
while temp and temp.data != key:
prev = temp
temp = temp.next
if temp is None:
return
prev.next = temp.next
temp = None
def display(self):
"""Display the linked list items."""
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements
def main():
while True:
else:
print("Invalid between (a), (d) , (n) and (s)")
else:
print("invalid option. Please select (push), (Pop),
(search), (display) or (NOthing ) options")
break
else:
print("Invalid option , Please select (e), (d), (v), 0r
(n).")
if action == 'a':
new_element = input("Enter the element you want to
append: ")
linked_list.append(new_element)
print(f"{new_element} has been added to the linked
list.")
elif action == 'd':
delete_element = input("Enter the element you want to
delete: ")
linked_list.delete(delete_element)
print(f"{delete_element} has been removed from the
linked list.")
elif action == 'v':
print("Current linked list:", linked_list.display())
elif action == 'n':
print("No changes made.")
break
else:
print("Invalid option. Please select (a), (d), (v), or
(n).")
break
else:
print("invalid correct syntax, please select from 1 up to 4....")
if __name__== "__main__":
main()