Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何手动将节点添加到链接列表C#_C#_Linked List_Singly Linked List - Fatal编程技术网

如何手动将节点添加到链接列表C#

如何手动将节点添加到链接列表C#,c#,linked-list,singly-linked-list,C#,Linked List,Singly Linked List,我用C#编写了一个链接列表程序,它创建了一个包含10个随机节点的链接列表。但是我想自己手动输入数字。我不知道如何添加函数,以便在程序运行时自己手动添加数字,而不是随机生成数字?我尝试了下面的代码,但似乎不起作用。任何帮助都将不胜感激 namespace Linked_List { class Program { public static void Main(String[] args) { Console.WriteLine("Linked List

我用C#编写了一个链接列表程序,它创建了一个包含10个随机节点的链接列表。但是我想自己手动输入数字。我不知道如何添加函数,以便在程序运行时自己手动添加数字,而不是随机生成数字?我尝试了下面的代码,但似乎不起作用。任何帮助都将不胜感激

namespace Linked_List
{
class Program
{

    public static void Main(String[] args)
    {
        Console.WriteLine("Linked List: ");
        int size = 10;
        int[] a;

        a = new int[size + 1];

        for (int i = 1; i <= size; i++)

        {
            Console.WriteLine("What value do you want to add?");
            a[i] = Convert.ToInt32(Console.ReadLine());
            Node n = new Node(a[i]);


            Node head = List(a, n);
            print_nodes(head); //used to print the values
            Console.ReadLine();
        }

    }
    public class Node
    {
        public int data;
        public Node next;
    };

    static Node add(Node head, int data) //Add nodes
    {
        Node temp = new Node();
        Node current;
        temp.data = data;
        temp.next = null; //next point will be null

        if (head == null) // if head is null
            head = temp;
        else
        {
            current = head;
            while (current.next != null)
                current = current.next;
            current.next = temp; //links to new node
        }
        return head;
    }

    static void print_nodes(Node head) //print the values in list
    {
        while (head != null) //while head is not null
        {
            Console.Write(head.data + " "); //outputs the numbers
            head = head.next;
        }
    }

    static Node List(int[] a, int n)
    {
        Node head = null; //head is originally null
        for (int i = 1; i <= n; i++)
            head = add(head, a[i]);
        return head;
    }

   
}
}
名称空间链接列表
{
班级计划
{
公共静态void Main(字符串[]args)
{
Console.WriteLine(“链接列表”);
int size=10;
int[]a;
a=新整数[大小+1];

对于(int i=1;i这将要求您手动输入值

public static void Main(String[] args)
{
    Console.WriteLine("Linked List: ");
    int n = 10;
    Random r = new Random(); 
    int[] a;
    a = new int[n + 1];

    for (int i = 1; i <= n; i++)
        a[i] = int.Parse(Console.ReadLine()); // here, no extra class needed

    Node head = List(a, n);
    print_nodes(head); 
    Console.ReadLine();
}
publicstaticvoidmain(字符串[]args)
{
Console.WriteLine(“链接列表”);
int n=10;
随机r=新随机();
int[]a;
a=新整数[n+1];

对于(int i=1;i如果你编写了这段代码,你应该知道是什么生成了随机数。知道了这一点,到目前为止你尝试过做什么?任务是什么?是的,我知道是什么生成了随机数,但是如果我去掉随机函数,我无法解决如何手动添加这些数字数字数字。这些数字应该来自哪里?我想手动输入我想说的是,我不想使用随机生成器,而是想自己实际输入它们,但不知道在程序运行或硬编码时如何进行?