创建链接列表。C#

创建链接列表。C#,c#,linked-list,nodes,user-input,C#,Linked List,Nodes,User Input,我尝试过创建链表/节点类,但我不确定下一步该怎么做。我的尝试没有成功,因为在创建类之后,我不确定下一步是什么 我正在尝试创建一个程序,该程序有一个恐龙节点,保存有关恐龙的信息,如id、物种等,我希望允许用户创建恐龙并从列表中删除恐龙。所以我需要允许用户输入数据,我假设有一种方法可以自动设置恐龙id,但我不确定 我已经包括了LinkedList.cs和节点.cs,因此您可以看到我要去哪里,但我不知道在我的程序类中如何利用链接列表并实现我要做的事情 添加了Program.cs类,以帮助识别/显示我在

我尝试过创建链表/节点类,但我不确定下一步该怎么做。我的尝试没有成功,因为在创建类之后,我不确定下一步是什么

我正在尝试创建一个程序,该程序有一个恐龙节点,保存有关恐龙的信息,如id、物种等,我希望允许用户创建恐龙并从列表中删除恐龙。所以我需要允许用户输入数据,我假设有一种方法可以自动设置恐龙id,但我不确定

我已经包括了
LinkedList.cs
节点.cs
,因此您可以看到我要去哪里,但我不知道在我的程序类中如何利用链接列表并实现我要做的事情

添加了
Program.cs
类,以帮助识别/显示我在程序中的位置/我需要做什么

链表类别:

using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Text;

namespace JurrasicFinal
{
    public class LinkedList
    {

        private Node head;
        private int count;

        public LinkedList()
        {
            this.head = null;
            this.count = 0;
        }

        public bool Empty
        {
            get { return this.count == 0; }
        }

        public int Count
        {
            get { return this.count; }
        }

        public object this[int index]
        {
            get { return this.Get(index); }
        }

        public object Add(int index, object o)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("Index: " + index);

            if (index > count)
                index = count;

            Node current = this.head;

            if (this.Empty || index == 0)
            {
                this.head = new Node(o, this.head);
            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                {
                    current = current.Next;
                    current.Next = new Node(o, current.Next);
                }
            }
            count++;
            return o;
        }

        public object Add(object o)
        {
            return this.Add(count, o);
        }

        public object Remove(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("Index: " + index);

            if (this.Empty)
                return null;

            if (index >= this.count)
                index = count - 1;

            Node current = this.head;
            object result = null;

            if (index == 0)
            {
                result = current.Data;
                this.head = current.Next;
            }
            else
            {
                for (int i = 0; index < index - 1; i++) ;
                current = current.Next;

                result = current.Next.Data;
                current.Next = current.Next.Next;
            }

            count--;

            return result;

        }

        public void Clear()
        {
            this.head = null;
            this.count = 0;
        }

        public int IndexOf(object o)
        {
            Node current = this.head;

            for (int i = 0; i < this.count; i++)
            {
                if (current.Data.Equals(o))
                    return i;

                current = current.Next;
            }
            return -1;
        }

        public bool Contains(object o)
        {
            return this.IndexOf(o) >= 0;
        }

        public object Get(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("Index: " + index);

            if (this.Empty)
                return null;

            if (index >= this.count)
                index = this.count - 1;

            Node current = this.head;

            for (int i = 0; i < index; i++)
                current = current.Next;

            return current.Data;
        }
    }
}
课程类别:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;

namespace JurrasicFinal
{
    class Program
    {

        class Dinosaur
        {
            public string Name;
            public string Classification;
            public char Sex;
        }


