简体   繁体   中英

How to create a display method for this (java) generic singly linked list?

The problem is basically to display the results once the user is done inputting data and since this is a generic linked list, I don't know the syntax.

Here is the code:

import java.util.Scanner;

public class singlyyyyyy<E>{

   class Node<E>{
      private E element;
      private Node<E> next;

      public Node(E e, Node<E> n){
         element = e;
         next = n;
      }

      public E getElement(){
         return element;
      }

      public Node<E> getNext(){
         return next;
      }

      public void setNext(Node<E> n){
         next = n;
      }
   }

   private Node<E> head = null;
   private Node<E> tail = null;
   int size = 0;

   public int size(){
      return size;
   }

   public boolean isEmpty(){
      return size == 0;
   }

   public E first(){
      if(isEmpty())
         return null;

         return head.getElement();
   }

   public E last(){
      if(isEmpty())
         return null;

         return tail.getElement();
   }

   public void addFirst(E e){
      head = new Node<>(e, head);
      if(size == 0)
         head = tail;
         size++;
   }

   public void addLast(E e){
      Node<E> newNode = new Node<>(e, null);
      if(isEmpty())
         head = newNode;
      else
         tail.setNext(newNode);
         tail = newNode;
         size++;
   }

   public E removeFirst(){
      if(isEmpty())
         return null;
      E a = head.getElement();
      head = head.getNext();
      size--;

      if(size == 0)
         tail = null;
         return a;
   }


     public void main(){
      Scanner scan = new Scanner(System.in);      
      singlyyyyyy<E> node = new singlyyyyyy<>();
      E e;
      System.out.print("Enter size : ");
      node.size();
      System.out.println("----------Menu----------");
      System.out.println("1. Add First Element    ");
      System.out.println("2. Add Last Element     ");
      System.out.println("3. Remove First         ");
      System.out.println("4. Display              ");
      System.out.println("0. Terminate Program    ");
      System.out.println("------------------------");
      int choice = scan.nextInt();

     switch(choice){
      case 1:
         node.addFirst(e);
         break;
      case 2:
         node.addLast(e);
         break;
      case 3:
         node.removeFirst();
         break;
      case 4:
         //node.display();
         break;
      case 0:
         System.out.println("\nProgram Terminated.\n");
         break;
      }
   }
}

I've yet to create a static main method and I'm planning to separate it into another class and will just call the main method which is inside the singlyyyyyy<E> class.

The reasonable thing here is to override the method toString() which each class inherits from java.lang.Object.

What you want to do here is: use a class like StringBuilder in order to create a single string that "lists" all the nodes in your list. Meaning: you call toString() on each of your nodes; and you append that string to the Stringbuilder; and in the end, you create a single string out of that which your list-class toString() returns. Then you can do a simple out.println(yourListObject).

And hint: read about java naming conventions. Class names start uppercase; and hint: don't try to be funny. Use names that say what the thing is; not like singlyyyy. That is just utter nonsense; and only distracts you and anybody reading your source code.

First, in addFirst() , the line head = tail; should be tail = head; . If that is meant to be a standard main method, you may declare it as public static void main(String... args) .

Add a toString method to you list class:

@Override
public String toString() {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    if (!isEmpty()) {
        buf.append(head.getElement());
        Node<E> nodeRef = head.getNext();
        while (nodeRef != null) {
            buf.append(", ");
            buf.append(nodeRef.getElement());
            nodeRef = nodeRef.getNext();
        }
    }
    buf.append(']');
    return buf.toString();
}

Then it's easy:

    case 4:
        System.out.println(node);
        break;

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