-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy59_linkedlist_1.py
More file actions
101 lines (86 loc) · 3.15 KB
/
py59_linkedlist_1.py
File metadata and controls
101 lines (86 loc) · 3.15 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
90
91
92
93
94
95
96
97
98
99
100
101
class Node:
def __init__(self, data):
# Constructor to initialize node with data and next as None
self.data = data
self.next = None
def insertatHead(head, tail, data):
# Inserting a node at the head (beginning) of the list
newNode = Node(data)
if head is None: # If the list is empty
head = newNode
tail = newNode
return head, tail
newNode.next = head # Link the new node to the current head
head = newNode # Move the head to the new node
return head, tail
def insertatEnd(head, tail, data):
# Inserting a node at the tail (end) of the list
newNode = Node(data)
if head is None: # If the list is empty
head = newNode
tail = newNode
return head, tail
tail.next = newNode # Link the current tail to the new node
tail = newNode # Move the tail to the new node
return head, tail
def insertatMid(head, tail, position, data):
# Inserting a node at a given position (middle) of the list
if position == 1: # If position is 1, insert at the head
return insertatHead(head, tail, data)
temp = head
p = 1
while p < position - 1: # Why position -1: Go to the node before the target position
temp = temp.next # Traverse the list until reaching the target position
p += 1
# If we enter the last position (e.g., if the length is 5 and we entered position 6)
if temp.next is None:
return insertatEnd(head, tail, data)
newNode = Node(data)
newNode.next = temp.next # Link new node to the next node in the list
temp.next = newNode # Link the current node to the new node
return head, tail
def deleteAtStart(head, pos):
# Delete function (placeholder) can be implemented similarly
pass
def display(head):
# Display the entire linked list
temp = head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
print()
def find(a, head):
# Search for a specific element in the linked list
temp = head
tf = False
while temp is not None:
if temp.data == a: # If the data matches the element
tf = True
return tf
temp = temp.next
return tf
# Main execution starts here
if __name__ == "__main__":
head = None
tail = None
print("AT HEAD")
for i in range(5):
data = int(input()) # Input data to insert at the head
head, tail = insertatHead(head, tail, data)
print("AT TAIL")
for i in range(5):
data = int(input()) # Input data to insert at the tail
head, tail = insertatEnd(head, tail, data)
display(head) # Display the linked list
print("Position to insert: ", end="")
position = int(input())
head, tail = insertatMid(head, tail, position, 200) # Insert at a specific position
display(head) # Display the updated linked list
print(head.data) # Display head data
print(tail.data) # Display tail data
print("Element to search: ", end="")
a = int(input()) # Input the element to search in the list
if find(a, head):
print("Present")
else:
print("Not present")