打印排序的链表问题 [英] Printing Sorted Linked List Question

查看:50
本文介绍了打印排序的链表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提示用户输入,直到输入的值为-1.输入-1时,列表将按排序顺序打印出来.但是,该程序不能那样工作.理想的运行就像这样....

I am prompting user input until the value entered is -1. When a -1 is entered, the list prints out in sorted order. However, the program doesn't work like that. An ideal run goes like this....

输入:4 3 2 -1

Input: 4 3 2 -1

输出:2 3 4

我的输出:3 4带有"NullReferenceException"错误出现在" Console.WriteLine(cur.number);"

My Output: 3 4 with a "NullReferenceException" error at "Console.WriteLine(cur.number);"

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedListlab
{
    public class LinkedList
    {
        public class Node
        {
            public Node link;
            public int number { get; set; }
            public Node(int num)
            {
                this.number = num;
            }
        }
        private Node Head;
        public int count = 0;
        //adds new node to list
        public void AddNewNode(int input)
        {
            if (input != -1)
            {
                this.count++;
                Node ToAdd = new Node(input);
                ToAdd.number = input;
                if (count == 1)
                {
                    ToAdd.link = null;
                    Head = ToAdd;
                    return;
                }
                if (input < Head.number)
                {
                    ToAdd.link = Head;
                    Head = ToAdd;
                }
                else
                {
                    Node current = Head;
                    Node prior = Head;
                    while (current != null)
                    {
                        if (input < current.number)
                        {
                            break;
                        }
                        prior = current;
                        current = current.link;
                    }
                    ToAdd.link = current;
                    prior.link = ToAdd;
                }
            }
        }
        //prints nodes
        public void PrintAll()
        {
            Node cur;
            cur = this.Head;
            Console.WriteLine("Output:");
            while (cur != null)
            {
                cur = cur.link;
                Console.WriteLine(cur.number);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LinkedList list = new LinkedList();
            int input;
            do
            {
                Console.WriteLine("Enter an integer, or -1 to exit the program...");
                input= Convert.ToInt32(Console.ReadLine());
                list.AddNewNode(input);             
            }
            while (input != -1);
            list.PrintAll();
            Console.Read();
        }
    }
}

推荐答案

我正在提示用户输入,直到输入的值为-1.输入-1时,列表将按排序顺序打印出来.但是,该程序不能那样工作.理想的运行就像这样....

I am prompting user input until the value entered is -1. When a -1 is entered, the list prints out in sorted order. However, the program doesn't work like that. An ideal run goes like this....

输入:4 3 2 -1

Input: 4 3 2 -1

输出:2 3 4

我的输出:3 4带有"NullReferenceException"错误出现在" Console.WriteLine(cur.number);"

My Output: 3 4 with a "NullReferenceException" error at "Console.WriteLine(cur.number);"



            Console.WriteLine("Output:");
            while (cur != null)
            {
                cur = cur.link;
                Console.WriteLine(cur.number);
            }
        }
    }

如果您在此处更改订单会怎样?

What happens if you change the order here?

while (cur != null)
{
    //cur = cur.link;
    Console.WriteLine(cur.number);
    cur = cur.link; // <<<<
}

-韦恩


这篇关于打印排序的链表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