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."); } } } }