Skip to content

Commit 1951c8b

Browse files
author
tianqing.liang
committed
Stack Queue LoopQueue
1 parent 9660640 commit 1951c8b

File tree

6 files changed

+182
-37
lines changed

6 files changed

+182
-37
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<h3>使用Flutter的Dart语言重写数据结构与算法</h3>
44

5-
1. [Dart,Python]线性搜索
6-
2. [Dart,Python]选择排序
7-
3. [Dart,Python]插入排序
8-
4. 栈,队列,循环队列
5+
1. **[Dart,Python]线性搜索**
6+
2. **[Dart,Python]选择排序**
7+
3. **[Dart,Python]插入排序**
8+
4. **[Dart,Python]栈,队列,循环队列**
99
5. 链表,链表实现栈,链表实现队列
1010
6. 递归
1111
7. 归并排序
@@ -15,8 +15,8 @@
1515
11. 集合 和 映射
1616
12.
1717
13. 优先队列
18-
14. [Dart,Python]冒泡排序
19-
15. [Dart,Python]希尔排序
18+
14. **[Dart,Python]冒泡排序**
19+
15. **[Dart,Python]希尔排序**
2020
16. 线段树
2121
17. Trie字典树
2222
18. 并查集

dart/04-Stack-Queue/LoopQueue.dart

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
import 'Queue.dart';
22

