[英]How Can I Print The Previous Node and Next Node for a Doubly Linked List?
因此,當您播放專輯時,我正在創建CD播放器或iTunes跟蹤器的雙向鏈接列表。 使用AppendFront和AppendBack,我還使用了掃描儀和一條語句,該語句可以打印出曲目號,歌曲名稱以及上一首和下一首歌曲 。
我想我記下了代碼,但是,我對如何打印上一個節點和下一個節點感到困惑。 我想我的想法是將DoubleNode代碼與previous和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代碼。
//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代碼,還是仍然需要創建一個對象?
您似乎對要求您做什么感到困惑。 似乎這也可能是學校的作業,我們通常不為這里的人做家庭作業...
話雖如此,這是一個提示:
您的switch
語句完全沒有必要並且是錯誤的。 您有一個歌曲列表,用戶正在輸入要播放的歌曲; 您應該掃描 (即遍歷)列表以搜索歌曲。 您的switch
語句有效地復制了列表中已有的內容。
如果列表中有成千上萬首歌曲怎么辦? 您會像以前一樣為每首歌寫一個case
子句嗎? 如果您事先不知道列表中的內容(可能是從數據庫中加載的)怎么辦? 那么,您將如何編寫switch
語句?
您根本不需要該開關。
您應該在列表中循環搜索所需的歌曲,即從列表的開頭開始,並在遍歷歌曲時對它們進行計數。
到達請求的歌曲后,它的節點同時具有上一個和下一個鏈接,因此在打印歌曲的標題時,您還可以查看相鄰節點並打印出它們的標題。
希望它能幫助您擺脫困境。
該代碼有很多錯誤。
將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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.