File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed
Data Structures/linkedLists Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node :
2
+ def __init__ (self , data ):
3
+ self .data = data
4
+ self .next = None
5
+ def setData (self , data ):
6
+ self .data = data
7
+ def setNext (self , nextItem ):
8
+ self .next = nextItem
9
+ def getData (self ):
10
+ return self .data
11
+ def getNext (self ):
12
+ return self .next
13
+
14
+ class UnorderedList :
15
+ def __init__ (self ):
16
+ self .head = None
17
+ def __str__ (self ):
18
+ current = self .head
19
+ string = ""
20
+ while (current != None ):
21
+ string += str (current .getData ()) + ", "
22
+ current = current .getNext ()
23
+
24
+ string = string [:- 2 ]
25
+ return string
26
+ def isEmpty (self ):
27
+ return self .head == None
28
+ def add (self , data ):
29
+ temp = Node (data )
30
+ temp .setNext (self .head )
31
+ self .head = temp
32
+ def size (self ):
33
+ count = 0
34
+ current = head
35
+ while (current != None ):
36
+ count += 1
37
+ current = current .getNext ()
38
+ return count
39
+ def search (self , item ):
40
+ current = head
41
+ found = False
42
+ while (current != None and found != True ):
43
+ if (item == current .getData ()):
44
+ found = True
45
+ else :
46
+ current = current .getNext ()
47
+ return found
48
+ def remove (self , item ):
49
+ current = self .head
50
+ found = False
51
+ previous = None
52
+ while (current != None and not found ):
53
+ if current .getData () == item :
54
+ found = True
55
+ else :
56
+ previous = current
57
+ current = current .getNext ()
58
+ if previous == None and found == True :
59
+ self .head = None
60
+ elif previous != None and found == True :
61
+ previous .setNext (current .getNext ())
62
+
63
+ newList = UnorderedList ()
64
+ newList .add (3 )
65
+ newList .add (5 )
66
+ newList .add (8 )
67
+ newList .remove (3 )
68
+ print (newList )
You can’t perform that action at this time.
0 commit comments