3-
class LoopQueue<T> implements Queue<T>{
4-
3+
class LoopQueue<T> implements Queue<T> {
54
List? arr;
6-
int front = 0,tail=0;
7-
int size =0;
5+
int front = 0, tail = 0;
6+
int size = 0;
87

9-
LoopQueue(){
8+
LoopQueue() {
109
arr = List.filled(10, false);
1110
}
1211

13-
LoopQueue.capcity(int capacity){
14-
arr = List.filled(capacity+1, false);
15-
}
12+
// LoopQueue.capcity(int capacity){
13+
// arr = List.filled(capacity+1, false);
14+
// }
1615

17-
int getCapacity(){
16+
int getCapacity() {
1817
//目前这种方式浪费一个空间
19-
int capacity = arr!.length-1;
18+
int capacity = arr!.length - 1;
2019
return capacity;
2120
}
2221

2322
@override
2423
dequeue() {
25-
if(isEmpty()){
24+
if (isEmpty()) {
2625
throw Exception("Cannot dequeue from an empty queue");
2726
}
2827
T ret = arr![front];
2928
arr![front] = null;
3029
front = (front + 1) % arr!.length;
3130
size--;
32-
if(size == getCapacity() / 4 && getCapacity() / 2 != 0){
33-
resize((getCapacity()/2) as int);
31+
if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
32+
resize((getCapacity() / 2) as int);
3433
}
3534
return ret;
3635
}
3736

38-
void resize(int newCapacity){
39-
40-
List? newArr = List.filled(newCapacity+1, false);
41-
for(int i = 0 ; i < arr!.length ; i ++){
37+
void resize(int newCapacity) {
38+
List? newArr = List.filled(newCapacity + 1, false);
39+
for (int i = 0; i < arr!.length; i++) {
4240
newArr[i] = arr![(i + front) % arr!.length];
4341
}
4442
arr = newArr;
@@ -48,17 +46,15 @@ class LoopQueue<T> implements Queue<T>{
4846

4947
@override
5048
void enqueue(e) {
51-
52-
if((tail + 1) % arr!.length == front)
53-
resize(getCapacity() * 2);
49+
if ((tail + 1) % arr!.length == front) resize(getCapacity() * 2);
5450
arr![tail] = e;
5551
tail = (tail + 1) % arr!.length;
5652
size++;
5753
}
5854

5955
@override
6056
getFront() {
61-
if(isEmpty()){
57+
if (isEmpty()) {
6258
throw Exception("Queue is empty.");
6359
}
6460
return arr![front];
@@ -71,21 +67,19 @@ class LoopQueue<T> implements Queue<T>{
7167

7268
@override
7369
bool isEmpty() {
74-
return front == tail ;
70+
return front == tail;
7571
}
7672

7773
@override
7874
String toString() {
7975
StringBuffer buffer = new StringBuffer();
8076
buffer.write("Queue: size = $size , capacity = ${getCapacity()} \n");
8177
buffer.write('front [');
82-
for(int i = front ; i != tail ; i = (i + 1) % arr!.length){
78+
for (int i = front; i != tail; i = (i + 1) % arr!.length) {
8379
buffer.write(arr![i]);
84-
if((i + 1) % arr!.length != tail)
85-
buffer.write(", ");
80+
if ((i + 1) % arr!.length != tail) buffer.write(", ");
8681
}
8782
buffer.write("] tail");
8883
return buffer.toString();
8984
}
90-
91-
}
85+
}

dart/04-Stack-Queue/Main.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'ArrayStack.dart';
22
import 'ArrayQueue.dart';
33
import 'LoopQueue.dart';
44

5-
void main(){
5+
void main() {
66
// ArrayStack<int> stack = new ArrayStack<int>();
77
// for(int i = 0 ; i < 10 ; i ++){
88
// stack.push(i);
@@ -22,13 +22,14 @@ void main(){
2222
// }
2323

2424
LoopQueue<int> loopQueue = new LoopQueue<int>();
25-
for(int i = 0 ; i < 30 ; i ++){
25+
26+
for (int i = 0; i < 30; i++) {
2627
loopQueue.enqueue(i);
2728
print(loopQueue);
2829

29-
if(i % 3 == 2){
30+
if (i % 3 == 2) {
3031
loopQueue.dequeue();
3132
print(loopQueue);
3233
}
3334
}
34-
}
35+
}

python/loopqueue.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class LoopQueue(object):
2+
3+
def __init__(self, n=10):
4+
self.arr = [None] * (n)
5+
self.front = 0
6+
self.tail = 0
7+
self.size = 0
8+
9+
def getCapacity(self):
10+
capacity = len(self.arr) - 1;
11+
return capacity;
12+
13+
def dequeue(self):
14+
if self.isEmpty() :
15+
raise Exception("Cannot dequeue from an empty queue")
16+
ret = self.arr[self.front]
17+
self.arr[self.front] = None
18+
self.front = (self.front + 1)%len(self.arr)
19+
self.size -=1
20+
if((self.size == self.getCapacity()//4)) and (self.getCapacity()/2 !=0):
21+
resize(self.getCapacity()//2)
22+
return ret
23+
24+
def resize(self,newCapacity):
25+
newArr = [None]*(newCapacity+1)
26+
for i in range(0,len(self.arr)):
27+
newArr[i] = self.arr[(i+self.front)%len(self.arr)];
28+
self.arr = newArr
29+
self.front = 0;
30+
self.tail = self.size;
31+
32+
def enqueue(self,element):
33+
print('当前大小:',self.size)
34+
if ((self.tail+1)%len(self.arr)) == self.front:
35+
self.resize(self.getCapacity()*2)
36+
self.arr[self.tail] = element
37+
self.tail = (self.tail +1)% len(self.arr)
38+
self.size += 1
39+
print('当前大小:',self.size)
40+
41+
def getFront(self):
42+
if(self.isEmpty()):
43+
raise Exception("Queue is empty.")
44+
return self.arr[self.front]
45+
46+
def getSize(self):
47+
return len(self.arr)
48+
49+
def isEmpty(self):
50+
return self.front == self.tail
51+
52+
def __str__(self):
53+
return "front ["+" ".join(map(str, self.arr)) + "] tail"
54+
55+
if __name__ == '__main__':
56+
loopQueue = LoopQueue();
57+
for i in range(0,30):
58+
loopQueue.enqueue(i);
59+
print(loopQueue)
60+
61+
62+

python/queue.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
队列
3+
'''
4+
class Queue(object):
5+
6+
# 初始化
7+
def __init__(self):
8+
self.__list = []
9+
10+
#添加元素
11+
def push(self,element):
12+
self.__list.append(element)
13+
14+
# 弹出元素
15+
def pop(self):
16+
if self.isEmpty():
17+
return None
18+
else:
19+
return self.__list.pop(0)
20+
21+
#是否为空
22+
def isEmpty(self):
23+
return len(self.__list) == 0
24+
25+
def getSize(self):
26+
return len(self.__list)
27+
28+
def __str__(self):
29+
return " ".join(map(str, self.__list))
30+
31+
if __name__ == '__main__':
32+
testQueue = Queue();
33+
testQueue.push(1)
34+
testQueue.push(2)
35+
testQueue.push(3)
36+
testQueue.push(4)
37+
print(testQueue)
38+
testQueue.pop()
39+
print(testQueue)
40+
41+
42+
43+
44+

python/stack.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
3+
'''
4+
class Stack(object):
5+
6+
# 初始化
7+
def __init__(self):
8+
self.__list = []
9+
10+
#添加元素
11+
def push(self,element):
12+
self.__list.append(element)
13+
14+
# 弹出元素
15+
def pop(self):
16+
if self.isEmpty():
17+
return None
18+
else:
19+
return self.__list.pop()
20+
21+
#是否为空
22+
def isEmpty(self):
23+
return len(self.__list) == 0
24+
25+
def getSize(self):
26+
return len(self.__list)
27+
28+
def __str__(self):
29+
return " ".join(map(str, self.__list))
30+
31+
if __name__ == '__main__':
32+
testStack = Stack();
33+
testStack.push(1)
34+
testStack.push(2)
35+
testStack.push(3)
36+
testStack.push(4)
37+
print(testStack)
38+
testStack.pop()
39+
print(testStack)
40+
41+
42+
43+
44+

0 commit comments

Comments
 (0)