-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.py
More file actions
135 lines (116 loc) · 3.13 KB
/
Node.py
File metadata and controls
135 lines (116 loc) · 3.13 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
# -*- coding: utf-8 -*-
class Node:
"""
This is a class of the basic building block of linked list - Node
The Node class also includes the usual methods to access and modify the
data and the next reference.
Attributes
----------
data :
This where we store the list item
next :
Holds the reference to the next Node (default is None)
"""
def __init__(self,initdata):
"""
Parameters
----------
initdata :
Initial data value for the Node
"""
self.data = initdata
self.next = None
def getData(self):
"""
Returns
-------
data
item stored in current Node
"""
return self.data
def getNext(self):
"""
Returns
-------
next
reference to the next Node
"""
return self.next
def setData(self,newdata):
"""
Parameters
----------
newdata :
this is new data to be stored in the Node
Attributes
----------
data :
this where new data item is stored
"""
self.data = newdata
def setNext(self,newnext):
"""
Parameters
----------
newnext :
this is the next node
Attributes
----------
next :
Holds the reference to the next Node
"""
self.next = newnext
class unorderedList:
"""
This is class of unordered list.
Attributes
----------
head :
Each unorderedList object will maintain a single reference to the head
object
"""
def __init__(self):
"""
Attributes
----------
head :
head holds reference to the start of the list (default is None)
"""
self.head = None
def isEmpty(self):
"""
Returns
-------
isEmpty : bool
"""
return self.head == None
def add(self,item):
"""Adds new item to the list
Parameters
----------
item :
this is the data value that will be added to the list
Attributes
----------
temp : Node
stores the newely created Node
"""
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
while current != None or not found:
if current == item:
found = True
else:
current = current.getNext()
return found