mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 22:16:06 +00:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -4,122 +4,121 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class array
|
||||
{
|
||||
public class Array
|
||||
/* 随机返回一个数组元素 */
|
||||
public static int randomAccess(int[] nums)
|
||||
{
|
||||
/* 随机返回一个数组元素 */
|
||||
public static int RandomAccess(int[] nums)
|
||||
Random random = new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
public static int[] extend(int[] nums, int enlarge)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
int[] res = new int[nums.Length + enlarge];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
Random random=new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
public static int[] Extend(int[] nums, int enlarge)
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
public static void insert(int[] nums, int num, int index)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = nums.Length - 1; i > index; i--)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
int[] res = new int[nums.Length + enlarge];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
public static void Insert(int[] nums, int num, int index)
|
||||
/* 删除索引 index 处元素 */
|
||||
public static void remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = nums.Length - 1; i > index; i--)
|
||||
{
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
public static void Remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++)
|
||||
{
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
public static void Traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
// 直接遍历数组
|
||||
foreach (int num in nums)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
public static int Find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 辅助函数,数组转字符串 */
|
||||
public static string ToString(int[] nums)
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
|
||||
[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);
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
public static void traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
// 直接遍历数组
|
||||
foreach (int num in nums)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
public static int find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 辅助函数,数组转字符串 */
|
||||
public static string toString(int[] nums)
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,88 +5,87 @@
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class linked_list
|
||||
{
|
||||
public class linked_list
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
public static void insert(ListNode n0, ListNode P)
|
||||
{
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
public static void Insert(ListNode n0, ListNode P)
|
||||
ListNode? n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
public static void remove(ListNode n0)
|
||||
{
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
public static ListNode? access(ListNode head, int index)
|
||||
{
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
ListNode? n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
public static void Remove(ListNode n0)
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
public static int find(ListNode head, int target)
|
||||
{
|
||||
int index = 0;
|
||||
while (head != null)
|
||||
{
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
public static ListNode? Access(ListNode head, int index)
|
||||
{
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
public static int Find(ListNode head, int target)
|
||||
{
|
||||
int index = 0;
|
||||
while (head != null)
|
||||
{
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
[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}");
|
||||
[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}");
|
||||
// 插入结点
|
||||
insert(n0, new ListNode(0));
|
||||
Console.WriteLine($"插入结点后的链表为{n0}");
|
||||
|
||||
// 删除结点
|
||||
Remove(n0);
|
||||
Console.WriteLine($"删除结点后的链表为{n0}");
|
||||
// 删除结点
|
||||
remove(n0);
|
||||
Console.WriteLine($"删除结点后的链表为{n0}");
|
||||
|
||||
// 访问结点
|
||||
ListNode? node = Access(n0, 3);
|
||||
Console.WriteLine($"链表中索引 3 处的结点的值 = {node?.val}");
|
||||
// 访问结点
|
||||
ListNode? node = access(n0, 3);
|
||||
Console.WriteLine($"链表中索引 3 处的结点的值 = {node?.val}");
|
||||
|
||||
// 查找结点
|
||||
int index = Find(n0, 2);
|
||||
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
|
||||
}
|
||||
// 查找结点
|
||||
int index = find(n0, 2);
|
||||
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,70 +6,69 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class list
|
||||
{
|
||||
public class list
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
|
||||
/* 初始化列表 */
|
||||
// 注意数组的元素类型是 int[] 的包装类 int[]
|
||||
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
|
||||
List<int> list = numbers.ToList();
|
||||
Console.WriteLine("列表 list = " + string.Join(",", list));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list[1];
|
||||
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 清空列表 */
|
||||
list.Clear();
|
||||
Console.WriteLine("清空列表后 list = " + string.Join(",", list));
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.Add(1);
|
||||
list.Add(3);
|
||||
list.Add(2);
|
||||
list.Add(5);
|
||||
list.Add(4);
|
||||
Console.WriteLine("添加元素后 list = " + string.Join(",", list));
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 删除元素 */
|
||||
list.RemoveAt(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < list.Count(); i++)
|
||||
{
|
||||
|
||||
/* 初始化列表 */
|
||||
// 注意数组的元素类型是 int[] 的包装类 int[]
|
||||
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
|
||||
List<int> list = numbers.ToList();
|
||||
Console.WriteLine("列表 list = " + string.Join(",",list));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list[1];
|
||||
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 清空列表 */
|
||||
list.Clear();
|
||||
Console.WriteLine("清空列表后 list = " + string.Join(",", list));
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.Add(1);
|
||||
list.Add(3);
|
||||
list.Add(2);
|
||||
list.Add(5);
|
||||
list.Add(4);
|
||||
Console.WriteLine("添加元素后 list = " + string.Join(",", list));
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 删除元素 */
|
||||
list.RemoveAt(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < list.Count(); i++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
foreach (int n in list)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<int> list1 = new() { 6, 8, 7, 10, 9 };
|
||||
list.AddRange(list1);
|
||||
Console.WriteLine("将列表 list1 拼接到 list 之后,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 排序列表 */
|
||||
list.Sort(); // 排序后,列表元素从小到大排列
|
||||
Console.WriteLine("排序列表后 list = " + string.Join(",", list));
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
foreach (int n in list)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<int> list1 = new() { 6, 8, 7, 10, 9 };
|
||||
list.AddRange(list1);
|
||||
Console.WriteLine("将列表 list1 拼接到 list 之后,得到 list = " + string.Join(",", list));
|
||||
|
||||
/* 排序列表 */
|
||||
list.Sort(); // 排序后,列表元素从小到大排列
|
||||
Console.WriteLine("排序列表后 list = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,159 +6,158 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
/* 列表类简易实现 */
|
||||
class MyList
|
||||
{
|
||||
class MyList
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int numsCapacity = 10; // 列表容量
|
||||
private int numsSize = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList()
|
||||
{
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList()
|
||||
{
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
public int Size()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int Capacity()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int Get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void Set(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void Add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void Insert(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--)
|
||||
{
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int Remove(int index)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
int num = nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (int j = index; j < size - 1; j++)
|
||||
{
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void ExtendCapacity()
|
||||
{
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, Capacity() * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = nums.Length;
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] ToArray()
|
||||
{
|
||||
int size = Size();
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
nums[i] = Get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
nums = new int[numsCapacity];
|
||||
}
|
||||
|
||||
public class my_list
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
public int size()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
return numsSize;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int capacity()
|
||||
{
|
||||
return numsCapacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void set(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (numsSize == numsCapacity)
|
||||
extendCapacity();
|
||||
nums[numsSize] = num;
|
||||
// 更新元素数量
|
||||
numsSize++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void insert(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (numsSize == numsCapacity)
|
||||
extendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = numsSize - 1; j >= index; j--)
|
||||
{
|
||||
/* 初始化列表 */
|
||||
MyList list = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
list.Add(1);
|
||||
list.Add(3);
|
||||
list.Add(2);
|
||||
list.Add(5);
|
||||
list.Add(4);
|
||||
Console.WriteLine("列表 list = " + string.Join(",", list.ToArray()) +
|
||||
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.ToArray()));
|
||||
|
||||
/* 删除元素 */
|
||||
list.Remove(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.Get(1);
|
||||
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list.Set(1, 0);
|
||||
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.ToArray()));
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.Add(i);
|
||||
}
|
||||
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.ToArray()) +
|
||||
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
numsSize++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int remove(int index)
|
||||
{
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
int num = nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (int j = index; j < numsSize - 1; j++)
|
||||
{
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
numsSize--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity()
|
||||
{
|
||||
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, numsCapacity * extendRatio);
|
||||
// 更新列表容量
|
||||
numsCapacity = nums.Length;
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] toArray()
|
||||
{
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[numsSize];
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
{
|
||||
nums[i] = get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化列表 */
|
||||
MyList list = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
Console.WriteLine("列表 list = " + string.Join(",", list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.toArray()));
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.toArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.get(1);
|
||||
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.toArray()));
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i);
|
||||
}
|
||||
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user