Skip to content

Commit b5f5c49

Browse files
author
tianqing.liang
committed
链表
1 parent b98a6cc commit b5f5c49

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

04Stack-Queue/LoopQueue.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ class LoopQueue<T> implements Queue<T>{
1010
arr = List.filled(10, false);
1111
}
1212

13+
LoopQueue.capcity(int capacity){
14+
arr = List.filled(capacity+1, false);
15+
}
16+
1317
int getCapacity(){
14-
int capacity = arr!.length;
18+
//目前这种方式浪费一个空间
19+
int capacity = arr!.length-1;
1520
return capacity;
1621
}
1722

1823
@override
1924
dequeue() {
25+
if(isEmpty()){
26+
throw Exception("Cannot dequeue from an empty queue");
27+
}
2028
T ret = arr![front];
2129
arr![front] = null;
2230
front = (front + 1) % arr!.length;
@@ -29,7 +37,7 @@ class LoopQueue<T> implements Queue<T>{
2937

3038
void resize(int newCapacity){
3139

32-
List? newArr = List.filled(newCapacity, false);
40+
List? newArr = List.filled(newCapacity+1, false);
3341
for(int i = 0 ; i < arr!.length ; i ++){
3442
newArr[i] = arr![(i + front) % arr!.length];
3543
}

04Stack-Queue/Main.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ void main(){
1010
// }
1111
// stack.pop();
1212
//
13-
ArrayQueue queue = new ArrayQueue();
14-
for(int i = 0 ; i < 20 ; i ++){
15-
queue.enqueue(i);
16-
print(queue);
17-
if(i % 3 == 2){
18-
print('----出栈头部元素-----');
19-
queue.dequeue();
20-
print(queue);
21-
}
22-
}
13+
// ArrayQueue queue = new ArrayQueue();
14+
// for(int i = 0 ; i < 20 ; i ++){
15+
// queue.enqueue(i);
16+
// print(queue);
17+
// if(i % 3 == 2){
18+
// print('----出栈头部元素-----');
19+
// queue.dequeue();
20+
// print(queue);
21+
// }
22+
// }
2323

2424
LoopQueue<int> loopQueue = new LoopQueue<int>();
2525
for(int i = 0 ; i < 30 ; i ++){

05LinkedList/LinkedList.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class LinkedList<T>{
2+
3+
late _Node _head;
4+
late int _size;
5+
6+
LinkedList(){
7+
_head = new _Node.empty();
8+
_size = 0;
9+
}
10+
11+
int getSize(){
12+
return _size;
13+
}
14+
15+
bool isEmpty(){
16+
return _size == 0;
17+
}
18+
19+
addFirst(T t){
20+
// _Node _node =new _Node.head(t);
21+
// _node.next = _head;
22+
// _head = _node;
23+
24+
_head = new _Node.all(t, _head);
25+
_size++;
26+
}
27+
28+
//在链表的index位置添加新的元素e
29+
add(int index,T t){
30+
if(index<0||index>_size){
31+
throw Exception("Add Failed,Illegal index");
32+
}
33+
if(index == 0){
34+
addFirst(t);
35+
}else{
36+
_Node prev = _head;
37+
for(var i =0;i<index-1;i++){
38+
prev = prev.next;
39+
}
40+
_Node _node = new _Node.head(t);
41+
_node.next = prev.next;
42+
prev.next = _node;
43+
_size++;
44+
}
45+
}
46+
47+
addLast(T t){
48+
add(_size, t);
49+
}
50+
}
51+
//节点
52+
class _Node<T>{
53+
late _Node next;
54+
late T t;
55+
56+
_Node.empty();
57+
58+
_Node.all(this.t,this.next);
59+
60+
factory _Node.head(T t){
61+
dynamic i ;
62+
var result = new _Node.all(t, i);
63+
return result;
64+
}
65+
66+
@override
67+
String toString() {
68+
return t.toString();
69+
}
70+
}

05LinkedList/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'LinkedList.dart';
2+
3+
void main(){
4+
5+
}

0 commit comments

Comments
 (0)