-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.py
177 lines (158 loc) · 3.78 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
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
# -*- coding: utf-8 -*-
import numpy as np
import random
#conclusion: the most important thing in the linked list structure
#is the head, and it is a little like recurrence
class Node(object):
'''each node contains an element and the pointer
direct to the next element.
'''
def __init__(self, elem, pointer = None):
self.elem = elem
self.pointer = pointer
def length(self, point): #point is an example of class Node, and it let me think of recurrence
'''the length of of the linked list'''
count, node = 0, point
while node is not None:
count = count+1
node = node.pointer
return count
class linkedlist(object):
def __init__(self):
# super(linkedlist,self).__init__()
self._header = None
def is_empty(self):
'''check if it is empty'''
if self._header is None:
return 'the list is empty'
else:
return 'the list is not empty'
def prepend(self, elem):
'''append an element in the head'''
self._header = Node(elem, self._header)
def endpend(self, elem):
'''append an element in the end'''
if self._header is None:
self._header = Node(elem)
return
p = self._header
while p.pointer is not None:
p = p.pointer
p.pointer = Node(elem) #p.pointer.pointer = None
def pop(self): #pop the first element
if self._header is None:
print "the list is empty"
e = self._header.elem
self._header = self._header.pointer
return e
def pop_last(self):
if self._header is None:
print "the list is empty"
p = self._header
if p.pointer is None: #If only one element
e = p.elem
self._header=None
return e
while p.pointer.pointer is not None: #find the last
p = p.pointer
e = p.pointer.elem
p.pointer = None
return e
# def find(self, pred):
# '''find the element which is equal to value pred'''
# p = self._header
# count = 0
# while p is not None and pred != p.elem:
# count = count+1
# p = p.pointer
# if count == self._header.length(self._header):
# return 'the element you find is not in the list'
# return count
# use find function, I can pop any one
def find(self, index):
'''find the element with its index'''
if type(index) != int:
raise Exception('the value index must be integer')
p = self._header
count = 0
while p is not None and index != count:
count = count+1
p = p.pointer
if p is not None:
x = p.elem
return x,'**'
else:
return 'the index is out of length'
def print_all(self):
p = self._header
while p is not None:
print p.elem,'', #,not line break
p = p.pointer
print ''
def reverse(self):
p = self._header
if p is None or p.pointer is None:
return
p = None
while self._header is not None:
q = self._header
self._header = q.pointer
q.pointer = p
p = q
self._header = p
def sort(self):
p = self._header
if p is None and p.pointer is None:
return
rem = p.pointer
p.pointer = None
while rem:
p = self._header
q = None
while p and p.elem <= rem.elem:
q = p
p = p.pointer
if q is None:
self._header = rem
else:
q.pointer = rem
q = rem
rem = rem.pointer
q.pointer = p
# def elements(self):
# p = self._header
# while p:
# yield p.elem
# p = p.pointer
llist0 = Node(1)
p = llist0
for i in xrange(2,11):
p.pointer = Node(i)
p = p.pointer
print llist0.length(llist0)
p=llist0
while p is not None:
print p.elem
p=p.pointer
llist1 = linkedlist()
# print llist1.random_pop(1)
for i in range(10):
llist1.prepend(i)
for i in range(11, 20):
llist1.endpend(i)
llist1.print_all()
for i in range(5):
print llist1.pop()
print llist1.pop_last()
# print
print llist1.find(8)
print 'remained:'
llist1.print_all()
llist1.reverse()
print 'reversed:'
llist1.print_all()
# llist1.sort()
# llist1.print_all()
# for x in llist1.elements():
# print x
# print '\n'