Home > database >  Add and Print new Nodes
Add and Print new Nodes

Time:06-23

I was working on a program to add nodes to a list, but I seem to be doing something wrong...

My java program has three Classes; Demo, Lista and Node

Class Node:

public class Node {
    
    private int num;
    private Node tail;
    private Node head; 

    public Node (int num, Node head, Node tail) {
        this.num = num;
        this.head = head;
        this.tail = tail;
    }
}

Class Lista:

public class Lista {
    
    private Node nil;
    
    public Lista () {
        nil = null;
    }
    
    public void add (int num) {
        Node newNode = new Node(num, head, tail);
        
        if (head == null) {
            head = newNode;
            tail = newNode;
        }
    }
    
    public void display () {
        Node current = head;
        
        while(current != null) {
            System.out.print(current.num);
        }
    }
}

Class Demo:

public class Demo {
    
    public static void main ( String [] args) {
        Lista lista = new Lista();
        lista.add(3);
        lista.add(9);
        lista.add(7);
        lista.display();
    }
}

Demo class is to add the different nodes to the list "lista". Class Node has num, head which is the next one and tail which is the previous one. How can I go about getting Class Lista to be able to use head and tail from Class Node? And if it is possible would this code work when running Demo? What should I change/modify to get this to work?

CodePudding user response:

You may want to modify your code something like this:

class Node {
    int num;
    Node prev;
    Node next;

    Node(int num) {
        this.num = num;
    }

    Node(int num, Node prev, Node next) {
        this.num = num;
        this.prev = prev;
        this.next = next;
    }

    void setPrev(Node prev) {
        this.prev = prev;
    }

    void setNext(Node next) {
        this.next = next;
    }
}


class Lista {
    Node root;
    Node endNode;

    public void add(int num) {
        Node n = new Node(num);

        if (root == null) {
            root = n;
        } else {
            n.setPrev(endNode);
            endNode.setNext(n);
        }
        endNode = n;
    }

    public void display() {
        Node iterateeNode = root;

        while (iterateeNode != null) {
            System.out.print(iterateeNode.num   " ");
            iterateeNode = iterateeNode.next;
        }
    }
}

CodePudding user response:

The selected answer is technically not correct. For a (single) Linked List, all your Lista need is a single (head) node. Additionally, the Node class needs a single (next) Node field.

The following is a potential implementation of Node:

public class Node {
    
  private Node next;
  private int value;

  public Node(int value) {
      this.value = value;
  }

  public boolean hasNext() {
      return next != null;
  }
  
  public Node next() {
    return next;
  }
  
  public void add(Node node) {
    if (next == null) {
        next = node;
    } else {
        Node temp = next;
        while (temp != null) {
            temp = temp.next;
        }
        temp = node;
    }
  }

  @Override
  public String toString() {
    return String.valueOf(value);
  }
}

The add() method will insert the new node in next if it is null. Otherwise, it will traverse the nodes until it finds the tail node (the one where next is null).

The Lista has only the first element in the list (head node).

public class Lista {
    private Node head;
    
    public void add(Node node) {
        if (head == null) {
            head = node;
        } else {
            Node temp = head;
            while (temp.next() != null) {
                temp = temp.next();
            }
            temp.add(node);
        }
    }
    // Other methods
}

When the add() function in the list is called, it will either add the new node as the head (if the list doesn't have one already) or rely on the already added nodes to figure out where the end of the list is in order to insert the new node.

Lastly, to display the list, just override the toString() method in node and add the "toString" value to a string buffer and send the concatenated string value to the console similar to the the code below.

public void display() {
    StringBuilder buff = new StringBuilder("[");
    buff.append(head);
    if (head != null) {
        Node next = head.next();
        buff.append(",");
        while (next != null) {
            buff.append(next);
            next = next.next();
            buff.append(",");
        }
    }
    buff.append("]");
    int idx = buff.lastIndexOf(",");
    buff.replace(idx, idx 1, "");
    System.out.println(buff.toString());
}

Executing the following displays [3,9,7] as expected.

public class Demo {
  public static void main ( String [] args) {
      Lista lista = new Lista();
      lista.add(new Node(3));
      lista.add(new Node(9));
      lista.add(new Node(7));
      lista.display();
  }
}
  •  Tags:  
  • java
  • Related