|
1 | 1 | #This program illustrates an example of singly linked list |
2 | 2 | #Linked lists do NOT support random access hence only sequential search can be carried out |
3 | 3 |
|
| 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 | + |
4 | 21 | class LinkedList(object): |
5 | | - def __init__(self, head = None): |
6 | | - self.head = head |
| 22 | + def __init__(self): |
| 23 | + self.head = None |
7 | 24 |
|
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 |
12 | 32 |
|
13 | 33 | def size(self): |
14 | 34 | current = self.head |
15 | 35 | count = 0 |
16 | | - while current: |
17 | | - count += 1 |
| 36 | + while current != None: |
| 37 | + count = count + 1 |
18 | 38 | current = current.getNext() |
| 39 | + |
19 | 40 | return count |
20 | 41 |
|
21 | | - def search(self, data): |
| 42 | + def search(self,item): |
22 | 43 | current = self.head |
23 | 44 | 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: |
26 | 47 | found = True |
27 | 48 | else: |
28 | 49 | current = current.getNext() |
29 | | - if current is None: |
30 | | - raise ValueError("Data not in list") |
31 | | - return current |
32 | 50 |
|
33 | | - def delete(self, data): |
| 51 | + return found |
| 52 | + |
| 53 | + def remove(self,item): |
34 | 54 | current = self.head |
35 | 55 | previous = None |
36 | 56 | found = False |
37 | | - while current and found is False: |
38 | | - if current.getData() == data: |
| 57 | + while not found: |
| 58 | + if current.getData() == item: |
39 | 59 | found = True |
40 | 60 | else: |
41 | 61 | previous = current |
42 | 62 | 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: |
46 | 65 | self.head = current.getNext() |
47 | 66 | else: |
48 | 67 | previous.setNext(current.getNext()) |
49 | 68 |
|
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() |
56 | 75 |
|
57 | | - def getData(self): |
58 | | - return self.data |
| 76 | + return elements |
59 | 77 |
|
60 | | - def getNext(self): |
61 | | - return self.next |
| 78 | +if __name__ == '__main__': |
| 79 | + myList = LinkedList() |
62 | 80 |
|
63 | | - def setNext(self, newNext): |
64 | | - self.next = newNext |
| 81 | + print(myList.head) # None |
65 | 82 |
|
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) |
77 | 88 |
|
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