Skip to content

Commit f87f842

Browse files
author
tianqing.liang
committed
递归
1 parent 80f729e commit f87f842

File tree

7 files changed

+180
-20
lines changed

7 files changed

+180
-20
lines changed

05LinkedList/LinkedList.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class LinkedList<T> {
33
late int _size;
44

55
LinkedList() {
6-
_dummyHead = new _Node.all(null,null);
6+
_dummyHead = new _Node.withEmpty();
77
_size = 0;
88
}
99

@@ -52,7 +52,7 @@ class LinkedList<T> {
5252
for (var i = 0; i < index; i++) {
5353
prev = prev?.next;
5454
}
55-
_Node _node = new _Node.head(t);
55+
_Node _node = new _Node.wihtHead(t);
5656
_node.next = prev?.next;
5757
prev?.next = _node;
5858
_size++;
@@ -127,12 +127,12 @@ class _Node<T> {
127127

128128
T? t;
129129

130-
_Node.empty();
130+
_Node.withEmpty();
131131

132-
_Node.all(this.t, this.next);
132+
_Node.withAll(this.t, this.next);
133133

134-
factory _Node.head(T t) {
135-
var result = new _Node.all(t, null);
134+
factory _Node.wihtHead(T t) {
135+
var result = new _Node.withAll(t, null);
136136
return result;
137137
}
138138

05LinkedList/LinkedListQueue.dart

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,75 @@
11
import 'Queue.dart';
2+
3+
/**
4+
* 带尾结点的队列
5+
*/
26
class LinkedListQueue implements Queue{
7+
8+
late _Node? _head;
9+
late _Node? _tail;
10+
late int _size;
11+
12+
LinkedListQueue(){
13+
_head = null;
14+
_tail = null;
15+
_size = 0;
16+
}
17+
318
@override
419
dequeue() {
5-
// TODO: implement dequeue
6-
throw UnimplementedError();
20+
if (isEmpty()) {
21+
throw Exception("Cannot dequeue from an empty queue");
22+
}
23+
_Node retNode = _head!;
24+
_head = _head!.next;
25+
retNode.next = null;
26+
if(_head == null){
27+
_tail =null;
28+
}
29+
_size --;
30+
return retNode.t;
731
}
832

933
@override
1034
void enqueue(e) {
11-
// TODO: implement enqueue
35+
if(_tail == null){
36+
_tail = _Node.wihtHead(e);
37+
_head = _tail;
38+
}else{
39+
_tail!.next =_Node.wihtHead(e);
40+
_tail = _tail!.next;
41+
}
42+
_size++;
1243
}
1344

1445
@override
1546
getFront() {
16-
// TODO: implement getFront
17-
throw UnimplementedError();
47+
if (isEmpty()) {
48+
throw Exception("Queue is empty");
49+
}
50+
return _head!.t;
1851
}
1952

2053
@override
2154
int getSize() {
22-
// TODO: implement getSize
23-
throw UnimplementedError();
55+
return _size;
2456
}
2557

2658
@override
2759
bool isEmpty() {
28-
// TODO: implement isEmpty
29-
throw UnimplementedError();
60+
return _size == 0;
61+
}
62+
63+
@override
64+
String toString() {
65+
StringBuffer res =new StringBuffer();
66+
res.write("Queue: front ");
67+
for(_Node? cur = _head!.next ; cur != null ; cur = cur.next){
68+
res.write(cur);
69+
res.write("->");
70+
}
71+
res.write("NULL tail");
72+
return res.toString();
3073
}
3174

3275
}
@@ -37,17 +80,29 @@ class _Node<T> {
3780

3881
T? t;
3982

40-
_Node.empty();
83+
_Node();
4184

42-
_Node.all(this.t, this.next);
85+
_Node.withAll(this.t, this.next);
4386

44-
factory _Node.head(T t) {
45-
var result = new _Node.all(t, null);
87+
factory _Node.wihtHead(T t) {
88+
var result = new _Node.withAll(t, null);
4689
return result;
4790
}
4891

4992
@override
5093
String toString() {
5194
return t.toString();
5295
}
96+
}
97+
98+
void main(){
99+
LinkedListQueue queue = new LinkedListQueue();
100+
for(int i = 0 ; i < 10 ; i ++){
101+
queue.enqueue(i);
102+
print(queue);
103+
if(i % 3 ==2){
104+
queue.dequeue();
105+
print(queue);
106+
}
107+
}
53108
}

05LinkedList/main.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'LinkedList.dart';
22
import 'LinkedListStatck.dart';
3+
import 'LinkedListQueue.dart';
34
void main(){
45
LinkedList linkedList = new LinkedList();
56
for(int i = 0 ; i < 5 ; i ++){
@@ -20,4 +21,12 @@ void main(){
2021
stack.push(i);
2122
print(stack);
2223
}
24+
LinkedListQueue queue = new LinkedListQueue();
25+
for(int i = 0 ; i < 10 ; i ++){
26+
queue.enqueue(i);
27+
if(i % 3 ==2){
28+
queue.dequeue();
29+
print(queue);
30+
}
31+
}
2332
}

06Recursion/ListNode.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class ListNode{
2+
3+
int? val;
4+
ListNode? next;
5+
6+
ListNode(this.val);
7+
8+
ListNode.withArray(List arr){
9+
if(arr == null || arr.length == 0){
10+
throw Exception("arr cannot be empty");
11+
}
12+
this.val = arr[0];
13+
ListNode cur = this;
14+
for(var i = 1;i<arr.length;i++){
15+
cur.next = ListNode(arr[i]);
16+
cur = cur.next!;
17+
}
18+
}
19+
@override
20+
String toString() {
21+
StringBuffer s = new StringBuffer();
22+
ListNode cur =this;
23+
while(cur !=null){
24+
s.write("${cur.val} ->");
25+
if(cur.next !=null){
26+
cur = cur.next!;
27+
}else{
28+
break;
29+
}
30+
}
31+
s.write("NULL");
32+
return s.toString();
33+
}
34+
35+
36+
}

06Recursion/Solution3.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'ListNode.dart';
2+
class Solution3{
3+
4+
static ListNode? removeElements(ListNode head,int val){
5+
ListNode dummyHead = new ListNode(-1);
6+
dummyHead.next = head;
7+
8+
ListNode prev = dummyHead;
9+
while(prev.next!=null){
10+
if(prev.next!.val == val){
11+
prev.next = prev.next!.next;
12+
}else{
13+
prev = prev.next!;
14+
}
15+
}
16+
return dummyHead.next;
17+
if(head == null){
18+
return head;
19+
}
20+
// ListNode? res = removeElements(head.next!, val);
21+
// if(head.val == val){
22+
// return res;
23+
// }else{
24+
// head.next = res;
25+
// return head;
26+
// };
27+
28+
}
29+
}
30+
void main(){
31+
var nums = [1,2,6,3,4,5,6];
32+
ListNode listNode =ListNode.withArray(nums);
33+
print(listNode);
34+
35+
ListNode? res = Solution3.removeElements(listNode, 6);
36+
print(res);
37+
38+
}

06Recursion/Sum.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 递归
3+
*/
4+
class Sum{
5+
6+
static int sum(List arr){
7+
return sumRecurison(arr,0);
8+
}
9+
static int sumRecurison(List arr,int l){
10+
if(l == arr.length){
11+
return 0;
12+
}
13+
return arr[l]+sumRecurison(arr,l+1);
14+
}
15+
}
16+
17+
void main(){
18+
List nums = [1,2,3,4,5,6,7,8];
19+
int result = Sum.sum(nums);
20+
print("结果:$result");
21+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
2. 选择排序
77
3. 插入排序
88
4. 栈,队列,循环队列
9-
5. 链表,链表实现栈
9+
5. 链表,链表实现栈,链表实现队列
10+
6. 递归
1011

0 commit comments

Comments
 (0)