none
Printing Sorted Linked List Question RRS feed

  • Question

  • 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....

    Input: 4 3 2 -1

    Output: 2 3 4

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

    Here is my code:

    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();
            }
        }
    }



    • Edited by jcampos13 Saturday, April 1, 2017 11:16 PM
    Saturday, April 1, 2017 11:05 PM

All replies

  • 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....

    Input: 4 3 2 -1

    Output: 2 3 4

    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; // <<<<
    }
    

    - Wayne

    Sunday, April 2, 2017 12:27 AM