添加Lesson9
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,174 @@
|
||||
namespace Lesson9_顺序存储和链式存储练习
|
||||
{
|
||||
class LinkNode<T>
|
||||
{
|
||||
public T value;
|
||||
public LinkNode<T> nextNode;
|
||||
public LinkNode<T> frontNode;
|
||||
public LinkNode(T value)
|
||||
{
|
||||
this.value = value;
|
||||
nextNode = null;
|
||||
frontNode = null;
|
||||
}
|
||||
}
|
||||
class LinkList<T>
|
||||
{
|
||||
private LinkNode<T> head;
|
||||
private LinkNode<T> tail;
|
||||
private int count;
|
||||
public LinkList()
|
||||
{
|
||||
count = 0;
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
public LinkNode<T> Head
|
||||
{
|
||||
get
|
||||
{
|
||||
return head;
|
||||
}
|
||||
}
|
||||
public LinkNode<T> Tail
|
||||
{
|
||||
get
|
||||
{
|
||||
return tail;
|
||||
}
|
||||
}
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
public void Add(T value)
|
||||
{
|
||||
LinkNode<T> node = new LinkNode<T>(value);
|
||||
if (head == null)
|
||||
{
|
||||
head = node;
|
||||
tail = node;
|
||||
Console.WriteLine("空,已加节点并赋值");
|
||||
}
|
||||
else
|
||||
{
|
||||
tail.nextNode = node;
|
||||
tail.nextNode.frontNode = tail;
|
||||
tail = node;
|
||||
tail.nextNode = null;
|
||||
Console.WriteLine("添加尾部节点");
|
||||
}
|
||||
++count;
|
||||
}
|
||||
public void PrintAllNodesFront()
|
||||
{
|
||||
if (head == null)
|
||||
{
|
||||
Console.WriteLine("链表为空");
|
||||
return;
|
||||
}
|
||||
LinkNode<T> node = head;
|
||||
while (node != null)
|
||||
{
|
||||
Console.Write(node.value + " ");
|
||||
node = node.nextNode;
|
||||
}
|
||||
}
|
||||
public void PrintAllNodesLast()
|
||||
{
|
||||
if (head == null)
|
||||
{
|
||||
Console.WriteLine("链表为空");
|
||||
return;
|
||||
}
|
||||
LinkNode<T> node = tail;
|
||||
while (node != null)
|
||||
{
|
||||
Console.Write(node.value + " ");
|
||||
node = node.frontNode;
|
||||
}
|
||||
}
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
Console.WriteLine($"目前只有{count}个node,输入无效");
|
||||
return;
|
||||
}
|
||||
int tempCount = 0;
|
||||
LinkNode<T> tempNode = head;
|
||||
while (true)
|
||||
{
|
||||
if (tempCount == index)
|
||||
{
|
||||
//找到节点
|
||||
if (tempNode.frontNode != null)
|
||||
{
|
||||
tempNode.frontNode.nextNode = tempNode.nextNode;
|
||||
}
|
||||
if (tempNode.nextNode != null)
|
||||
{
|
||||
tempNode.nextNode.frontNode = tempNode.frontNode;
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
head = tempNode.nextNode;
|
||||
}else if (index == count - 1)
|
||||
{
|
||||
tail = tail.frontNode;
|
||||
}
|
||||
|
||||
count--;
|
||||
break;
|
||||
}
|
||||
tempNode = tempNode.nextNode;
|
||||
tempCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
LinkList<int> list = new LinkList<int>();
|
||||
list.Add(1);
|
||||
list.Add(2);
|
||||
list.Add(3);
|
||||
list.Add(4);
|
||||
|
||||
Console.WriteLine("Node数量:");
|
||||
Console.WriteLine(list.Count);
|
||||
|
||||
Console.WriteLine("-------------------");
|
||||
|
||||
Console.WriteLine("反向遍历");
|
||||
list.PrintAllNodesLast();
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-------------------");
|
||||
|
||||
Console.WriteLine("正向遍历");
|
||||
list.PrintAllNodesFront();//正向遍历
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-------------------");
|
||||
|
||||
Console.WriteLine("删除后正向遍历");
|
||||
list.RemoveAt(0);
|
||||
list.PrintAllNodesFront();//正向遍历
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-------------------");
|
||||
|
||||
Console.WriteLine("删除后反向遍历");
|
||||
list.RemoveAt(1);
|
||||
list.PrintAllNodesFront();//正向遍历
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user