Double Linked List
Double Linked List
In a single linked list, every node has link to its next node in the sequence. So, we can traverse from one node to
other node only in one direction and we cannot traverse back. We can solve this kind of problem by using double
linked list. Double linked list can be defined as follows...
Double linked list is a sequence of elements in which every element has links to its previous element and next
element in the sequence.
In double linked list, every node has link to its previous node and next node. So, we can traverse forward by using
next field and can traverse backward by using previous field. Every node in a double linked list contains three
fields and they are shown in the following figure...
Here, ' previous ' field is used to store the address of the previous node in the sequence, ' next ' field is used to
store the address of the next node in the sequence and 'data' field is used to store the actual value of that node.
Example
NOTE
In double linked list, the first node must be always pointed by head.
Always the previous field of the head node must be NULL.
Always the next field of the last node must be NULL.
Operations
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
Inserting At Beginning of the list
Inserting At End of the list
Inserting At Specific location in the list
1
Inserting at Beginning of the list
Steps to insert a new node at beginning of the double linked list...
1. Step 1: Create a newNode with given value and newNode. previous as NULL.
2. Step 2: Check whether list is Empty (head == NULL)
3. Step 3: If it is Empty then, assign NULL to newNode. next and newNode to head.
4. Step 4: If it is not Empty then, assign head to newNode. next and newNode to head.
Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
Deleting from Beginning of the list
Deleting from End of the list
Deleting a Specific Node
3
Deleting from End of the list
4
3. Step 3: If it is not empty, then define a Node pointer 'currentNode' and initialize with head.
4. Step 4: Keep moving the currentNode until it reaches to the exact node to be deleted or to the last node.
5. Step 5: If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and
terminate the function.
6. Step 6: If it is reached to the exact node which we want to delete, then check whether list is having only one node or
not
7. Step 7: If list has only one node and that is the node which is to be deleted then set head to NULL and delete
currentNode.
8. Step 8: If list contains multiple nodes, then check whether currentNode is the head node in the list (currentNode ==
head).
9. Step 9: If currentNode is the head node, then move the head to the next node (head = head. next), set head of previous
to NULL (head. previous = NULL) and delete currentNode.
10. Step 10: If currentNode is not the head node, then check whether it is the last node in the list (currentNode. next ==
NULL).
11. Step 11: If currentNode is the last node then set currentNode of previous of next to NULL (currentNode . previous .
next = NULL) and delete currentNode.
12. Step 12: If currentNode is not the head node and not the last node, then set currentNode of previous of next to
currentNode of next (currentNode.previous.next = currentNode . next), currentNode of next of previous to
currentNode of previous (currentNode.next . previous = currentNode . previous) and delete currentNode .
5
import java.io.*;
import java.util.*;
class chainNodeD{
public Object data = null;
public chainNodeD prev = null;
public chainNodeD next = null;
class LinkedListD
{
public LinkedListD()
{
head= null;
size=0;
}
//end basic
// -------------------------------------------------------------
public void insertFirst(Object elt) // insert at front of list
{
chainNodeD newNode = new chainNodeD(elt); // make new chainNodeD
6
if( isEmpty() ) // if empty list,
tail = newNode; // newchainNodeD <-- tail
else
head.prev = newNode; // newchainNodeD <-- old head
newNode.next = head; // newchainNodeD --> old head
head = newNode; // head --> newchainNodeD
size++;
} // end insertFirst
// -------------------------------------------------------------
public void insertLast(Object elt) // insert at end of list
{
chainNodeD newNode = new chainNodeD(elt); // make new chainNodeD
if( isEmpty() ) // if empty list,
head = newNode; // head --> newchainNodeD
else
{
tail.next = newNode; // old tail --> newchainNodeD
newNode.prev = tail; // old tail <-- newchainNodeD
}
tail = newNode; // newchainNodeD <-- tail
size++;
}
// -------------------------------------------------------------
public Object deleteFirst() // delete head chainNodeD
{ // (assumes non-empty list)
Object temp = null;
if( isEmpty() )
System.out.println("The list is empty");
else
{ temp=head.data;
if(head.next == null) // if only one item
tail = null; // null <-- tail
else
{
temp=head.data;
head.next.prev = null; // null <-- old next
head = head.next; // head --> old next
}
size--;
}
return temp;
}
// -------------------------------------------------------------
public Object deleteLast() // delete last chainNodeD
{
Object temp = null;
if( isEmpty() )
System.out.println("The list is empty");
else
{ temp=tail.data;
if(head.next == null) // if only one item
7
head = null; // head --> null
else
{
tail.prev.next = null; // old prev --> null
tail = tail.prev; // old prev <-- tail
}
size--;
}
return temp;
}// end deleteLast
// -------------------------------------------------------------
public void displayForward()
{
chainNodeD tmp=head;
System.out.println("List data are:");
while(tmp!=null)
{
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
System.out.println();
}// end displayForward
// -------------------------------------------------------------
public void displayBackword()
{
chainNodeD tmp=tail;
System.out.println("List data are in reverse:");
while(tmp!=null)
{
System.out.print(tmp.data+" ");
tmp=tmp.prev;
}
System.out.println();
}// end displayBackword
// -------------------------------------------------------------
if(tmp==null)
System.out.println("\nElement "+ after+ " is not in the list. "+ newElt +" was not created.");
else
8
{
chainNodeD newNode = new chainNodeD(newElt,tmp,tmp.next);
tmp.next.prev=newNode;
tmp.next=newNode;
size++;
success =true;
System.out.println("\nElement "+after+" was found. "+newElt+" was created.");
}
}
return success;
} // end insertAfter
// -------------------------------------------------------------
if(tmp==null)
System.out.println("\nElement "+ before+ " is not in the list. "+ newElt +" was not created.");
else
{
chainNodeD newNode = new chainNodeD(newElt,tmp.prev,tmp);
tmp.prev.next=newNode;
tmp.prev=newNode;
size++;
success =true;
System.out.println("\nElement "+before+" was found. "+newElt+" was created.");
}
}
return success;
} // end insertBefore
// -------------------------------------------------------------
if(tmp==null)
System.out.println("\nElement "+ key+ " is not in the list. "+ key +" was not deleted.");
else
{
rmData=tmp.data;
tmp.prev.next=tmp.next;
tmp.next.prev=tmp.prev;
size--;
System.out.println("\nElement "+key+" was found and deleted.");
}
}
return rmData;
} // end delete
// -------------------------------------------------------------
if(p==null)
return -1;
else
return index;
}// end search
// -------------------------------------------------------------
public Object remove(int i)
{// removes (i)th element of the list
Object retElt=null;
if (i >=0 && i <= size)
{
if(i==0)
{
retElt = head.data;
tail.next=head.next;
head.next.prev=tail;
}
else
{
chainNodeD p=head;
for(int j=0; j<i-1;j++)
p=p.next;
retElt=p.next.data;
p.next.next.prev=p;
p.next=p.next.next;
}
size--;
11
}
return retElt;
}// end remove
// -------------------------------------------------------------
public Object getObject(int i)
{
Object retElt=null;
if (i >=0 && i < size)
{
if(i==0)
retElt=head.data;
else
{ chainNodeD p=head;
for(int j=0; j<i-1;j++)
p=p.next;
retElt=p.next.data;
}
}
else
System.out.println(i+"th index is not found");
return retElt;
}// end getObject
// -------------------------------------------------------------
d.insertFirst(64);
d.insertLast(47);
d.displayForward();
d.insertFirst(95);
d.insertLast(38);
d.displayForward();
d.deleteFirst();
d.displayForward();
d.deleteLast();
d.displayForward();
12
d.insertAfter(64,43);
d.displayForward();
d.displayBackword();
d.insertBefore(43,85);
d.displayForward();
d.displayBackword();
d.delete(78);
d.displayForward();
d.displayBackword();
d.delete(43);
d.displayForward();
d.displayBackword();
}
}//end chainDApp2019
13