-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.py
executable file
·148 lines (129 loc) · 3.58 KB
/
linkedlist.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# 节点
class Node(object):
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
# 反转单链表
def reversed(head):
# 设置向前指针
# 每次遍历更改head指针和prev指针
# 当head指针指向之前链表null时,返回prev即为head
prev = None
while head:
tmp = head.next # 保存当前next指针
head.next = prev
prev = head
head = tmp
return tmp
# 反转单链表
def reverse(head):
if not head or not head.next:
return head
prev = None
while head:
tmp = head.ne
tmp = head.next # 缓存当前节点向后指针
head.next = prev # 反转关键
prev = head
head = tmp
return prev
# SingleLinkedList
class SingleLinkedList(object):
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
# 从链表头部插入
def add(self, item):
tmp = Node(item)
tmp.setNext(self.head)
self.head = tmp
# 从链表尾部插入
def append(self, item):
tmp = Node(item)
if self.isEmpty():
self.head = tmp
else:
current = self.head
while current.getNext() != None:
current = current.getNext()
current.setNext(tmp)
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def index(self, item):
current = self.head
count = 0
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
count = count + 1
if found:
return count
else:
raise ValueError, "%s is not in the list" % item
# remove方法 考虑 pre指针
def remove(self, item):
current = self.head
pre = None
found = False
while not found:
if current.getData() == item:
found = True
else:
pre = current
current = current.getNext()
if pre == None:
self.head = current.getNext()
else:
pre.setNext(current.getNext())
# insert
def insert(self, pos, item):
if pos == self.size():
self.append(item)
else:
tmp = Node(item)
count = 1
pre = None
current = self.head
while count <= pos and current != None:
if count == pos:
pre = current.getNext()
current.setNext(tmp)
tmp.setNext(pre)
else:
current = current.getNext()
count = count + 1
self.size = self.size + 1
if __name__ == '__main__':
l = SingleLinkedList()
l.add(3)
l.add(4)
l.append(5)
print l.index(6)