title: How Can I Print The Previous Node and Next Node for a Doubly Linked List?
body: <p>So I'm creating a doubly linked list of a CD player or iTunes tracker when you play an album. Using the AppendFront and AppendBack, I'm also using a scanner and a statement that prints out the track number, song name and the <strong>previous song</strong> and the <strong>next song</strong>.</p>
  
  <p>I think I got the code down, however, I am so puzzled on how to print the previous node and the next node. I think I have the idea of using my DoubleNode code with <em>previous</em> and <em>next</em>, but it's been so hard on how to use it.</p>
  
  <pre><code>//CD PROGRAM CODE
  package dynamicData; //package is set
  
  import java.util.Scanner; //imported the scanner to type
  
  public class DoubleLinkedListTester {
  
  public static void main(String[] args) {
      System.out.println("Welcome to your BeoCenter 2 CD Player.");
      System.out.println("Inserted CD: Beyoncé - I Am... Sasha Fierce (2008)");
      System.out.println("Tracklist: ");
      System.out.println("No. – Name");
      appendFront(1, "If I Were A Boy");
      appendBack(2, "Halo");
      appendBack(3, "Disappear");
      appendBack(4, "Broken-Hearted Girl");
      appendBack(5, "Ave Maria");
      appendBack(6, "Satellites");
      appendBack(7, "Single Ladies (Put a Ring on It)");
      appendBack(8, "Radio");
      appendBack(9, "Diva");
      appendBack(10, "Sweet Dreams");
      appendBack(11, "Video Phone [featuring Lady Gaga]");
      appendBack(12, "Ego [featuring Kanye West]");
      appendBack(13, "Roc");
  
      System.out.println(" ");
      //tracklist();
      Scanner album = new Scanner(System.in); //scanner is set
      System.out.print("Which track number would you like to hear?: "); //asking the use what their favourite song is
      int input = album.nextInt(); //would type in their input
  
      switch (input) {
          case 1 : scan(input, input, "If I Were A Boy");
              break;
          case 2 :  scan(input, input, "Halo");
              break;
          case 3 :  scan(input, input, "Disappear");
              break;
          case 4 :  scan(input, input, "Broken-Hearted Girl");
              break;
          case 5 :  scan(input, input, "Ave Maria");
              break;
          case 6 :  scan(input, input, "Satellites");
              break;
          case 7 :  scan(input, input, "Single Ladies (Put A Ring On It)");
              break;
          case 8 :  scan(input, input, "Radio");
              break;
          case 9 :  scan(input, input, "Diva");
              break;
          case 10 :  scan(input, input, "Sweet Dreams");
              break;
          case 11 :  scan(input, input, "Video Pohone [featuring Lady Gaga]");
              break;
          case 12 :  scan(input, input, "Ego [featuring Kanye West]");
              break;
          case 13 :  scan(input, input, "Roc");
              break;
              default : System.out.println("Invalid selection. Please restart program.");
              break;
          }
  
  } //main void ends
  
  private static DoubleNode headNode =  null;
  private static DoubleNode tailNode =  null;
  private static int size;
  
  public int size() {
      return size;
      }
  
  //add new node at the front of the DLL
  public static void appendFront(int trackNumber, String songName) {
      DoubleNode newNode = new DoubleNode(trackNumber, songName, headNode, null);
  
      if(headNode != null ) {
          headNode.previous = newNode;
          } //end of if 1
  
      headNode = newNode;
      if(tailNode == null) {
          tailNode = newNode;
          } //end of if 2
      size++;
      System.out.println(trackNumber+ "   –   " + songName);
  }
  
  //add at the back
  public static void appendBack(int trackNumber, String songName) {
      DoubleNode newNode = new DoubleNode(trackNumber, songName, headNode, null);
  
      if(tailNode != null ) {
          tailNode.previous = newNode;
          } //end of if 1
  
      tailNode = newNode;
      if(headNode == null) {
          headNode = newNode;
          } //end of if 2
      size++; //size is then added on
      System.out.println(trackNumber+ "   –   " + songName);
  
  }
  
  public static void tracklist() {
      DoubleNode currentNode = headNode;
  
      while (currentNode != null ) {
  
          System.out.println(currentNode.data + " — " + currentNode.description);
          System.out.println("Next Link: " + currentNode.next);
          currentNode = currentNode.next;
          System.out.println(" ");
      }
  }
  
  public static void scan(int input, int trackNumber, String songName) {
  
  if (input &gt;= 1 ) {
      System.out.println(" ");
      System.out.println("Song selected.");
      System.out.println("Now playing: ");
      System.out.println("Track " + trackNumber + " – " + songName);
      System.out.println("Previous song: " + songName);
      System.out.println("Next song: " + songName);
      System.out.println(" ");
      } else {
      System.out.println("Goodbye!");
  } //end of if
  }
  
  //scan backwards
  public static void scanBackward() {
      System.out.println("Scanning backwards through playlist.");
  
      DoubleNode temp = tailNode;
  
      while(temp != null){
          System.out.println(temp.data + " " + temp.description);
          temp = temp.previous;
      }
    }
  } //class ends
  </code></pre>
  
  <p>And down below is the Node code I'm using.</p>
  
  <pre><code>//Node Code
  package dynamicData; //package is set
  
  public class DoubleNode { //class is set
  
  public int data; //created a public integer called data to store the int number
  public DoubleNode next; //created a public Node called next to call the next element or node in the array
  public DoubleNode previous;
  public String description; //created a public String called description to store the description of what the element or node is
  
  
  public DoubleNode (int trackNumber, String songName, DoubleNode next, DoubleNode previous) { //the object is set as we recall the node class
      data = trackNumber; //we set the data as the track number we are on
      description = songName; //we set the description as the songs name to know what we are on
      this.next = next; //using this.next to make sure the computer is getting from this public node and not mixing it up or getting confused through the other next up above
      this.previous = previous;
      //this next is using the next element or node in the array
  } //end of inner Node
  
  public String toString(){ //public string to return the name of the site
      return description; //would return the description, which is the site's name
  } //end of string
  } //end of class Node
  </code></pre>
  
  <p>Mind the commenting, I know it's all over the place right now and I normally fix it when I finish the code. I am puzzled whether if I still need to access my DoubleNode code for the previous or next or if I still need to create an object?</p>