-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linklist.py
221 lines (169 loc) · 4.8 KB
/
Linklist.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- coding:utf-8 -*-
import Gamma
class Node(object):
def __init__(self, val, p=0):
self.docno = val[0]
self.tf = val[1]
self.next = p
class LinkList(object):
def __init__(self):
self.head = 0
def __getitem__(self, key):
if self.is_empty():
print('linklist is empty.')
return
elif key < 0 or key > self.getlength():
print('the given key is error')
return
else:
return self.getitem(key)
def __setitem__(self, key, value):
if self.is_empty():
print('linklist is empty.')
return
elif key < 0 or key > self.getlength():
print('the given key is error')
return
else:
self.delete(key)
return self.insert(key)
def initlist(self, data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
node = Node(i)
p.next = node
p = p.next
def getlength(self):
p = self.head
length = 0
while p != 0:
length += 1
p = p.next
return length
def is_empty(self):
if self.getlength() == 0:
return True
else:
return False
def clear(self):
self.head = 0
def append(self, item):
pre = self.head
curr = self.head
temp = item[0]
if self.head == 0:
self.head = Node(item)
return
else:
val = curr.docno
curr = curr.next
while curr != 0:
val += Gamma.__gammaUncompress__(curr.docno)
pre = curr
curr = curr.next
# val += Gamma.__gammaUncompress__(curr.docno)
item[0] = Gamma.__gamma__(temp - val)
pre.next = Node(item)
def getitem(self, index, double=False):
if self.is_empty():
print('Linklist is empty.')
return
j = 0
p = self.head
while p.next != 0 and j < index:
p = p.next
j += 1
if j == index:
if not double:
return p.docno
elif double:
return p.docno, p.tf
else:
print ('target is not exist!')
def insert(self, index, item):
if self.is_empty() or index < 0 or index > self.getlength():
print ('Linklist is empty.')
return
if index == 0:
q = Node(item, self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next != 0 and j < index:
post = p
p = p.next
j += 1
if index == j:
q = Node(item, p)
post.next = q
q.next = p
def delete(self, index):
if self.is_empty() or index < 0 or index > self.getlength():
print('Linklist is empty. or Index Error')
return
if index == 0:
self.head = self.head.next
# if index ==0:
# q = Node(item,self.head)
# self.head = q
p = self.head
post = self.head
j = 0
while p.next != 0 and j < index:
post = p
p = p.next
j += 1
if index == j:
post.next = p.next
def index(self, value):
if self.is_empty():
print('Linklist is empty.')
return
p = self.head
i = 0
while p.next != 0 and not p.docno == value:
p = p.next
i += 1
if p.docno == value:
return i
else:
return -1
def increase(self, docno):
if self.is_empty():
# print('Linklist is empty.')
return False
p = self.head
docno_val = p.docno
while p.next != 0 and not docno_val == docno: # 匹配docno
p = p.next
docno_val += Gamma.__gammaUncompress__(p.docno) # 解压?没懂
if docno_val == docno:
p.tf += 1 # 词项频率加一
return True
else:
return False
def output(self):
p = self.head
if p == 0:
print('empty')
s = ''
count = p.docno
s += str(count)+','+str(p.tf)+'|'
p = p.next
while p != 0:
# print(count)
count += Gamma.__gammaUncompress__(p.docno)
s += str(count)+','+str(p.tf)+'|'
p = p.next
return s
#
# list_test = LinkList()
# data = [['a',1],['b',2],['c',1]]
# list_test.initlist(data)
# print(list_test.getitem(2,True))
# list_test.increase('c')
# print(list_test.getitem(2,True))
# list_test.append(['d',2])
# print(list_test.output())