简体   繁体   中英

How Can I Print The Previous Node and Next Node for a Doubly Linked List?

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 previous song and the next song .

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 previous and next , but it's been so hard on how to use it.

//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 >= 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

And down below is the Node code I'm using.

//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

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?

It seems you are a bit confused about what you are being asked to do. It also seems that this is possibly a school assignment, and we generally don't do homework for people here...

Having said that, here is a hint:

Your switch statement is completely unnecessary and wrong. You have a list of songs, the user is entering a number of the song to play; you are supposed to scan (ie loop through) the list in search of the song. Your switch statement effectively duplicates what's in the list already.

What if there were thousands of songs in the list? Would you write a case clause for each song like you did? What if you didn't know in advance what was in the list (maybe it was loaded from a database)? How would you write your switch statement then?

You don't need that switch at all.

You are supposed to loop through the list in search of the requested song, ie start at the head of the list and count the songs as you walk through them.

Once you get to the requested song, its node has both a previous and a next link, so as you are printing the title of the song, you can also peek at the neighboring nodes and print out their titles as well.

Hope it helps to get you unstuck.

The code has a lot of bugs.

It is much easier to initialize the headNode and tailNode as two dummy nodes.

Then in your appendFront , you should do this

headNode.previous = newNode;
newNode.next = headNode;

headNode = newNode;
headNode.previous = tailNode;
tailNode.next = headNode;

Similarly, in your appendBack

tailNode.next = newNode;
newNode.previous = tailNode;

tailNode = newNode;
tailNode.next = headNode;
headNode.previous = tailNode;

Since this is a track list, it should be a cycle:

headNode <-> node1 <-> node2 ... <-> headNode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM