![](/img/trans.png)
[英]How to add a display method to a Java Linked List implementation that displays only the first and the last 10 items on the list?
[英]How to display items in a linked list in java
大家好,我想知道如何顯示鏈表中的所有節點。 這是我到目前為止的代碼。 應該使用Manager類來操作該列表。 movieNode類為電影創建新的列表節點。 我知道我也必須使用其他東西,但是我只是想讓列表的第一個元素顯示給初學者。
public class Manager {
MovieNode head;
public Manager (){
head=null;
}
public void Add (MovieNode data) {
if (head==null){
head=data;
}
}
public void Display () {
int i=1;
MovieNode temp=head;
System.out.println("Displaying Movies");
while (head!=null) {
System.out.println(temp.getData().getName());
head=null;
}
}
}
還有,MovieNode類的代碼
public class MovieNode {
private Movie data;
private MovieNode next;
public MovieNode (){
data=null;
next=null;
}
public MovieNode (Movie data){
setData(data);
}
public void setData (Movie data){
this.data=data;
}
public Movie getData (){
return data;
}
}
希望這會幫助您入門。 這里有一些指針:
經理班
head
變量,並且無需將任何其他信息傳遞給構造函數 data
並在add方法中創建節點 head
,並檢查是否有一個節點next
列表。 如果您不需要實現此自定義方法,則建議將類實現為Iterable。 關於此主題的SO討論可以在這里找到 MovieNode類
data
並設置私有變量 next
變量的getter和setter,以便保留列表結構並遍歷列表 toString()
實現將允許直接打印此類的實例,如displayAllMovies()
方法中一樣 電影課
title
,但是您可以根據自己的規范對其進行擴展。 這是代碼:
public class Manager {
MovieNode head = null;
public void addMovie(Movie data) {
MovieNode newNode = new MovieNode(data);
if (head == null) {
head = newNode;
} else {
newNode.setNext(head);
head = newNode;
}
}
public void addMovieInOrder(Movie data) {
MovieNode newNode = new MovieNode(data);
if (head == null) {
head = newNode;
} else {
MovieNode higher = head;
MovieNode lower = null;
// find the right position for newNode
while(higher != null){
if(newNode.compareTo(higher) > 0){
lower = higher;
higher = higher.getNext();
}
else break;
}
newNode.setNext(higher);
if(higher == head) head = newNode; //inserting as head
else lower.setNext(newNode);
}
}
public void displayAllMovies() {
MovieNode node = head;
if (node == null) {
System.out.println("The list is empty!");
}
do {
System.out.println(node.getData());
node = node.getNext();
} while (node != null);
}
public static void main(String[] args) {
Manager manager = new Manager();
manager.addMovieInOrder(new Movie("ddd"));
manager.addMovieInOrder(new Movie("ccc"));
manager.addMovieInOrder(new Movie("aaa"));
manager.addMovieInOrder(new Movie("bbb"));
manager.displayAllMovies();
}
}
電影節點類:
public class MovieNode implements Comparable<MovieNode> {
private Movie data;
private MovieNode next = null;
public MovieNode(Movie data) {
this.data = data;
}
public void setData(Movie data) {
this.data = data;
}
public Movie getData() {
return data;
}
public void setNext(MovieNode node) {
this.next = node;
}
public MovieNode getNext() {
return next;
}
@Override
public String toString() {
return data.toString();
}
@Override
public int compareTo(MovieNode otherMovieNode) {
return data.compareTo(otherMovieNode.getData());
}
}
電影課:
public class Movie implements Comparable<Movie> {
private String title;
public Movie(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
@Override
public int compareTo(Movie otherMovie) {
return title.compareTo(otherMovie.title);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.