-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.py
113 lines (98 loc) · 2.94 KB
/
linkedList.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
# This program implements an unordered linked list
# ref:http://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementinganUnorderedListLinkedLists.html#the-node-class
#implement a class called 'node'
class node:
def __init__(self, initVal):
self.data = initVal
self.next = None
def getVal(self):
return self.data
def setVal(self, newVal):
self.data = newVal
def getNext(self):
return self.next
def setNext(self, newNext):
self.next = newNext # class and its function definition ends here
class unorderedList:
def __init__(self):
self.head = None
size = 0
def isEmpty(self):
return self.head == None
def add(self, item):
temp = node(item)
temp.setNext(self.head)
self.head = temp
# size+=1
return True
## linkedlist traversal functions --search, size and remove
def size(self):
current = self.head
count = 0
while current!= None:
count = count+1
current = current.getNext()
return count
def printList(self):
current = self.head
# print(current.getVal())
while current!= None:
print(current.getVal())
current = current.getNext()
def search(self, item):
current = self.head
itemFound = False
while current!=None and not itemFound:
if current.getVal() == item:
itemFound = True
else:
current = current.getNext()
return itemFound
def remove(self, item):
current = self.head
previous = None
itemFound = False
while not itemFound:
if current.getVal()==item:
itemFound = True
else:
previous = current
current = current.getNext()
previous.setNext(current.getNext())
def append(self, item):
temp = node(item)
temp.setNext(None)
current = self.head
if current:
while current.getNext()!=None:
current = current.getNext()
current.setNext(temp)
else:
self.head = Node(item)
# now use the class functions
myList = unorderedList()
print(myList.add(10))
print(myList.add(11))
print(myList.add(12))
print(myList.add(22))
size_ll = myList.size()
print("Number of items in Linked list are: \t" + str(size_ll))
print("The items in the linked list are: ")
myList.printList()
print(myList.search(22))
myList.remove(10)
print("New linked list is: ")
myList.printList()
myList.append(23)
print("appending 23")
myList.printList()
# define an object of class node with name 'temp' and value '93'
# temp = node(93)
# print its value
# print(temp.getVal())
# set next node value
# temp.setNext(10)
# temp.setNext(11)
# print it
# print(temp.getNext())
# print(temp.getVal())