-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinkedListOperationIterative.py
executable file
·134 lines (106 loc) · 2.97 KB
/
LinkedListOperationIterative.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 19:34:08 2020
@author: Cheerag
"""
class Node:
def __init__(self,data):
'''
Linked list node creation with data in input and next as None
'''
self.data = data
self.next = None
def createLinkedList(arr):
'''
This function generates the linked list
'''
if len(arr) == 0:
return None
head = Node(arr[0])
tail = head
for data in arr[1:]:
tail.next = Node(data)
tail = tail.next
return head
def lenghtOfLinkedList(head):
if head is None:
return 0
return 1+lenghtOfLinkedList(head.next)
def insertNode(head,index,data):
'''
This function inserts the nodes at any position in the linkedList
'''
l = lenghtOfLinkedList(head)
if index <0 or index > l:
return head
count = 0
current = head
prev = None
while current is not None: # This loop helps in finding the correct insert position
if count == index:
break
else:
prev = current
current = current.next
count +=1
newNode = Node(data)
if prev is None: # Insertion at beginning
newNode.next = head
head = newNode
else:
prev.next = newNode # Insertion at specific position
newNode.next = current
return head
def deleteNode(head,index):
'''
This function deletes the node at any position in the linkedList
'''
l= lenghtOfLinkedList(head)
if index <0 or index > l:
return head
current = head
count = 0
prev = None
while current is not None: # This loop helps in finding the correct index before deletion
if count == index:
break
else:
count+=1
prev = current
current = current.next
if prev is None:
head = current.next # deletion from beginning
else:
prev.next = current.next #deletion from any index
return head
def printLinkedList(head):
'''
This function prints the linkedList
'''
curr = head
while curr is not None:
print(curr.data,end="--")
curr = curr.next
print("None")
ll = list(map(int,input().split()))
head = createLinkedList(ll[:-1])
#printLinkedList(head)
flag = True
while flag:
print('Select-1:For insertion')
print('Select-2:For Deletion')
print('Select-3:For Printing Linked List')
i = int(input())
if i<0 or i>4:
flag = False
if i ==1:
#Linked list insertion
head = insertNode(head,int(input('Enter index')),int(input('Enter data')))
printLinkedList(head)
if i == 2:
#Linked list deletion
head = deleteNode(head,int(input('Enter Index to be deleted')))
printLinkedList(head)
if i == 3:
printLinkedList(head)