Download code

Jump to: navigation, search

Back to Singly_linked_list_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

list.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Singly_linked_list_(Python)?oldid=18648
14 
15 from __future__ import print_function
16 
17 class ListNode:
18     def __init__(self, data, next):
19         self.data = data
20         self.next = next
21 
22 
23 class List:
24     def __init__(self):
25         self.head = None
26         self.tail = None
27     def insert(self, node, data):
28         new_node = ListNode(data, node.next)
29         node.next = new_node
30         if self.tail == node:
31             self.tail = new_node
32     def insert_end(self, data):
33         if self.tail is None:
34             new_node = ListNode(data, None)
35             self.head = self.tail = new_node
36         else:
37             self.insert(self.tail, data)
38     def insert_beginning(self, data):
39         new_node = ListNode(data, self.head)
40         self.head = new_node
41     def remove_after(self, node):
42         node.next = node.next.next
43         if node.next is None:
44             self.tail = node
45     def remove_beginning(self):
46         self.head = self.head.next
47         if self.head is None:
48             self.tail = None
49 
50     def iterate(self, func):
51         node = list.head
52         while node is not None:
53             func(node.data)
54             node = node.next
55 
56     def find_first(self, data):
57         node = list.head
58         while node is not None:
59             if node.data == data:
60                 return node
61             node = node.next
62     def find_first_func(self, func):
63         node = list.head
64         while node is not None:
65             if func(node.data):
66                 return node
67             node = node.next
68 
69 list = List()
70 for x in range(1,10):
71     list.insert_end(x)
72 
73 node = list.head
74 while node is not None:
75     print(node.data)
76     node = node.next
77 print()
78 
79 list.iterate(lambda x: print(x))
80 print()
81 
82 list.insert(list.find_first(7), 13)
83 list.iterate(lambda x: print(x))
84 print()
85 
86 list.remove_after(list.find_first(7))
87 list.iterate(lambda x: print(x))
88 print()
89 
90 list.insert_beginning(-5)
91 list.iterate(lambda x: print(x))
92 print()
93 
94 list.remove_beginning()
95 list.iterate(lambda x: print(x))
96 print()
97 
98 node = list.find_first_func(lambda x: 2*x + 7 == 19)
99 print(node.data)


hijacker
hijacker
hijacker
hijacker