python16_day40【数据结构】

一、链表

 

 1 #!/usr/bin/env python
 2 # -*-coding:utf8-*-
 3 __author__ = "willian"
 4 
 5 
 6 # 一、简单链表
 7 
 8 class Node(object):
 9     def __init__(self, item):
10         self.item = item
11         self.next = Node
12 
13 
14 a = Node(10)
15 b = Node(20)
16 c = Node(30)
17 
18 a.next = b
19 b.next = c
20 
21 
22 # print(a.next.item)
23 # 遍历链表
24 
25 def traversal(head):
26     curr_node = head
27     while curr_node is not Node:
28         print(curr_node.item)
29         curr_node = curr_node.next
30 
31 
32 traversal(b)
33 
34 # 二、链表节点的插入和删除(插入和删除比列表快)
35 
36 # 插入
37 """
38     p.next = curr_node.next
39     curr_node.next = p
40 """
41 
42 # 删除
43 """
44     p = curr_node.next
45     curr_node.next = p.next
46     del p
47 """
48 
49 
50 # 新建链表
51 # 1.头插发
52 
53 def create_link_list_left(li):
54     ll = Node()
55     for num in li:
56         s = Node(num)
57         s.next = ll.next
58         ll.next = s
59     return ll
60 
61 
62 def create_link_list_right(li):
63     ll = Node()
64     r = ll
65     for num in li:
66         s = Node(num)
67         r.next = s
68         r = s

 

posted @ 2017-11-14 20:38  willianflasky  阅读(114)  评论(0编辑  收藏  举报