-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearLinkList.py
More file actions
155 lines (141 loc) · 5.73 KB
/
LinearLinkList.py
File metadata and controls
155 lines (141 loc) · 5.73 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
###########################
# Author: Hemant Tripathi #
###########################
class Node:
def __init__(self, data = None):
self.data = data
self.next = None
class LinearLinkList:
def __init__(self):
self.header = None
def main():
linkList = LinearLinkList()
print('Starting LinearLinkList operations.................')
while(1):
print('Please select an option:')
print('1. Add a node to the beginning of the list')
print('2. Add a node into the middle of the list')
print('3. Add a node into the end of the list')
print('4. Remove a node from the beginning of the list')
print('5. Remove a node from the middle of the list')
print('6. Remove a node from the end of the list')
print('7. Show all nodes of the list')
print('8. Exit')
selection = int(input())
if selection == 1:
print('Enter the data into new node')
data = input()
if(linkList.header == None):
print('No Linklist found. Creating first node of list')
linkList.header = Node(data)
print('Linklist first node created')
else:
newNode = Node(data)
newNode.next = linkList.header
linkList.header = newNode
print('Added new node at the beginning of list')
elif selection == 2:
print('Enter the data into new node')
data = input()
if(linkList.header == None):
print('No Linklist found. Creating first node of list')
linkList.header = Node(data)
print('Linklist first node created')
else:
newNode = Node(data)
print('Enter the position of this node in the list')
limitException = False
position = int(input())
if(position > 0):
tmpNode = linkList.header
for counter in range(0, position-1):
if tmpNode.next is None:
limitException = True
print('Error: IndexOutOfBound, position must be less than linklist range')
break
tmpNode = tmpNode.next
if limitException == False:
newNode.next = tmpNode.next
tmpNode.next = newNode
print('New Node is inserted into link list')
else:
print('Node position must be greater than zero')
elif selection == 3:
print('Enter the data into new node')
data = input()
if(linkList.header == None):
print('No Linklist found. Creating first node of list')
linkList.header = Node(data)
print('Linklist first node created')
else:
newNode = Node(data)
tmpNode = linkList.header
count = 0;
while tmpNode.next is not None:
count = count + 1
tmpNode = tmpNode.next
tmpNode.next = newNode
print('Added new node at position : ', count)
elif selection == 4:
print('Removing the node from the beginning of the list')
if linkList.header is None:
print('No linklist found!')
elif linkList.header.next is None:
linkList.header = None
else:
tmp = linkList.header
linkList.header = linkList.header.next
tmp.next = None
del tmp
print('Removed the first node from linkList')
elif selection == 5:
if linkList.header is None:
print('No linklist found!')
else:
limitException = False
tmp = linkList.header
tmp1 = linkList.header.next
print('Enter the position of the node you want to remove')
position = int(input())
for count in range(0, position-1):
if tmp1 is None:
limitException = True
print('Error: IndexOutOfBound, position must be less than linklist range')
break
tmp = tmp.next
tmp1 = tmp1.next
tmp.next = tmp1.next
tmp1.next = None
del tmp1
elif selection == 6:
print('Removing the node from the end of the list')
if linkList.header is None:
print('No linklist found!')
else:
tmp = linkList.header
tmp1 = linkList.header.next
while tmp1.next is not None:
tmp = tmp.next
tmp1 = tmp1.next
tmp.next = None
del tmp1
elif selection == 7:
print('Calculating all nodes of the link list')
if linkList.header is None:
print('No linklist found!')
else:
tmpNode = linkList.header
count = 0;
while tmpNode is not None:
count = count + 1
print('Next node Value: '+tmpNode.data+' At position: ', count)
tmpNode = tmpNode.next
print('Total Nodes : ', count)
elif selection == 8:
print('########### Terminating the program ##############')
del linkList
break
else:
print('Invalid input. Please enter a number between 1 to 8')
if __name__ == "__main__":
main()