        static void Main(string[] args)
        {
            LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();

            Dinosaur Dino1 = new Dinosaur();
            Dino1.Name = "Tyrannosaurus Rex";
            Dino1.Classification = "Carnivorous";
            Dino1.Sex = 'M';

            Dinosaur Dino2 = new Dinosaur();
            Dino2.Name = "Velociraptor";
            Dino2.Classification = "Carnivorous";
            Dino2.Sex = 'F';

            Dinosaur Dino3 = new Dinosaur();
            Dino3.Name = "Procompsognathus";
            Dino3.Classification = "Carnivorous";
            Dino3.Sex = 'M';

            void printList()
            {
                Console.WriteLine("Current Queue: ");
                Console.WriteLine("\n");
                foreach (Dinosaur d in DinoList)
                {
                    Console.WriteLine("Name: " + d.Name);
                    Console.WriteLine("Classification: " + d.Classification);
                    Console.WriteLine("Sex " + d.Sex);
                    Console.WriteLine("\n");
                }

                Console.WriteLine(Dino1.Name +  Dino1.Sex);
            }
            DinoList.AddLast(Dino1);
            DinoList.AddLast(Dino2);
            DinoList.AddLast(Dino3);
            printList();
            Console.WriteLine(DinoList.Count);


            FileStream fileStream = File.OpenWrite("E:/Work/Dinosaur.txt");
            BinaryWriter writer = new BinaryWriter(fileStream);
            foreach (Dinosaur d in DinoList)
            {
                writer.Write(d.Name);
                writer.Write(d.Classification);
                writer.Write(d.Sex);
            }
            writer.Close();


            Console.WriteLine("Reading Back From File");
            FileStream file = File.OpenRead("E:/Work/Dinosaur.txt");
            BinaryReader reader = new BinaryReader(file);
            for (int i = 1; i < 3; i++)
            {
                Dinosaur d = new Dinosaur();
                d.Name = reader.ReadString();
                d.Classification = reader.ReadString();
                d.Sex = reader.ReadChar();
                DinoList.AddLast(d);
            }
            reader.Close();
            Console.ReadKey();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq.Expressions;
命名空间JurrasicFinal
{
班级计划
{
类恐龙
{
公共字符串名称;
公共字符串分类;
公共性;
}
静态void Main(字符串[]参数)
{
LinkedList DinoList=新LinkedList();
恐龙恐龙1=新恐龙();
恐龙1.Name=“霸王龙”;
恐龙1.分类=“食肉”;
恐龙1.性别='M';
恐龙恐龙2=新恐龙();
Dino2.Name=“Velociraptor”;
Dino2.Classification=“肉食性”;
Dino2.Sex='F';
恐龙恐龙恐龙3=新恐龙();
Dino3.Name=“procompsognatus”;
Dino3.分类=“肉食性”;
Dino3.性别='M';
作废打印列表()
{
Console.WriteLine(“当前队列:”);
Console.WriteLine(“\n”);
foreach(恐龙化石中的恐龙d)
{
Console.WriteLine(“名称:+d.Name”);
控制台写入线(“分类:+d.Classification”);
Console.WriteLine(“Sex”+d.Sex);
Console.WriteLine(“\n”);
}
Console.WriteLine(Dino1.Name+Dino1.Sex);
}
DinoList.AddLast(Dino1);
DinoList.AddLast(Dino2);
DinoList.AddLast(Dino3);
打印列表();
Console.WriteLine(DinoList.Count);
FileStream FileStream=File.OpenWrite(“E:/Work/dingor.txt”);
BinaryWriter=新的BinaryWriter(文件流);
foreach(恐龙化石中的恐龙d)
{
作者姓名;
写作(d.分类);
作家、作家(d.Sex);
}
writer.Close();
WriteLine(“从文件读回”);
FileStream file=file.OpenRead(“E:/Work/dingor.txt”);
BinaryReader=新的BinaryReader(文件);
对于(int i=1;i<3;i++)
{
恐龙d=新恐龙();
d、 Name=reader.ReadString();
d、 分类=reader.ReadString();
d、 Sex=reader.ReadChar();
DinoList.AddLast(d);
}
reader.Close();
Console.ReadKey();
}
}
}

我想您可能正在寻找类似的东西,它依赖于用户输入并尝试进行简单的验证。我让演示一些选项变得有点过于复杂

class Sample
{
    private static int index = 0;

    static void Main(string[] args)
    {
        LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();

        while (true)
        {
            var dino = new Dinosaur();
            dino.Name = GetInput("Enter dino name (q to quit): ");
            if (dino.Name == "q" || dino.Name == "Q")
            {
                break;
            }

            dino.Classification = GetInput("Enter dino classification: ");
            char[] sexes = new char[] {'F', 'f', 'M', 'm'};
            while (true)
            {
                Console.WriteLine("Enter dino sex (M/F): ");
                dino.Sex = (char) Console.Read();
                if (sexes.Contains(dino.Sex))
                {
                    break;
                }
            }

            int inputIndex = default;
            while (true)
            {
                var indexString = GetInput($"Enter 0-index list position (max {DinoList.Count})");
                inputIndex = Convert.ToInt32(indexString);
                if (inputIndex <= DinoList.Count)
                {
                    break;
                }
            }
            DinoList.Add(inputIndex, dino);
            index++;

            Console.WriteLine("Dinosaurs:");
            Console.WriteLine(new string('-', 30));
            for (var i = 0; i < DinoList.Count; i++)
            {
                var dinosaur = (Dinosaur) DinoList.Get(i);
                Console.WriteLine("Name: " + dinosaur.Name);
                Console.WriteLine("Classification: " + dinosaur.Classification);
                Console.WriteLine("Sex: " + dinosaur.Sex);
            }
        }
    }

    private static string GetInput(string prompt)
    {
        Console.WriteLine(prompt);
        var input = Console.ReadLine();
        while (string.IsNullOrWhiteSpace(input))
        {
            input = Console.ReadLine();
        }
        return input;
    }
}

这是一种锻炼吗?如果没有,那么为什么不使用已经提供的
LinkedList
?是的,这是一种练习。我以前尝试过使用'LinkedList',但也不知道我在用它做什么,我需要在不使用C#中已经提供的一个的情况下实现它。我按照教程创建了linkedlist和node类,但现在我不知道如何使用它来实现我想要实现的目标。
我不知道如何使用它来实现我想要实现的目标。
。好的,那么你想要实现什么呢?标准的
列表
不起作用吗?通常,当你不明白某件事是如何运作的,一般的建议不会是“试着写你自己的实现”(除非练习是写你自己的实现)我正在尝试从用户那里获取dinoName的用户输入,其中dinoID将自动设置,为每个新创建的恐龙增加1>2>3等。我希望用户能够决定它在链接列表中的存储位置,这样他们就可以将它放在开始、中间、结束等位置,还应该能够从列表中的任何位置删除它。我的任务是使用LinkedList并实现我自己的LinkedList如果我不必使用它,我猜这是编程课程中令人讨厌的入门级任务之一。这是一篇有解释的文章。我建议您使用泛型参数来实现这一点,因为它们在C#中很重要,而且不难实现。您的代码看起来与我所期望的完全一样。不幸的是,为了实现我所要做的,我必须创建“LinkedList”和“Node”类,而不使用内置的“LinkedList”和“Node”这些都是C#内置的,我想我能理解。我想说的是,你们的课程很好,只要在几个地方打字就行了。一旦您将类更改为公共类LinkedList,您的IDE可能会告诉您在其他地方键入它。希望:)我使用我们的代码一起运行控制台应用程序。是的,但这是使用C#中已经实现的列表,我不能将其用于我正在做的事情。如果你能把我们的两个代码加在一起,那会很有帮助。啊,我还以为你自己写了LinkedList呢?我所做的只是编辑你在上面粘贴的内容。我会贴上我的
class Sample
{
    private static int index = 0;

    static void Main(string[] args)
    {
        LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();

        while (true)
        {
            var dino = new Dinosaur();
            dino.Name = GetInput("Enter dino name (q to quit): ");
            if (dino.Name == "q" || dino.Name == "Q")
            {
                break;
            }

            dino.Classification = GetInput("Enter dino classification: ");
            char[] sexes = new char[] {'F', 'f', 'M', 'm'};
            while (true)
            {
                Console.WriteLine("Enter dino sex (M/F): ");
                dino.Sex = (char) Console.Read();
                if (sexes.Contains(dino.Sex))
                {
                    break;
                }
            }

            int inputIndex = default;
            while (true)
            {
                var indexString = GetInput($"Enter 0-index list position (max {DinoList.Count})");
                inputIndex = Convert.ToInt32(indexString);
                if (inputIndex <= DinoList.Count)
                {
                    break;
                }
            }
            DinoList.Add(inputIndex, dino);
            index++;

            Console.WriteLine("Dinosaurs:");
            Console.WriteLine(new string('-', 30));
            for (var i = 0; i < DinoList.Count; i++)
            {
                var dinosaur = (Dinosaur) DinoList.Get(i);
                Console.WriteLine("Name: " + dinosaur.Name);
                Console.WriteLine("Classification: " + dinosaur.Classification);
                Console.WriteLine("Sex: " + dinosaur.Sex);
            }
        }
    }

    private static string GetInput(string prompt)
    {
        Console.WriteLine(prompt);
        var input = Console.ReadLine();
        while (string.IsNullOrWhiteSpace(input))
        {
            input = Console.ReadLine();
        }
        return input;
    }
}
public class Node<T>
{
    private object data;
    private Node<T> next;
    private string DinoSpecies;
    private string DinoName;

    public Node(object data, Node<T> next)
    {
        this.data = data;
        this.next = next;
    }

    public object Data
    {
        get { return this.data; }
        set { this.data = value; }
    }

    public Node<T> Next
    {
        get { return this.next; }
        set { this.next = value; }
    }
}

public class LinkedList<T>
{
    private Node<T> head;
    private int count;

    public LinkedList()
    {
        this.head = null;
        this.count = 0;
    }

    public bool Empty
    {
        get { return this.count == 0; }
    }

    public int Count
    {
        get { return this.count; }
    }

    public object this[int index]
    {
        get { return this.Get(index); }
    }

    public object Add(int index, object o)
    {
        if (index < 0)
            throw new ArgumentOutOfRangeException("Index: " + index);
        if (index > count)
            index = count;
        Node<T> current = this.head;
        if (this.Empty || index == 0)
        {
            this.head = new Node<T>(o, this.head);
        }
        else
        {
            for (int i = 0; i < index - 1; i++)
            {
                current = current.Next;
                current.Next = new Node<T>(o, current.Next);
            }
        }
        count++;
        return o;
    }

    public object Add(object o)
    {
        return this.Add(count, o);
    }

    public object Remove(int index)
    {
        if (index < 0)
            throw new ArgumentOutOfRangeException("Index: " + index);
        if (this.Empty)
            return null;
        if (index >= this.count)
            index = count - 1;
        Node<T> current = this.head;
        object result = null;
        if (index == 0)
        {
            result = current.Data;
            this.head = current.Next;
        }
        else
        {
            for (int i = 0; index < index - 1; i++) ;
            current = current.Next;
            result = current.Next.Data;
            current.Next = current.Next.Next;
        }
        count--;
        return result;
    }

    public void Clear()
    {
        this.head = null;
        this.count = 0;
    }

    public int IndexOf(object o)
    {
        Node<T> current = this.head;
        for (int i = 0; i < this.count; i++)
        {
            if (current.Data.Equals(o))
                return i;
            current = current.Next;
        }
        return -1;
    }

    public bool Contains(object o)
    {
        return this.IndexOf(o) >= 0;
    }

    public object Get(int index)
    {
        if (index < 0)
            throw new ArgumentOutOfRangeException("Index: " + index);
        if (this.Empty)
            return null;
        if (index >= this.count)
            index = this.count - 1;
        Node<T> current = this.head;
        for (int i = 0; i < index; i++)
            current = current.Next;
        return current.Data;
    }
}