Skip to content

Commit c7075f5

Browse files
committed
feat: stack and queue
1 parent f43e3e3 commit c7075f5

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
.vscode
3+
*.exe

Python_Stack_by_link.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 对于我们实现栈的操作的是,实际上的话就是实现的是我们的操作我们的头节点实现的增删的操作实现
2+
3+
# 设置节点的元素
4+
class Node(object):
5+
def __init__(self, val, _next = None):
6+
self.val = val
7+
self.next = _next
8+
9+
10+
# 开始实现设计我们的栈结构的实现
11+
class Link_Stack(object):
12+
# 在构造函数中实现设置我们的栈顶
13+
def __init__(self):
14+
self.__top = None
15+
16+
# 栈结构只有两个操作就是一个是推数据进入我们的栈,另一个就是推数据出栈
17+
def push(self, data):
18+
# 先实现转化为节点设计
19+
node = Node(data)
20+
# 将该节点的指针指向栈顶
21+
node.next = self.__top
22+
self.__top = node
23+
24+
def pop(self):
25+
# 探究我们的栈是否为空
26+
if self.__top is None:
27+
raise Exception("stack is None")
28+
data = self.__top.val
29+
# 实现更新头栈顶节点
30+
self.__top = self.__top.next
31+
return data
32+
33+
# 实现获取栈顶元素
34+
def get_top_data(self):
35+
if self.__top is None:
36+
print(None)
37+
return
38+
return self.__top.val
39+
40+
# 实现的是显示栈中的所有的元素
41+
def show_all_data(self):
42+
if self.__top is None:
43+
print(None)
44+
return
45+
while self.__top is not None:
46+
print(self.__top.val)
47+
self.__top = self.__top.next
48+
49+
50+
if __name__ == "__main__":
51+
link_stack = Link_Stack()
52+
53+
# link_stack.push(1)
54+
# link_stack.push(2)
55+
56+
print(link_stack.get_top_data())
57+
58+
link_stack.show_all_data()

Python_Stack_by_order_list.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 顺序表实现栈结构
2+
# 栈顶的定位是关键点
3+
# list 这个时候就是我们底层的数据结构了
4+
# 为了我们性能的考虑,所以说我们实现的是将我们的列表的尾部作为栈顶的
5+
6+
class List_Stack(object):
7+
# 初始化函数,封装列表作为我们的底层的数据结构
8+
def __init__(self):
9+
self.__list_stack = []
10+
11+
# 使用我们的 append 方法来实现我们的栈的添加元素
12+
def push_back(self, data):
13+
self.__list_stack.append(data)
14+
15+
# 实现的是我们的出栈
16+
def pop_back(self):
17+
if self.__list_stack == []:
18+
raise Exception("stack is empty")
19+
return self.__list_stack.pop(-1)
20+
21+
# 判断我们的栈是否为空
22+
def is_empty(self):
23+
# if len(self.__list_stack) > 0:
24+
# return False
25+
# else:
26+
# return True
27+
return self.__list_stack == []
28+
29+
30+
if __name__ == "__main__":
31+
list_stack = List_Stack()
32+
print(list_stack.is_empty()) # True
33+
34+
list_stack.push_back(11)
35+
print(list_stack.is_empty()) # False
36+
37+
list_stack.pop_back()
38+
print(list_stack.is_empty()) # False
39+
40+
# 栈 stack 的这个数据结构的话还是十分简单的,相比链表的话,其具备的功能这些的的确确少了很多呐

Python_queue_by_list.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 通过我们的列表实现我们的队列
2+
# 队列的实现的是一边进入一边出
3+
# 实现的是先进先出,后进后出
4+
5+
class Queue(object):
6+
# 构造函数
7+
def __init__(self):
8+
self.__list_queue = []
9+
10+
# 实现的是我们的入队
11+
def push_back(self, data):
12+
self.__list_queue.append(data)
13+
14+
# 开始实现我们的出队
15+
def pop_front(self):
16+
if self.__list_queue == []:
17+
raise Exception("queue is empty")
18+
return self.__list_queue.pop(0)
19+
20+
# 开始实现我们的获取队列大小的操作
21+
def get_size(self):
22+
if self.__list_queue == []:
23+
return "queue is empty"
24+
return len(self.__list_queue)
25+
26+
# 实现判断queue 是否为空的函数
27+
def is_empty(self):
28+
return self.__list_queue == []
29+
30+
# 开始实现获取我们的数组中的元素
31+
def show_queue(self):
32+
if self.__list_queue == []:
33+
return None
34+
for item in self.__list_queue:
35+
print(item, end=" ")
36+
print("\n")
37+
38+
if __name__ == "__main__":
39+
queue = Queue()
40+
41+
queue.push_back(11)
42+
queue.push_back(12)
43+
44+
queue.show_queue()
45+
print(queue.pop_front())

0 commit comments

Comments
 (0)