简体   繁体   中英

How to display items in a linked list in java

Hi all im wondering how to display all nodes in a linked list. Heres the code I have so far. The Manager class is supposed to operate the list. The movieNode class creates new list nodes for the movie. I know I have to use other things as well but im just trying to get the first element of the list to display for starters.

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;
        }
    }
    }

also, the code for the MovieNode class

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;
        }
    }

Hopefully this will help you get started. Here are some pointers:

Manager Class

  • You don't need an explicit constructor for Manager, as you can initialize the head variable in line and you're not passing any other information to the constructor
  • Method names in Java are conventionally camel case and start with lower-case letter
  • When you add a new item to the linked list, you can pass in just the data and create the node in the add method
  • Assuming you don't need to maintain any special order, you can insert the new item to the head of the list. This saves the time to go through the whole list to find the tail or keeping a reference to the tail.
  • To display all the movies, you just need to start with the head and check if there is a node next in list. If you don't need to implement this custom method, I would recommend implementing the class as Iterable. A SO discussion on this topic can be found here

MovieNode Class

  • You only need one constructor that takes the data and sets the private variable
  • You also need the getter and setter for the next variable in order to hold the list structure and iterate through the list
  • The toString() implementation will allow to print an instance of this class directly, as in displayAllMovies() method

Movie Class

  • This class just holds the title of the movie for now, but you can extend it according to your spec.

Here is the code:

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();
    }
}

Movie Node class:

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());
    }
}

Movie class:

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);
    }
}

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