Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a toString method to print out a LinkedList

I'm having trouble with a project I have for my OOP class. I'm nearly finished, but still lack a toString method and a main method. Not really sure how to go about it and would appreciate any help. I want my toString method to function as follows:

Returns a string representation of all the items stored in the list. A string representation of an empty list looks like head--><--tail A string representation of a non-empty list looks like: head-->134<-->-8<-->42<-->1<--tail

public class IntegerNode{

    private IntegerNode next;
    private IntegerNode prev;
    private int data;

    public IntegerNode(int data){
        next = next; 
        prev = prev;
        data = data;     
    }

    public int getData(){
        data = data;
        return this.data;   
    }

    public IntegerNode getNext(){
        return next;
    }

    public IntegerNode getPrevious(){
        return prev;
    }

    public void setNext(IntegerNode in){
        prev = in;
    }

    public void setPrevious(IntegerNode in){
        prev = in;
    }

}

and here is what I have so far in my IntegerLinkedList class

public class IntegerLinkedList{

    private IntegerNode head;
    private IntegerNode tail;

    public IntegerLinkedList(){
        head = null;
        tail = null;
    }

    public void addFirst(int x){
        IntegerNode nH = new IntegerNode(x);
        if (head == null) {
            head = nH;
            tail = nH;
        }else{
            head.setPrevious(nH);
            nH.setNext(head);
            head = nH;

        }
    }

    public void addLast(int x){
        IntegerNode t = new IntegerNode(x);
        if (tail == null){
            head = t;
            tail = t;
        }else{
            tail.setNext(t);
            t.setPrevious(tail);
            tail = t;
        }
    }

    public int peekFirst(){
        return head.getData();
    }

    public int peekLast(){
        return tail.getData();
    }

    public String toString(){
        if (head == null && tail == null){
            String empty = "head--><--tail";
            return empty;
        }else{
            String h = "Head--> " + head;
            String t = tail + " <--Tail";
            String m = " <--> ";
           // while(IntegerNode.getNext() != null) 
           //}
           //return h + m + t;

        }
    }

    public int pollFirst(){
        int x = head.getData();
        head = head.getNext();
        head.setPrevious(null);
        return x;
    }

    public int pollLast(){
        int x = tail.getData();
        tail = tail.getPrevious();
        tail.setNext(null);
        return x;
    }

}

I'm thinking a while loop is the way to go here, but then again I'm not sure.

like image 542
Paul Daniels Avatar asked Jun 11 '16 17:06

Paul Daniels


1 Answers

Here's how to write it:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
    if (head == null && tail == null){
        String empty = "head--><--tail";
        return empty;
    }else{
       StringBuilder sb = new StringBuilder();
       sb.append("Head-->");

       IntegerNode curr = head;
       sb.append(curr.getData());
       curr = curr.getNext();
       while(curr != null) {
           sb.append("<-->");
           sb.append(curr.getData());
           curr = curr.getNext();
       }
       sb.append("<--tail");
       return sb.toString();
    }
}

As an alternative, you can simplify the logic to not have an outer if else:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
   StringBuilder sb = new StringBuilder();
   sb.append("Head-->");

   IntegerNode curr = head;

   if (curr == null)
   {
      sb.append("<--tail");
      return sb.toString();
   }

   sb.append(curr.getData());
   curr = curr.getNext();
   while(curr != null) {
       sb.append("<-->");
       sb.append(curr.getData());
       curr = curr.getNext();
   }
   sb.append("<--tail");

   return sb.toString();
}
like image 136
Michael Markidis Avatar answered Sep 26 '22 08:09

Michael Markidis