//Garth Bruen //10.09.02 /* Set up with extra statements and methods to show the backlinking in action */ public class LL21{ private Node head; private Node next; private Node previous; class Node{ int data; Node next = null; Node previous = null; }//end Node() public void addTo(int data){ Node newNode = new Node(); newNode.previous = null; newNode.data = data; newNode.next = head; head = newNode; }//end addFirst() public Node backlink(){ Node current = head; //Begining of list Node prev = null; //temp node while(current.next != null){ prev = current; current = current.next; System.out.println("current is: "+current.data); if(current != null){ current.previous = prev; System.out.println("prev is: "+current.previous.data); } } this.printList(); return current; }//end public void printList(){ Node current = head; while(current != null){ System.out.print(current.data); if(current.previous != null){ System.out.print(current.previous.data); } current = current.next; } System.out.println(); }//end printList() public static void main(String args[]) throws Exception { LL21 unsorted = new LL21(); unsorted.addTo(1); unsorted.addTo(2); unsorted.addTo(3); unsorted.addTo(4); unsorted.addTo(5); unsorted.addTo(6); unsorted.addTo(7); unsorted.addTo(8); unsorted.addTo(9); unsorted.printList(); unsorted.backlink(); }//End main }