双向链表(C#、Java)

本文介绍了如何在C#中使用DoubleLinkedLists类创建双向链表,包括节点的创建、链表的添加、删除、插入操作,以及一个示例程序展示了这些功能的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建节点

namespace Testmain
{
    public class TwoWayNode
    {
        public TwoWayNode preNode;
        public int Data;
        public TwoWayNode nextNode;
        public TwoWayNode(int data)
        {
            preNode = null;
            this.Data = data;
            nextNode = null;
        }
    }
}

2.使用DoubleLinkedLists类创建链表,包含添加、展示、删除、插入

namespace Testmain
{

    public class DoubleLinkedLists
    {
        private TwoWayNode head;
        public void AddNode(int data)
        {
            TwoWayNode newNode = new TwoWayNode(data);
            if (head == null)
            {
                head = newNode;
            }
            else
            {
                TwoWayNode currentNode = head;
                while (currentNode.nextNode != null)
                {
                    currentNode = currentNode.nextNode;

                }
                currentNode.nextNode = newNode;
                newNode.preNode = currentNode;
            }
        }
        public void delectNode(int data)
        {
            if (head == null)
            {
                return; // 空链表
            }

            if (head.Data == data)
            {
                head = head.nextNode; // 如果要删除的节点在头部
                return;
            }

            TwoWayNode current = head;
            while (current.nextNode != null)
            {
                if (current.nextNode.Data == data)
                {
                    current.nextNode = current.nextNode.nextNode;
                    current.nextNode.nextNode.preNode = current;
                    return;
                }
                current = current.nextNode;
            }

        }
        public void InsertNode(int index, TwoWayNode setNode)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", "Index cannot be negative");
            }

            if (index == 0)
            {
                setNode.nextNode = head;
                if (head != null)
                {
                    head.preNode = setNode;
                }
                head = setNode;
                return;
            }

            TwoWayNode current = head;
            int count = 0;

            while (current != null && count < index - 1)
            {
                current = current.nextNode;
                count++;
            }

            if (current == null)
            {
                throw new ArgumentOutOfRangeException("index", "Index is greater than the number of nodes in the list");
            }

            setNode.nextNode = current.nextNode;
            if (current.nextNode != null)
            {
                current.nextNode.preNode = setNode;
            }
            current.nextNode = setNode;
            setNode.preNode = current;
        }

        public void printList()
        {
            TwoWayNode current = head;
            while (current.nextNode != null)
            {
                Console.Write(current.Data + "-");
                current = current.nextNode;
            }
            Console.WriteLine(current.Data);
        }
    }
}


3、程序入口

using System;
namespace Testmain
{
    class Program
    {
        static void Main(string[] args)
        {
            DoubleLinkedLists node = new DoubleLinkedLists();
            node.AddNode(1);
            node.AddNode(2);
            node.AddNode(3);
            node.AddNode(4);
            node.AddNode(5);
            node.AddNode(6);
            node.delectNode(3);
            TwoWayNode node1 = new TwoWayNode(27);
            node.InsertNode(0, node1);
            node.printList();

        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值