add dart chapter_array_and_linkedlist (#338)
* add dart chapter_array_and_linkedlist * update my_list.dart * update chapter_array_and_linkedlist * Update my_list.dart * Update array.dart --------- Co-authored-by: huangjianqing <huangjianqing@52tt.com> Co-authored-by: Yudong Jin <krahets@163.com>pull/358/head
parent
1cc9cecebe
commit
7f4efa6d5e
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* File: linked_list.dart
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/ListNode.dart';
|
||||
import '../utils/PrintUtil.dart';
|
||||
|
||||
class LinkedList {
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
void insert(ListNode n0, ListNode P) {
|
||||
ListNode? n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
void remove(ListNode n0) {
|
||||
if (n0.next == null) return;
|
||||
ListNode P = n0.next!;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
ListNode? access(ListNode? head, int index) {
|
||||
for (var i = 0; i < index; i++) {
|
||||
if (head == null) return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
int find(ListNode? head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 初始化链表
|
||||
//初始化各个结点
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
ListNode n2 = ListNode(2);
|
||||
ListNode n3 = ListNode(5);
|
||||
ListNode n4 = ListNode(4);
|
||||
// 构建引用指向
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
|
||||
print('初始化的链表为');
|
||||
PrintUtil().printLinkedList(n0);
|
||||
|
||||
/* 插入结点 */
|
||||
LinkedList().insert(n0, ListNode(0));
|
||||
PrintUtil().printLinkedList(n0);
|
||||
|
||||
/* 删除结点 */
|
||||
LinkedList().remove(n0);
|
||||
PrintUtil().printLinkedList(n0);
|
||||
|
||||
/* 访问结点 */
|
||||
ListNode? node = LinkedList().access(n0, 3);
|
||||
print('链表中索引 3 处的结点的值 = ${node!.val}');
|
||||
|
||||
/* 查找结点 */
|
||||
int index = LinkedList().find(n0, 2);
|
||||
print('链表中值为 2 的结点的索引 = $index');
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: list.dart
|
||||
* Created Time: 2023-01-24
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
List<int> list = [1,3,2,5,4];
|
||||
print('列表 list = $list');
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list[1];
|
||||
print('访问索引 1 处的元素,得到 num = $num');
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
print('将索引 1 处的元素更新为 0 ,得到 list = $list');
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
print('清空列表后 list = $list');
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
print('添加元素后 list = $list');
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
print('在索引 3 处插入数字 6 ,得到 list = $list');
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
count++;
|
||||
}
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (var n in list) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<int> list1 = [6,8,7,10,9];
|
||||
list.addAll(list1);
|
||||
print('将列表 list1 拼接到 list 之后,得到 list = $list');
|
||||
|
||||
/* 排序列表 */
|
||||
list.sort();
|
||||
print('排序列表后 list = $list');
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* File: ListNode
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
class ListNode {
|
||||
int val;
|
||||
ListNode? next;
|
||||
|
||||
ListNode(this.val, [this.next]);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ListNode{val: $val, next: $next}';
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* File: PrintUtil
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import 'ListNode.dart';
|
||||
|
||||
class PrintUtil {
|
||||
|
||||
void printLinkedList(ListNode? head) {
|
||||
List<String> list = [];
|
||||
|
||||
while (head != null) {
|
||||
list.add('${head.val}');
|
||||
head = head.next;
|
||||
}
|
||||
|
||||
print(list.join(' -> '));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue