doubly node linked list code example

Example 1: double linked list java

class doublelinkedlist{  
    Node head;
    Node tail;

    static class Node{
        int data;
        Node previous;
        Node next;

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

    public void addLink(int data) {
        Node node = new Node(data);

        if(head == null) {
            head = tail = node;
            head.previous = null;
        } else {
            tail.next = node;
            node.previous = tail;
            tail = node;
        }
        tail.next = null;
    }
}

Example 2: doubly linked list example

//insert link at the first location
void insertFirst(int key, int data) {

   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
	
   if(isEmpty()) {
      //make it the last link
      last = link;
   } else {
      //update first prev link
      head->prev = link;
   }

   //point it to old first link
   link->next = head;
	
   //point first to new first link
   head = link;
}

Tags:

Misc Example