直接上代码,封装好的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
public interface IClinkList//定义链表接口
{
void AppendList(ListNode node);//进入链表
void InsertList(int index, ListNode node);//插入链表
int Find(int index);//查找节点
void Update(int index, ListNode node);//修改节点
void Delete(ListNode node);//删除节点
}
public class ListNode//设计一个头节点类
{
public int Data;
public ListNode head;//前一个
public ListNode next;//后一个
public ListNode(int x)
{
head = null;
this.Data = x;
}
}
class LinkList:IClinkList
{
ListNode head = null;//头节点置空
public void AppendList(ListNode node)
{
if(head==null)
{
head = node;
}
else
{
ListNode tmpNode = head;
while(tmpNode.next!=null)
{
tmpNode = tmpNode.next;
}
tmpNode.next = node;//将指针指向增加的节点
}
}
public void InsertList(int index,ListNode node)
{
if(head==null)
{
head = node;
}
else
{
ListNode tmpNode = head;
//头部不为空,插入位置为头
if(index==0)
{
head = node;
node.next = tmpNode;
}
else
{
int cnt = 0;
while(tmpNode.next!=null&&cnt!=index)
{
cnt++;
tmpNode = tmpNode.next;
}
//A-C插入B
node.next = tmpNode.next;//B--C
tmpNode.next = node;//A--B
//所有执行后为A--B--C
}
}
}
public int Find(int index)//查找节点
{
if(head==null)
{
return int.MaxValue;//头指针为空
}
else
{
ListNode tmpNode = head;
int cnt = 0;
while(tmpNode!=null&&cnt!=index)
{
cnt++;
tmpNode = tmpNode.next;
}
if(cnt==index)//下标相同找到结果
{
return tmpNode.Data;
}
else//下标未找到
{
return int.MaxValue;//
}
}
}
public void Update(int index,ListNode node)//修改节点
{
if(head==null)
{
return;
}
else
{
ListNode tmpNode = head;
int cnt = 0;
while (tmpNode.next != null && cnt != index)
{
cnt++;
tmpNode = tmpNode.next;
}
if (cnt == index)
{
tmpNode.Data = node.Data;
}
else
{
return;
}
}
}
public void Delete(ListNode node) //删除节点
{
if (head == null)
{
return;
}
else
{
if (head.Data == node.Data)
{
head = null;
}
ListNode tmpNode = head;
while (tmpNode != null && tmpNode.Data != node.Data)
{
tmpNode = tmpNode.next;
} //遍历链表,当节点为空或者找到删除节点时退出
if (tmpNode == null)
{
Console.WriteLine("Delete Node is not Exist");
}
else
{
//记录删除的节点
ListNode temp = tmpNode.next;
//将指针指向删除节点的下一个节点
tmpNode.next = tmpNode.next.next;
//节点置空
temp.next = null;
}
}
}
public void ShowMe()
{
ListNode tmpNode = head;
while (tmpNode != null)
{
Console.WriteLine("Value == " + tmpNode.Data);
tmpNode = tmpNode.next;
}
}
}
}
测试代码如下:
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
LinkList tmpList = new LinkList();
tmpList.AppendList(new ListNode(10)); //10
tmpList.AppendList(new ListNode(20)); //10 20
tmpList.AppendList(new ListNode(30)); //10 20 30
tmpList.AppendList(new ListNode(40)); //10 20 30 40
tmpList.AppendList(new ListNode(50)); //10 20 30 40 50
tmpList.ShowMe(); //10 20 30 40 50
Console.WriteLine("===================");
tmpList.Delete(new ListNode(30)); //删除30
tmpList.Delete(new ListNode(70)); //删除不存在的70
tmpList.ShowMe();
Console.WriteLine("===================");
tmpList.Update(3, new ListNode(31)); //将下标为3的节点改为31
tmpList.ShowMe();
}
}
}