因此,当您播放专辑时,我会创建一个CD播放器或iTunes跟踪器的双向链接列表。使用AppendFront和AppendBack,我还使用扫描仪和声明打印出曲目编号,歌曲名称和上一首歌以及下一首歌。
我认为我的代码已经关闭,但是,我对如何打印前一个节点和下一个节点感到困惑。我想我的想法是将我的DoubleNode代码与之前的和 next 一起使用,但是如何使用它是如此困难。
//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
下面是我正在使用的节点代码。
//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
注意评论,我知道它现在到处都是,我通常在完成代码时修复它。我是否仍然需要访问上一个或下一个的DoubleNode代码,或者我是否还需要创建一个对象,我感到很困惑?
答案 0 :(得分:0)
看来你对被要求做的事情感到有些困惑。这似乎也可能是学校作业,我们一般不会为这里的人做作业......
话虽如此,这里有一个提示:
您的switch
声明完全没必要且错误。你有一个歌曲列表,用户正在输入一些要播放的歌曲;你应该扫描(即循环)搜索歌曲的列表。您的switch
语句有效地复制了列表中的内容。
如果列表中有数千首歌曲怎么办?你会像你一样为每首歌写一个case
条款吗?如果您事先不知道列表中的内容(可能是从数据库加载的),该怎么办?那你怎么写switch
声明呢?
根本不需要那个开关。
你应该在列表中循环搜索所请求的歌曲,即从列表的头部开始并在你走过它们时对歌曲进行计数。
一旦你找到所要求的歌曲,它的节点就有前一个和下一个链接,所以当你打印歌曲的标题时,你也可以偷看邻近的节点并打印出它们的标题。 / p>
希望有助于让你解开。
答案 1 :(得分:0)
代码有很多错误。
将headNode
和tailNode
初始化为两个虚拟节点要容易得多。
然后在你的appendFront
中,你应该这样做
headNode.previous = newNode;
newNode.next = headNode;
headNode = newNode;
headNode.previous = tailNode;
tailNode.next = headNode;
同样,在appendBack
tailNode.next = newNode;
newNode.previous = tailNode;
tailNode = newNode;
tailNode.next = headNode;
headNode.previous = tailNode;
由于这是一个曲目列表,它应该是一个循环:
headNode <-> node1 <-> node2 ... <-> headNode