Question sorted linked lists

leoncari

New member
Joined
Jan 19, 2015
Messages
1
Programming Experience
1-3
Hi
Please, I need your help. I created the following C# program in Visual Studio 2012 Ultimate:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ProAssign1Version2
{
    class Program
    {
        public class Node
        {
            public object data;
            public Node next;
            public Node(object data)
            {
                this.data = data;
            }
        }
        public class LinkedList
        {
            Node head;
            Node current;
            public Node Head //Expose a public property to get to head of the list
            {
                get { return head; }
            }
            public void Add(Node n)
            {
                if (head == null)
                {
                    head = n; // point head to first added node
                    current = head; // set current to head
                }
                else
                {
                    current.next = n; //Set current next to newly added node.
                    current = current.next;  //Set new current to current next.
                }
            }
            public void MergeSortedList(Node first, Node second)
            {
                // We would be always adding nodes from the second list to the first one
                // If second node head data is more than first one exchange it
                if (Convert.ToInt32(first.next.data.ToString())
                        > Convert.ToInt32(second.data.ToString()))
                {
                    Node t = first;
                    first = second;
                    second = t;
                }
                head = first; //Assign head to first node
                //We need to assign head to first because first will continuosly be changing and so we want to store the beginning of list in head.
                while ((first.next != null) && (second != null))
                {
                    if (Convert.ToInt32(first.next.data.ToString())
                        < Convert.ToInt32(second.data.ToString()))
                    {
                        first = first.next; //Iterate over the first node
                    }
                    else
                    {
                        Node n = first.next;
                        Node t = second.next;
                        first.next = second;
                        second.next = n;
                        first = first.next;
                        second = t;
                    }
                }
                if (first.next == null) // Means there are still some elements in second
                    first.next = second;
            }
            public void PrintNodes()
            {
                Node current = head;
                while (current != null)
                {
                    Console.Write(current.data + " ");
                    current = current.next;
                    




                } // end while
            }
        }
        
        static void Main(string[] args)
        {
            LinkedList l1 = new LinkedList();
            //l1.Add(new Node("1"));
            l1.Add(new Node("2"));
            l1.Add(new Node("100"));
            l1.Add(new Node("4"));
            l1.Add(new Node("120"));
            l1.Add(new Node("8"));
            l1.Add(new Node("3"));
            l1.Add(new Node("5"));




            LinkedList l2 = new LinkedList();
            l2.Add(new Node("10"));
            l2.Add(new Node("30"));
            l2.Add(new Node("34"));
            LinkedList list = new LinkedList();
            list.MergeSortedList(l1.Head, l2.Head);
            list.PrintNodes();
            Console.ReadLine();
            
        }
    }
}

The problem is that before the new node is inserted into the linkedlist, my code needs to find the "right" spot for it. In
other words, after the new node has been inserted, the linkedlist should be a sorted list, ascending or
descending. My solution cannot generate the expected sorted linked list. Please, let me know your solution. Thank you so much. I do appreciate it.
 
Last edited by a moderator:
Back
Top Bottom