File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from node import Node
2+
3+
4+ class LinkedList (object ):
5+ def __init__ (self ):
6+ self .head_ = None
7+
8+ def set_head (self , head_node ):
9+ self .head_ = head_node
10+
11+ def __len__ (self ):
12+ count = 0
13+ current = self .head_
14+ while current :
15+ count += 1
16+ current = current .get_next ()
17+ return count
18+
19+ def __str__ (self ):
20+ current = self .head_
21+ output = ""
22+ while current :
23+ output += str (current ) + " -> "
24+ current = current .get_next ()
25+ return output
26+
27+ # Pops an item from the front of the list
28+ def pop (self ):
29+ if self .head_ :
30+ self .head_ = self .head_ .get_next ()
31+ else :
32+ raise IndexError ("Unable to pop from empty list" )
33+
34+ # Returns true if list contains the given value.
35+ def contains (self , value ):
36+ found = False
37+ current = self .head_
38+ while current and not found :
39+ if current .get_data () == value :
40+ found = True
41+ else :
42+ current = current .get_next ()
43+ return found
44+
45+ # Deletes all instances of given value in list.
46+ def delete (self , value ):
47+ current = self .head_
48+ prev = None
49+ while current :
50+ if current .get_data () == value :
51+ if prev :
52+ prev .set_next (current .get_next ())
53+ else :
54+ self .head_ = current .get_next ()
55+ break
56+ else :
57+ prev = current
58+ current = current .get_next ()
59+
60+ # Pushes an item on the front of the list.
61+ def push (self , value ):
62+ node = Node (value )
63+ node .set_next (self .head_ )
64+ self .set_head (node )
65+
66+ def append (self , value ):
67+ node = Node (value )
68+ current = self .head_
69+ if not current :
70+ self .head_ = node
71+ return
72+
73+ while current .get_next ():
74+ current = current .get_next ()
75+
76+ current .set_next (node )
Original file line number Diff line number Diff line change 1+ from linked_list import LinkedList
2+ # from node import Node
3+
4+
5+ def main ():
6+ ll = LinkedList ()
7+
8+ print ("Initial size: " , len (ll ))
9+ ll .push (24 )
10+ print ("New size: " , len (ll ))
11+ print ("List content: " , ll )
12+ print ("Pushing more" )
13+ ll .push (6 )
14+ ll .push (783 )
15+ print ("List content: " , ll )
16+ print ("popping..." )
17+ ll .pop ()
18+ print ("List content: " , ll )
19+ print ("Does list contain 24?" )
20+ if ll .contains (24 ):
21+ print ("Yes" )
22+ else :
23+ print ("No" )
24+ print ("Deleting 24" )
25+ ll .delete (24 )
26+ print ("List content: " , ll )
27+ print ("Pushing another onto end." )
28+ ll .append (365 )
29+ print ("List content: " , ll )
30+
31+
32+ if __name__ == "__main__" :
33+ main ()
Original file line number Diff line number Diff line change 1+ class Node (object ):
2+ def __init__ (self , data = None , next = None ):
3+ self .data_ = data
4+ self .next_ = next
5+
6+ def get_data (self ):
7+ return self .data_
8+
9+ def set_data (self , data ):
10+ self .data_ = data
11+
12+ def get_next (self ):
13+ return self .next_
14+
15+ def set_next (self , next ):
16+ self .next_ = next
17+
18+ def __str__ (self ):
19+ return str (self .data_ )
You can’t perform that action at this time.
0 commit comments