Skip to content

Commit 0a97fbc

Browse files
committed
Python Programs
1 parent c1fa0b6 commit 0a97fbc

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

Programs/P31_SinglyLinkedList.py

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,94 @@
11
#This program illustrates an example of singly linked list
22
#Linked lists do NOT support random access hence only sequential search can be carried out
33

4+
class Node(object):
5+
def __init__(self, data, Next = None):
6+
self.data = data
7+
self.next = Next
8+
9+
def getData(self):
10+
return self.data
11+
12+
def setData(self, data):
13+
self.data = data
14+
15+
def getNext(self):
16+
return self.next
17+
18+
def setNext(self, newNext):
19+
self.next = newNext
20+
421
class LinkedList(object):
5-
def __init__(self, head = None):
6-
self.head = head
22+
def __init__(self):
23+
self.head = None
724

8-
def insert(self, data):
9-
newNode = Node(data)
10-
newNode.setNext(self.head)
11-
self.head = newNode
25+
def isEmpty(self):
26+
return self.head == None
27+
28+
def add(self, element):
29+
temp = Node(element)
30+
temp.setNext(self.head)
31+
self.head = temp
1232

1333
def size(self):
1434
current = self.head
1535
count = 0
16-
while current:
17-
count += 1
36+
while current != None:
37+
count = count + 1
1838
current = current.getNext()
39+
1940
return count
2041

21-
def search(self, data):
42+
def search(self,item):
2243
current = self.head
2344
found = False
24-
while current and found != False:
25-
if current.get_data() == data:
45+
while current != None and not found:
46+
if current.getData() == item:
2647
found = True
2748
else:
2849
current = current.getNext()
29-
if current is None:
30-
raise ValueError("Data not in list")
31-
return current
3250

33-
def delete(self, data):
51+
return found
52+
53+
def remove(self,item):
3454
current = self.head
3555
previous = None
3656
found = False
37-
while current and found is False:
38-
if current.getData() == data:
57+
while not found:
58+
if current.getData() == item:
3959
found = True
4060
else:
4161
previous = current
4262
current = current.getNext()
43-
if current is None:
44-
raise ValueError("Data not in list")
45-
if previous is None:
63+
64+
if previous == None:
4665
self.head = current.getNext()
4766
else:
4867
previous.setNext(current.getNext())
4968

50-
class Node(object):
51-
''' Class for creating a singly linked list '''
52-
def __init__(self, data, next = None):
53-
''' This constructor initilizes the node with the data and the next item in the list'''
54-
self.data = data
55-
self.next = next
69+
def getAllData(self):
70+
current = self.head
71+
elements = []
72+
while current:
73+
elements.append(current.getData())
74+
current = current.getNext()
5675

57-
def getData(self):
58-
return self.data
76+
return elements
5977

60-
def getNext(self):
61-
return self.next
78+
if __name__ == '__main__':
79+
myList = LinkedList()
6280

63-
def setNext(self, newNext):
64-
self.next = newNext
81+
print(myList.head) # None
6582

66-
def replace(self, targetItem, newItem):
67-
probe = self
68-
while probe != None and targetItem != probe.data:
69-
probe = probe.next
70-
if probe != None:
71-
probe.data = newItem
72-
print('Target Updated')
73-
return True
74-
else:
75-
print('Target not found in the linked list')
76-
return False
83+
myList.add(12)
84+
myList.add(2)
85+
myList.add(22)
86+
myList.add(32)
87+
myList.add(42)
7788

78-
if __name__ == '__main__':
79-
#node1 = Node("A", "B")
80-
#node2 = Node("B", "C")
81-
#node3 = Node("C")
82-
#print(node1.data,'=>', node1.next)
83-
#print(node2.data,'=>', node2.next)
84-
#print(node3.data,'=>', node3.next)
85-
86-
#node = None
87-
#for i in range(6, 1, -1):
88-
# node = Node(i, node)
89-
90-
#print(node.data)
91-
#node.search(4)
92-
93-
#node.replace(2, 5)
94-
#print(node.data)
95-
96-
node = LinkedList(1)
97-
print(node.head)
98-
node.insert(2)
99-
print(node.head)
100-
print(node.size())
89+
print(myList.size()) # 5
90+
91+
print(myList.search(93)) # False
92+
print(myList.search(12)) # True
93+
94+
print(myList.getAllData())

0 commit comments

Comments
 (0)