猿问

如何使用类在 C# 的链表中创建并声明链表?

所以,我试图在链表的每个元素内创建一个链表,但我不知道如何用元素填充内部列表。


我首先像这样声明外部列表


RoomList<int> room = new RoomList<int>();

这是我使用的类结构(不确定这是否正确)


public class RoomList<T>

{

    DailyList head;


    public class DailyList

    {

        DailyListElement head;

        DailyListElement next;


        class DailyListElement

        {

            public T data;

            public DailyListElement next;

        }

    }

}

这是我用来创建 DailyList 对象的函数


public void DailyListCreate()

{

    DailyList newDailyList = new DailyList();          

}

但我不知道如何添加 DailyListElement。我希望这是有道理的。


所以我想知道如何用 DailyListElements 填充 DailyList。


米脂
浏览 101回答 3
3回答

蝴蝶不菲

我根据您提供的代码尝试了链表中的链表。在那里我添加了添加新节点和获取两个级别(外部和内部链表)的所有节点的方法。请在下面找到链表结构的代码以及客户端代码:链表结构public class RoomList<T> where T : class{&nbsp; &nbsp; private DailyList current;&nbsp; &nbsp; private DailyList head;&nbsp; &nbsp; public void Add(DailyList newItem)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(current != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current.next = newItem;&nbsp; &nbsp; &nbsp; &nbsp; current = newItem;&nbsp; &nbsp; &nbsp; &nbsp; if(head == null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head = current;&nbsp; &nbsp; }&nbsp; &nbsp; public IEnumerable<DailyList> GetAllNodes()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DailyList current&nbsp; = head;&nbsp; &nbsp; &nbsp; &nbsp; List<DailyList> lst = new List<DailyList>();&nbsp; &nbsp; &nbsp; &nbsp; while (current != null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lst.Add(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = current.next;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return lst;&nbsp; &nbsp; }&nbsp; &nbsp; public class DailyList&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DailyList next;&nbsp; &nbsp; &nbsp; &nbsp; private DailyListElement head;&nbsp; &nbsp; &nbsp; &nbsp; private DailyListElement current;&nbsp; &nbsp; &nbsp; &nbsp; public void Add(DailyListElement newItem)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(current != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current.next = newItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = newItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(head == null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head = current;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public IEnumerable<DailyListElement> GetAllNodes()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DailyListElement current&nbsp; = head;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<DailyListElement> lst = new List<DailyListElement>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (current != null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lst.Add(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = current.next;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return lst;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class DailyListElement&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public T data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public DailyListElement next;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}客户端代码:using System;using System.Collections.Generic;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var lst =new RoomList<string>();&nbsp; &nbsp; &nbsp; &nbsp; var upperNode = new RoomList<string>.DailyList();&nbsp; &nbsp; &nbsp; &nbsp; var element = new RoomList<string>.DailyList.DailyListElement();&nbsp; &nbsp; &nbsp; &nbsp; element.data = "first";&nbsp; &nbsp; &nbsp; &nbsp; upperNode.Add(element);&nbsp; &nbsp; &nbsp; &nbsp; element = new RoomList<string>.DailyList.DailyListElement();&nbsp; &nbsp; &nbsp; &nbsp; element.data = "second";&nbsp; &nbsp; &nbsp; &nbsp; upperNode.Add(element);&nbsp; &nbsp; &nbsp; &nbsp; lst.Add(upperNode);&nbsp; &nbsp; &nbsp; &nbsp; upperNode = new RoomList<string>.DailyList();&nbsp; &nbsp; &nbsp; &nbsp; element = new RoomList<string>.DailyList.DailyListElement();&nbsp; &nbsp; &nbsp; &nbsp; element.data = "third";&nbsp; &nbsp; &nbsp; &nbsp; upperNode.Add(element);&nbsp; &nbsp; &nbsp; &nbsp; element = new RoomList<string>.DailyList.DailyListElement();&nbsp; &nbsp; &nbsp; &nbsp; element.data = "fourth";&nbsp; &nbsp; &nbsp; &nbsp; upperNode.Add(element);&nbsp; &nbsp; &nbsp; &nbsp; lst.Add(upperNode);&nbsp; &nbsp; &nbsp; &nbsp; foreach(var item in lst.GetAllNodes())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var child in item.GetAllNodes())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(child.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

30秒到达战场

您需要实现一个添加新元素的方法。您可以通过遍历元素直到到达第一个 next 为 null 的元素,然后将新元素分配给 next 来完成此操作。或者您也可以维护指向最后一个元素的指针并直接访问它。

烙印99

您需要将方法添加Add到您的RoomList类DailyList中。public class RoomList<T>{&nbsp; &nbsp; public DailyList head;&nbsp; &nbsp; public DailyList Add()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var newItem = new DailyList();&nbsp; &nbsp; &nbsp; &nbsp; if (head != null) head.next = newItem;&nbsp; &nbsp; &nbsp; &nbsp; head = newItem;&nbsp; &nbsp; &nbsp; &nbsp; return newItem;&nbsp; &nbsp; }&nbsp; &nbsp; public class DailyList&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DailyList next;&nbsp; &nbsp; &nbsp; &nbsp; public DailyListElement head;&nbsp; &nbsp; &nbsp; &nbsp; public DailyListElement Add()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newItem = new DailyListElement();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (head != null) head.next = newItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head = newItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return newItem;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class DailyListElement&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public T data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public DailyListElement next;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}使用示例:var roomList = new RoomList<string>();var dailyList = roomList.Add();var dailyListElement = dailyList.Add();dailyListElement.data = "StackOverflow rocks!";Console.WriteLine(roomList.head.head.data);输出:StackOverflow 摇滚!
随时随地看视频慕课网APP
我要回答