Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Sorting ascending/descending methods not working on double linked list

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting ascending/descending methods not working on double linked list

    Hi there. I'd like some help with the following code. I wrote displayAscending() and displayDescending() methods to this double linked list and it is not working at all. Logically it seems fine to me. I positioned the head in the beginning in the ascending method; created a variable named data1 as an auxiliar variable so it can store the values that are going to be moved; and moved the values. Same thing for the descending method but instead of the head I put the tail and move left the list, instead of right. I'd really appreciate some help because I've been stuck here for hours. Thank you

    import java.util.*;
     
    class node {
     
        int data;
        node left;
        node right;
     
        node(int d, node l, node r) {
            data = d;
            left = l;
            right = r;
        }
    }
     
    class Link {
     
        node head;
        node tail;
     
        public void insertfront(int data) {
            int flag = 0;
            node newnode = new node(data, null, null);
            if (head == null) {
                head = newnode;
                tail = head;
                tail.right = head;
                head.left = tail;
                flag = 1;
            } else {
                newnode.right = head;
                newnode.left = tail;
                tail.right = newnode;
                head = newnode;
                flag = 1;
            }
            if (flag == 1) {
                System.out.println("element " + data + "is inserted.");
            } else {
                System.out.println("element " + data + "is not inserted.");
            }
        }
     
        public void insertend(int data) {
            node newnode = new node(data, null, null);
            node current = head;
            while (current.right != head) {
                current = current.right;
            }
            current.right = newnode;
            newnode.left = current;
            newnode.right = head;
            tail = newnode;
        }
     
        public void insertatPosition(int data, int pos) {
            node newnode = new node(data, null, null);
            node current = head;
            node previous = head;
            int i = 1;
            while (i != (pos - 1)) {
                current = current.right;
                i++;
            }
            newnode.right = current.right;
            newnode.left = current;
            current.right = newnode;
            //current=current.right;  
        }
     
        public void display() {
            node current = head;
            do {
                System.out.print("     " + current.data);
                current = current.right;
            } while (current != head);
            System.out.println();
        }
     
        public void displayDescending() {
            node current = tail;
            int data1;
            while (current.left != null) {
                if (current.data > current.left.data) {
                    data1 = current.left.data;
                    current.left.data = current.data;
                    current.data = data1;
                    current = current.left;
                }
     
                System.out.println(current.data + "    ");
            }
            System.out.println();
        }
     
        public void displayAscending() {
            node current = head;
            int data1;
            while (current.right != null) {
                if (current.data > current.right.data) {
                    data1 = current.right.data;
                    current.right.data = current.data;
                    current.data = data1;
                    current = current.right;
                }
     
                System.out.println(current.data + "    ");
            }
            System.out.println();
        }
    }
     
    class Circular_Double {
     
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            Link obj = new Link();
            while (true) {
                System.out.println("*******Please select the option.*********");
                System.out.println("\t1.Insert the element at first in the list."
                        + "\n\t2.Insert at element at end."
                        + "\n\t3.Insert element at certain positon."
                        + "\n\t4.Insert element at last."
                        + "\n\t5.Delete the element."
                        + "\n\t6.Search element."
                        + "\n\t7.display the Linked list."
                        + "\n\t8.display the Linked List ordered ascending"
                        + "\n\t9.display the Linked List ordered descending"
                        + "\n\t10.Exit from the program.");
     
                int num = scan.nextInt();
                switch (num) {
                    case 1:
                        System.out.println("Insert the element.");
                        int n = scan.nextInt();
                        obj.insertfront(n);
                        break;
                    case 2:
                        System.out.println("Insert the element.");
                        n = scan.nextInt();
                        obj.insertend(n);
                        break;
                    case 3:
                        System.out.println("Insert the element.");
                        n = scan.nextInt();
                        System.out.println("POSITION");
                        int p = scan.nextInt();
                        obj.insertatPosition(n, p);
                        break;
                    case 7:
                        obj.display();
                        break;
                    case 8:
                        obj.displayAscending();
                        break;
                    case 9:
                        obj.displayDescending();
                        break;
                    case 10:
                        System.exit(0);
                        break;
                    default:
                        System.out.println("U entered invalied data.So,enter valied data.");
                }
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting ascending/descending methods not working on double linked list

    it is not working at all.
    Can you post the print outs that show what is happening?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting ascending/descending methods not working on double linked list

    Let's suppose I inserted the number 4 and then the number 10. If I press number 8 to order it ascending, it gets into an infinite loop printing the number 10.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting ascending/descending methods not working on double linked list

    I inserted the number 4 and then the number 10. If I press number 8
    When I enter: 4 10 8 I (I put it in the Scanner constructor) I get this output:

    Running: java -client -cp . Circular_Double

    *******Please select the option.*********
    1.Insert the element at first in the list.
    2.Insert at element at end.
    3.Insert element at certain positon.
    4.Insert element at last.
    5.Delete the element.
    6.Search element.
    7.display the Linked list.
    8.display the Linked List ordered ascending
    9.display the Linked List ordered descending
    10.Exit from the program.
    U entered invalied data.So,enter valied data.
    *******Please select the option.*********
    1.Insert the element at first in the list.
    2.Insert at element at end.
    3.Insert element at certain positon.
    4.Insert element at last.
    5.Delete the element.
    6.Search element.
    7.display the Linked list.
    8.display the Linked List ordered ascending
    9.display the Linked List ordered descending
    10.Exit from the program.

    0 error(s)

    For testing I preload all of the Scanner input in its constructor:
            Scanner scan = new Scanner("4 10 8\n"); //System.in);

    A copy of the console would have shown me exactly what was entered on the keyboard. I need that for testing. I don't care to waste time responding to prompts and possibility entering the wrong value. Put it in the program and then it is always fast and easy and the same every time.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Infinite loop on double linked list
    By krafterwerk in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 12th, 2014, 10:47 AM
  2. Bubble Sort on double Linked List
    By Wolverine89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2013, 12:44 PM
  3. Replies: 1
    Last Post: September 26th, 2012, 12:31 PM
  4. Sorting a Linked List
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2012, 07:07 AM
  5. Replies: 3
    Last Post: October 22nd, 2011, 01:53 AM