-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL-class.py
More file actions
89 lines (80 loc) · 2.46 KB
/
SLL-class.py
File metadata and controls
89 lines (80 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
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):
if not self.head:
print("List is empty")
return
current = self.head
print("List:", end=" ")
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
def insert_at_start(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def delete_node(self, key):
if not self.head:
print("List is empty")
return
if self.head.data == key:
self.head = self.head.next
print(f"Deleted node with data: {key}")
return
current = self.head
while current.next:
if current.next.data == key:
current.next = current.next.next
print(f"Deleted node with data: {key}")
return
current = current.next
print(f"Node with data {key} not found")
def search(self, key):
current = self.head
while current:
if current.data == key:
return True
current = current.next
return False
sll = SinglyLinkedList()
while True:
print("\n1. Append 2. Insert at Start 3. Delete Node 4. Display 5. Search 6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
data = int(input("Enter the data to append: "))
sll.append(data)
elif choice == 2:
data = int(input("Enter the data to insert at start: "))
sll.insert_at_start(data)
elif choice == 3:
data = int(input("Enter the data to delete: "))
sll.delete_node(data)
elif choice == 4:
sll.display()
elif choice == 5:
data = int(input("Enter the data to search: "))
found = sll.search(data)
if found:
print(f"Node with data {data} found.")
else:
print(f"Node with data {data} not found.")
elif choice == 6:
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")