码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构练习 单链表实现反转

时间:2017-02-18 18:50:47      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:span   reverse   return   val   erro   append   int   object   实现   

 1 #!/usr/bin/env python3
 2 
 3 class LNode(object):
 4         def __init__(self, elem, next_=None):
 5                 self.elem = elem
 6                 self.next = next_
 7 
 8 class ListError(ValueError):
 9         pass
10 
11 class LList(object):
12         def __init__(self):
13                 self.head = None
14                 self.num = 0
15 
16         def is_empty(self):
17                 return self.head is None
18 
19         def count(self):
20                 return self.num
21 
22         def prepend(self, elem):
23                 self.head = LNode(elem, self.head)
24                 self.num += 1
25 
26         def pop(self):
27                 if self.head is None:
28                         raise ListError
29                 e = self.head.elem
30                 self.head = self.head.next
31                 self.num -= 1
32                 return e
33 
34         def append(self, elem):
35                 if self.head is None:
36                         self.head = LNode(elem, self.head)
37                         self.num += 1
38                         return
39                 p = self.head
40                 while p.next:
41                       p = p.next
42                 p.next = LNode(elem)
43                 self.num += 1
44 
45         def pop_last(self):
46                 if self.head is None:
47                         raise ListError
48                 p = self.head
49                 if p.next is None:
50                         e = p.elem
51                         self.head = None
52                         self.num -= 1
53                         return e
54                 while p.next.next:
55                         p = p.next
56                 e = p.next.elem
57                 p.next = None
58                 self.num -= 1
59                 return e
60           
61         def bianli(self):
62                 p = self.head
63                 li = []
64                 while p:
65                         li.append(p.elem)
66                         p = p.next
67                 return li
68 
69         def reverse(self):
70                 li = self.bianli()
71                 sl = LList()
72                 for i in li:
73                         sl.prepend(i)
74                 return sl
75 
76 if __name__ == __main__:
77         l = LList()
78         l.append(1)
79   l.prepend(2) 80 l.pop_last() 81 l.append(3) 82 l.append(4) 83 l.append(5) 84 li = l.bianli() 85 print(li) 86 sl = l.reverse() 87 li = sl.bianli() 88 print(li) 8889

 结果:

[2,3,4,5]

[5,4,3,2]

 

好久没写数据结构了 回顾一下

数据结构练习 单链表实现反转

标签:span   reverse   return   val   erro   append   int   object   实现   

原文地址:http://www.cnblogs.com/xautxuqiang/p/6413748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!