mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Fine tune
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// File: Array.cs
|
||||
// File: array.cs
|
||||
// Created Time: 2022-12-14
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
@@ -134,4 +134,4 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// File: LinkedList.cs
|
||||
// File: linked_list.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
@@ -7,14 +7,14 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
public class LinkedList
|
||||
public class linked_list
|
||||
{
|
||||
/// <summary>
|
||||
/// 在链表的结点 n0 之后插入结点 P
|
||||
/// </summary>
|
||||
public static void Insert(ListNode n0, ListNode P)
|
||||
{
|
||||
ListNode n1 = n0.next;
|
||||
ListNode? n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode n1 = P.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
@@ -97,4 +97,4 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index)
|
||||
public int Get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index >= size)
|
||||
@@ -43,7 +43,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void set(int index, int num)
|
||||
public void Set(int index, int num)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
@@ -51,24 +51,24 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void add(int num)
|
||||
public void Add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
extendCapacity();
|
||||
ExtendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void insert(int index, int num)
|
||||
public void Insert(int index, int num)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
extendCapacity();
|
||||
ExtendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--)
|
||||
{
|
||||
@@ -80,7 +80,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int remove(int index)
|
||||
public int Remove(int index)
|
||||
{
|
||||
if (index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
@@ -97,7 +97,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity()
|
||||
public void ExtendCapacity()
|
||||
{
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, Capacity() * extendRatio);
|
||||
@@ -106,14 +106,14 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] toArray()
|
||||
public int[] ToArray()
|
||||
{
|
||||
int size = Size();
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
nums[i] = get(i);
|
||||
nums[i] = Get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
@@ -127,37 +127,37 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
/* 初始化列表 */
|
||||
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.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.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.ToArray()));
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.toArray()));
|
||||
list.Remove(3);
|
||||
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.get(1);
|
||||
int num = list.Get(1);
|
||||
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.toArray()));
|
||||
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);
|
||||
list.Add(i);
|
||||
}
|
||||
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.toArray()) +
|
||||
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.ToArray()) +
|
||||
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user