Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding at the End

Tags:

java

Thank you for taking the time to read this, I'm currently taking a Java class and the professor told us that a good practice to understand links would be to make a doubly linked list. I have made a singly linked list but I am having trouble converting it to a doubly linked list. The problem I am having is I'm trying to add a number to the end of the list but when I try to add a new number to the end it just shows that numbers and not the others. I think I'm just creating a new list and displaying that but I'm not sure. An example of code would be really appreciated.

Normal Add Code:

 public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) {

                head.setBefore(n); 
            }
            head = n;

        }

The add to last code:

public void addLast(int element) {

        Node currentNode = head;


        currentNode.setItem(element);
        currentNode.setBefore(tail); 
        currentNode.setNext(null);


        tail = currentNode;

    }

FULL CODE:

public class DoubleLink {

private Node head; 
private Node tail;

    //Methods
    //Constructors
    public DoubleLink(){
        head = null;
        tail = null;

    }

    public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) {

                head.setBefore(n); 
            head = n;


        }
    public void display(){      //LIST TRAVERSAL!
        // Reference traversal
        //Needed an interatior
        Node currentNode = head;
        while(currentNode != null){
            System.out.print(currentNode.getItem()+ " ");
            currentNode = currentNode.getNext();
        }
        System.out.println();
    }


    public int search(int element){
        int position = 0;
        Node currentNode = head;
        while(currentNode != null){
            if(currentNode.getItem() == element){
                return position;

            }
            position++;
            currentNode = currentNode.getNext();

        }
        return -1;
    }

    public void insert(int element, int position){
        int currentposition = 0;
        Node currentNode = head;

        //Traverse to the right position
        while(currentposition < position-1){

            currentposition++;
        } 
        Node n = new Node();
        n.setItem(element);
        n.setNext(currentNode.getNext());
        currentNode.setNext(n);

        //The previous number connecting to the new number
        currentNode = tail;

    }

    public void remove(int position){
        int currentposition = 0;
        Node currentNode = head;

        //Traverse to the right position
        while(currentposition < position-1){

            currentposition++;
        }

        Node dyingNode = currentNode.getNext();
        currentNode.setNext(dyingNode.getNext());


    }

    public void addLast(int element) {



        Node nodeToInsert = new Node();
        nodeToInsert.setItem(element);
        nodeToInsert.setBefore(tail); 
        nodeToInsert.setNext(null);

        if(tail != null)
          tail.setNext(nodeToInsert); //link the list

        tail = nodeToInsert; //now the tail is the new node i added

        if(head == null)       // if the list has no elements then set the head
             head = nodeToInsert;


    } 

    public static void main(String[] args) {
    DoubleLink l = new DoubleLink();


    l.add(1);
    l.add(2);
    l.add(3);
    l.display();
    l.addLast(99);
    l.display();

    }
}

Node Class:

public class Node {

    //Data

    private int item;
    private Node next;
    private Node before;

    //Methods
    public int getItem(){
        return item;
    }

    public void setItem(int item){
        this.item = item;
    }

    public Node getNext(){
        return next;
    }

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

    public Node getBefore(){
        return before;
    }

    public void setBefore(Node before){
        this.before = before;
    }

}
like image 988
PrimeRC Avatar asked Jun 29 '26 19:06

PrimeRC


1 Answers

You should change your code, creating a new Node() like this.

public void addLast(int element) {

        Node nodeToInsert = new Node();
        nodeToInsert.setItem(element);
        nodeToInsert.setBefore(tail); 
        nodeToInsert.setNext(null);

        if(tail != null)
          tail.setNext(nodeToInsert); //link the list

        tail = nodeToInsert; //now the tail is the new node i added

        if(head == null)       // if the list has no elements then set the head
             head = nodeToInsert;
}

UPDATE

The problem is in your add method , you are not setting never tail

public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) 
                head.setBefore(n); 
            else{
                tail=n; // if head == null then now you have an element so head = tail
            }

            head = n;        
    }
like image 65
nachokk Avatar answered Jul 01 '26 08:07

nachokk