ray
3
Q:

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;
    }
}
1
/* Node of a doubly linked list */
struct Node { 
    int data; 
    struct Node* next; // Pointer to next node in DLL 
    struct Node* prev; // Pointer to previous node in DLL 
};
1
//delete first item
struct node* deleteFirst() {

   //save reference to first link
   struct node *tempLink = head;
	
   //if only one link
   if(head->next == NULL) {
      last = NULL;
   } else {
      head->next->prev = NULL;
   }
	
   head = head->next;
	
   //return the deleted link
   return tempLink;
}
0

New to Communities?

Join the community