Skip to content

Commit fb03fc5

Browse files
committed
26数据结构day03部分完成
1 parent a4dc3f1 commit fb03fc5

7 files changed

Lines changed: 160 additions & 3 deletions

File tree

26数据结构/day01/__init__.py

Whitespace-only changes.

26数据结构/day02/02basic.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,51 @@ def insert(self, pos, item):
9090
pre.next = node
9191

9292
def remove(self, item):
93+
if self.is_empty():
94+
return
95+
9396
cur = self.__head
9497
pre = None
9598

96-
while cur is not None:
99+
while cur.next is not self.__head:
97100
if cur.elem == item:
98101
# 判断是不是头节点
99102
if cur == self.__head:
103+
rear = self.__head
104+
while rear.next is not self.__head:
105+
rear = rear.next
100106
self.__head = cur.next
107+
rear.next = self.__head
108+
101109
else:
102110
pre.next = cur.next
103-
break
111+
return
104112
else:
105113
pre = cur
106114
cur = cur.next
107115

116+
if cur.elem == item:
117+
if cur is self.__head:
118+
# 链表只有一个节点
119+
self.__head = None
120+
else:
121+
pre.next = cur.next
122+
108123
def search(self, item):
124+
125+
if self.is_empty():
126+
return False
127+
109128
cur = self.__head
110-
while cur is not None:
129+
while cur.next is not self.__head:
111130
if cur.elem == item:
112131
return True
113132
else:
114133
cur = cur.next
134+
# 此时的cur指向最后一个元素
135+
if cur.elem == item:
136+
return True
137+
115138
return False
116139

117140

26数据结构/day02/__init__.py

Whitespace-only changes.

26数据结构/day03/01basic.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
堆栈就像水杯一样,一种容器,类似于顺序表结构但是只有push和pop,一端进出,后进的先出去
3+
队列就像排队一样,先来的先出去
4+
"""
5+
6+
7+
class Stack(object):
8+
def __init__(self):
9+
self.__list = []
10+
11+
def push(self, item):
12+
self.__list.append(item)
13+
# 可以选择append和insert0的方式,不过insert的方式时间复杂度为O(n)
14+
# self.__list.insert(0)
15+
# self.__list.pop(0)
16+
17+
def pop(self):
18+
self.__list.pop()
19+
20+
def peek(self):
21+
if self.__list:
22+
return self.__list[-1]
23+
else:
24+
return None
25+
26+
def is_empty(self):
27+
# return not self.__list
28+
# return self.__list == []
29+
return self.size() == 0
30+
31+
def size(self):
32+
return len(self.__list)
33+
34+
35+
class Queue(object):
36+
def __init__(self):
37+
self.__list = []
38+
39+
def enqueue(self, item):
40+
self.__list.append(item)
41+
# 可以选择append和insert0的方式,不过insert的方式时间复杂度为O(n)
42+
# self.__list.insert(0, item)
43+
# self.__list.pop()
44+
45+
def dequeue(self):
46+
self.__list.pop(0)
47+
48+
def is_empty(self):
49+
# return not self.__list
50+
# return self.__list == []
51+
return self.size() == 0
52+
53+
def size(self):
54+
return len(self.__list)
55+
56+
57+
class Deque(object):
58+
"""
59+
double-ended-queue双端队列,两端都可以进行push和pop,类似于两个堆栈底部合起来
60+
61+
"""
62+
def __init__(self):
63+
self.__list = []
64+
65+
def add_front(self, item):
66+
self.__list.insert(0, item)
67+
68+
def ad_rear(self, item):
69+
self.__list.append(item)
70+
71+
def remove_front(self):
72+
self.__list.pop(0)
73+
74+
def remove_rear(self):
75+
self.__list.pop()
76+
77+
def is_empty(self):
78+
# return not self.__list
79+
# return self.__list == []
80+
return self.size() == 0
81+
82+
def size(self):
83+
return len(self.__list)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
def bubble_sort(alist):
3+
4+
n = len(alist)
5+
for j in range(0, n-1):
6+
# j表示需要n-1次冒泡过程, i表示一次冒泡过程中的交换
7+
8+
count = 0
9+
for i in range(0, n-j-1):
10+
# 只需要到列表的n-1位置,因为从0开始正好range结尾是闭区间,所以只需要-1
11+
# 比如总共9个元素,需要遍历到第八个位置,让其跟8+1去比较,第八个元素的索引是7,所以range(0, 8)
12+
if alist[i] > alist[i+1]:
13+
alist[i], alist[i+1] = alist[i+1], alist[i]
14+
count += 1
15+
"""
16+
每次冒泡的过程count都为0来记录这次冒泡过程中是否有交换发生
17+
如果只要有一次冒泡过程中没有交换发生直接结束循环,表示前面的都是有序的
18+
[1,2,3,9,8]第三次循环的时候发现count没变,直接return
19+
"""
20+
if 0 == count:
21+
return
22+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
def select_sort(alist):
3+
4+
"""
5+
选择排序:从无序队列中选择最小的放到前面去,有序部分和无序部分
6+
首先认为最小的元素就是第一个元素,不断比较更新这个min的数值
7+
"""
8+
for j in range(0, len(alist)-1):
9+
10+
min_index = j
11+
# 因为j要和剩余的部分作比较也就是从j+1到最后一个元素,n的索引为n-1,闭区间所以到n
12+
for i in range(j+1, len(alist)):
13+
if alist[min_index] > alist[i]:
14+
min_index = i
15+
16+
alist[j], alist[min_index] = alist[min_index], alist[j]
17+
18+
"""
19+
j从0开始比较到倒数第二个元素,总共9个元素,最后让8和8+1比较,所以range:9-1=8,闭区间只能到7
20+
内层循环中让j和j+1比较,更新min_index和剩余无序部分的数值
21+
时间复杂度为O(n^2)
22+
"""
23+
24+
"""
25+
排序中的稳定性,如果列表中两个元素相等,排序如果可能出现这两个相等元素的位置不固定的话,则为不稳定排序
26+
[1, 3, 2, 2]
27+
"""
28+
29+

26数据结构/day03/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)