【问题标题】:Linked List/Node Clas - Adding User Input to Linked List链表/节点类 - 将用户输入添加到链表
【发布时间】:2020-06-25 11:11:16
【问题描述】:

我让用户输入了一个存储在变量“word”中的字符串。我现在想将此存储的变量添加到链表中。我试过使用

LinkedList<string>.Add(word);

要将变量添加到链接的 this 但它不起作用并返回错误“非静态字段、方法或属性 'LinkedList.Add(string)”需要对象引用”

我假设它与我的链接列表有关,但我不确定。

任何关于这个问题的帮助或想法都会很棒。

using System;
using System.Collections.Generic;
using System.Text;

namespace project
{
    public class LinkedList<TData>
    {

        private Node<TData> head;
        private int count;

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

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

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

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

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

            if (index > count)
                index = count;

            Node<TData> current = this.head;

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

        public TData Add(TData data)
        {
            return this.Add(count, data);
        }

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

            if (this.Empty)
                return default(TData);

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

            Node<TData> current = this.head;
            TData result;

            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(TData data)
        {
            Node<TData> current = this.head;

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

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

        public bool Contains(TData data)
        {
            return this.IndexOf(data) >= 0;
        }

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

            if (this.Empty)
                return default(TData);

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

            Node<TData> current = this.head;

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

            return current.Data;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace project
{
    public class Node<TData>
    {

        private Node<TData> next { get; set; }

        public Node(TData data, Node<TData> next)
        {
            this.Data = data;
            this.next = next;
        }

        public TData Data { get; set; }

        public Node<TData> Next
        {
            get { return this.next; }
            set { this.next = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace project
{
    class Program
    {

        static void Main(string[] args)
        {

            string word;
            Console.WriteLine("Please enter a word");
            word = Console.ReadLine();

            Console.WriteLine("You typed: " + word);
            Console.ReadKey();

            var list = new LinkedList<string>(word);

            LinkedList<string>.Add(word);

        }
    }
}

【问题讨论】:

    标签: c# linked-list nodes user-input


    【解决方案1】:

    改变这个:

            var list = new LinkedList<string>(word);
    
            LinkedList<string>.Add(word);
    

    到这里:

            var list = new LinkedList<string>();
    
            list.Add(word);
    

    list 是你在内存中的链表对象。每当你想对你的列表做某事时,你使用变量引用list,而不是对象的种类名称。我们使用变量名称而不是种类名称的原因是因为我们很可能希望在我们的程序中包含两个或更多变量:

            var postiveList = new LinkedList<string>();
            var negativeList = new LinkedList<string>();
    
            positiveList.Add("happy");
            negativeList.Add("sad");
    

    只有当你使用声明为 static 的东西时,才可以使用类型(种类)的名称——而且你的程序中没有什么是静态的,我可以指向并制作一个合适的的示范。不过你可能会用过的东西:

            int x = Convert.ToInt32("1234");
    

    这里的convert是静态的,一个类的名字。你不需要在使用之前创建一个新的 Convert ,而且你不能(也不需要)在一个程序中拥有多个 Convert ,所以让它是静态的(只有其中一个)是有意义的

    有些东西既是静态的又不是:

    var startTime = new DateTime(1970,1,1);
    var endTime = DateTime.UtcNow;
    

    startTimeendTime 都是 DateTime 的实例。然而,为了获得结束时间,我们使用了 DateTime 类型的静态属性 UtcNow。它根据计算机时钟的值为我们创建了一个新的 DateTime 并返回它。在我们这样做之前,我们不需要任何其他日期时间。如果有帮助,可以将这些静态事物想象成按需生产制造对象的工厂。只有一个工厂,它制造的东西总是同一种东西,但它可以制造数百万个


    也改变这个:

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

    到这里:

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

    要求某事然后不做任何事情是没有意义的 - 它只会使要求的东西更难使用。我感觉你没有写 LinkedList,但你可能在尝试输入单词时添加了它

    【讨论】:

    • 是的,我没有编写 LinkedList,是的,它是在我尝试输入单词时添加的,感谢它解决问题的帮助。我现在将如何将此列表打印到屏幕上? ``` foreach(string str in list) { Console.WriteLine(str); } ``` 我认为这应该可以解决问题?
    • 要计算出你的类必须是可枚举的(实现 IEnumerable 接口)
    • 我很想说这很有帮助,但我根本不知道,将它显示到控制台的最佳方式是什么,或者是否需要实现“IEnumberable 接口”我要这样做吗?
    • codeproject.com/articles/571139/foreach-on-ienumerable - 您现在不能说“foreach var item in list”,因为要使该代码正常工作,C# 需要知道如何获取它可以调用的内容“next next next ” 继续遍历它。如果你实现了 IEnumerable 接口,那么这意味着 C# 可以将你的对象视为“一些可枚举的东西”——它会调用 Next,你会给它列表中的下一个东西等
    • 谢谢,我会看看它,希望我能解决它,慢慢地看着代码变得疯狂。
    【解决方案2】:

    你写:

    LinkedList<string>.Add(word);
    

    这是对LinkedList&lt;string&gt; 类型调用静态方法Add 的尝试。编译器告诉你这是一个 instance 方法而不是 static 方法。 IE。你想打电话:

    list.Add(word);
    

    还有构造函数

    public LinkedList(string word)
    

    接受一个字符串,但不对它做任何事情。这个参数可能应该被删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多