mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
Update the project to make it simpler.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// Created Time: 2022-12-14
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
public class Array
|
||||
@@ -97,5 +99,39 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
[Test]
|
||||
public static void Test()
|
||||
{
|
||||
// 初始化数组
|
||||
int[] arr = new int[5];
|
||||
Console.WriteLine("数组 arr = " + ToString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
Console.WriteLine("数组 nums = " + ToString(nums));
|
||||
|
||||
// 随机访问
|
||||
int randomNum = RandomAccess(nums);
|
||||
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
|
||||
|
||||
// 长度扩展
|
||||
nums = Extend(nums, 3);
|
||||
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
|
||||
|
||||
// 插入元素
|
||||
Insert(nums, 6, 3);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
|
||||
|
||||
// 删除元素
|
||||
Remove(nums, 2);
|
||||
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
|
||||
|
||||
// 遍历数组
|
||||
Traverse(nums);
|
||||
|
||||
// 查找元素
|
||||
int index = Find(nums, 3);
|
||||
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
@@ -60,5 +61,40 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
// 初始化链表
|
||||
// 初始化各个结点
|
||||
ListNode n0 = new ListNode(1);
|
||||
ListNode n1 = new ListNode(3);
|
||||
ListNode n2 = new ListNode(2);
|
||||
ListNode n3 = new ListNode(5);
|
||||
ListNode n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
Console.WriteLine($"初始化的链表为{n0}");
|
||||
|
||||
// 插入结点
|
||||
Insert(n0, new ListNode(0));
|
||||
Console.WriteLine($"插入结点后的链表为{n0}");
|
||||
|
||||
// 删除结点
|
||||
Remove(n0);
|
||||
Console.WriteLine($"删除结点后的链表为{n0}");
|
||||
|
||||
// 访问结点
|
||||
ListNode node = Access(n0, 3);
|
||||
Console.WriteLine($"链表中索引 3 处的结点的值 = {node.val}");
|
||||
|
||||
// 查找结点
|
||||
int index = Find(n0, 2);
|
||||
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user