C#实现链表的增加,查找,修改和删除

直接上代码,封装好的类

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();
        }
       
    }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值