forked from casual-silva/practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_link.py
122 lines (101 loc) · 2.7 KB
/
single_link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- encoding: utf-8 -*-
# !/usr/bin/env python
'''
基于python实现的单向链表操作
'''
__author__ = 'billy'
class Node(object):
def __init__(self, value, next=None):
self.value = value
self.next = next
def __repr__(self):
return str(self.value)
class SingleLinkList(object):
def __init__(self):
self.length = 0
self.head = None
def init_links(self, list_data):
# 创建头结点
self.length = len(list_data)
self.head = Node(list_data[0])
p = self.head
for data in list_data[1:]:
p.next = Node(data)
p = p.next
print self.head.value, 465
def _head(self):
return self.head
def is_empty(self):
return self.head.next == 0
def getlength(self):
if self.is_empty():
return None
length = 0
p = self.head
while p:
length += 1
p = p.next
return length
def show_links(self):
if self.getlength() == 0:
return None
p = self.head
while p:
print p.value
p = p.next
def insert(self, value, index):
if self.is_empty():
return None
if index < 0 or index > self.getlength() - 1:
if self.getlength() == index:
# 调用 append 操作
self.append(value)
else:
p = self.head
pre = p
i = 0
while i <= index:
p = p.next
i += 1
pre = p
node = Node(value)
pre.next = Node(value)
node.next = p
self.length += 1
def append(self, value):
if self.getlength() == 0:
return None
p = self.head
while p:
p = p.next
p.next = Node(value)
self.length += 1
def delect(self, index):
if self.is_empty():
return None
if index < 0 or index > self.getlength() - 1:
return 'index error'
if index == 0:
self.head = self.head.next
p = self.head
i = 0
while i <= index:
p = p.next
i += 1
p.next = None
def get_data(self, index):
if index < 0 or index > self.length:
return 'index error'
p = self.head
i = 0
while i < index:
p = p.next
i += 1
return p.value
if __name__ == '__main__':
pass
a = [i for i in range(10, 50, 3)]
# link = Linklist()
link.init_links(a)
print link.getlength()
# print link.show_links()