0% found this document useful (0 votes)
11 views13 pages

Double Linked List

Uploaded by

Sinthu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Double Linked List

Uploaded by

Sinthu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Double Linked List

What is 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...

previous data next

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

In a double linked list, we perform the following operations...


 Insertion
 Deletion
 Display

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.

Inserting at End of the list


Steps to insert a new node at end of the double linked list...
1. Step 1: Create a newNode with given value and newNode. next as NULL.
2. Step 2: Check whether list is Empty (head == NULL)
3. Step 3: If it is Empty, then assign NULL to newNode. previous and newNode to head.
4. Step 4: If it is not empty, then, define a node pointer currentNode and initialize with head.
5. Step 5: Keep moving the currentNode to its next node until it reaches to the last node in the list (until currentNode.
next is equal to NULL).
6. Step 6: Assign newNode to currentNode. next and currentNode to newNode. previous.

Inserting at Specific location in the list (After a Node)


Steps to insert a new node after a node in the double linked list...
1. Step 1: Create a newNode with given value.
2. Step 2: Check whether list is Empty (head == NULL)
3. Step 3: If it is Empty then, assign NULL to newNode. previous & newNode.next and newNode to head.
2
4. Step 4: If it is not empty then, define two node pointers currentNode1 & currentNode2 and initialize currentNode1
with head.
5. Step 5: Keep moving the currentNode1 to its next node until it reaches to the node after which we want to insert the
newNode (until currentNode1. data is equal to location, here location is the node value after which we want to insert
the newNode).
6. Step 6: Every time check whether currentNode1 is reached to the last node. If it is reached to the last node then
display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move
the currentNode1 to next node.
7. Step 7: Assign currentNode1.next to currentNode2, newNode to currentNode1. next, currentNode1 to
newNode.previous, currentNode2 to newNode.next and newNode to currentNode2.previous.

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

Deleting from Beginning of the list

Steps to delete a node from beginning of the double linked list...


1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is not empty then, define a Node pointer 'currentNode' and initialize with head.
4. Step 4: Check whether list is having only one node (currentNode . previous is equal to currentNode . next)
5. Step 5: If it is TRUE, then set head to NULL and delete currentNode (Setting Empty list conditions)
6. Step 6: If it is FALSE, then assign currentNode. next to head, NULL to head . previous and delete currentNode.

3
Deleting from End of the list

Steps to delete a node from end of the double linked list...


1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is not empty then, define a Node pointer 'currentNode' and initialize with head.
4. Step 4: Check whether list has only one Node (currentNode . previous and currentNode . next both are NULL)
5. Step 5: If it is TRUE, then assign NULL to head and delete currentNode. And terminate from the function. (Setting
Empty list condition)
6. Step 6: If it is FALSE, then keep moving currentNode until it reaches to the last node in the list. (until currentNode .
next is equal to NULL)
7. Step 7: Assign NULL to currentNode. previous . next and delete currentNode.

Deleting a Specific Node from the list

Steps to delete a specific node from the double linked list...


1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.

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 .

Displaying a Double Linked List

Steps to display the elements of a double linked list...


1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
3. Step 3: If it is not Empty, then define a Node pointer 'currentNode' and initialize with head.
4. Step 4: Display 'NULL ←'.
5. Step 5: Keep displaying currentNode. data with an arrow (←→) until currentNode reaches to the last node
6. Step 6: Finally, display currentNode. data with arrow pointing to NULL (currentNode. data → NULL).

5
import java.io.*;
import java.util.*;

class chainNodeD{
public Object data = null;
public chainNodeD prev = null;
public chainNodeD next = null;

public chainNodeD(Object o){


this.data =o;
this.prev=null;
this.next = null;
}

public chainNodeD(Object o, chainNodeD p, chainNodeD n){


this.data =o;
this.prev=p;
this.next = n;
}
}// end chainNodeD

class LinkedListD
{

protected chainNodeD head;


protected chainNodeD tail;
protected int size;

public LinkedListD()
{
head= null;
size=0;
}

public boolean isEmpty()


{
return head==null;
}

public int size()


{
return size;
}

//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
// -------------------------------------------------------------

public boolean insertAfter(Object after, Object newElt)


{ boolean success=false;
if(isEmpty())
System.out.println("\nLinked List is empty. "+newElt+" was not created.");
else
{
chainNodeD tmp=head;
while(tmp!=null && tmp.data!=after)
tmp=tmp.next;

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
// -------------------------------------------------------------

public boolean insertBefore(Object before, Object newElt)


{ boolean success=false;
if(isEmpty())
System.out.println("\nLinked List is empty. "+newElt+" was not created.");
else
{
chainNodeD tmp=tail;
while(tmp!=null && tmp.data!=before)
tmp=tmp.prev;

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
// -------------------------------------------------------------

public Object delete(Object key)


{
Object rmData=null;
if(isEmpty())
System.out.println("\nLinked List is empty. "+key+" was not deleted.");
else if (head.data==key)
{
rmData=head.data;
head.next.prev=null;
head=head.next;
size--;
9
System.out.println("\nElement "+key+" was found and deleted.");
}
else if (tail.data==key)
{
rmData=tail.data;
tail.prev.next=null;
tail=tail.prev;
size--;
System.out.println("\nElement "+key+" was found and deleted.");
}
else
{
chainNodeD tmp=head;
while(tmp!=null && tmp.data!=key)
tmp=tmp.next;

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
// -------------------------------------------------------------

public void insert( Object o, int i)


{// inserts new element O at (i)th position of the list
if(size==0)
{
head = new chainNodeD(o,null,null);
tail=head;
}
else
{
if (i >=0 && i <= size)
{
if(i==0)
{
chainNodeD newNode = new chainNodeD(o,tail,head);
head.prev=newNode;
tail.next=newNode;
head=newNode;
}
else
10
{
chainNodeD p=head;
for(int j=0; j<i-1;j++)
p=p.next;
chainNodeD newNode=new chainNodeD(o,p,p.next);
p.next.prev=newNode;
p.next=newNode;
}
}
}
size++;
}// end insert
// -------------------------------------------------------------
public int search(Object e)
{
int index =0;
chainNodeD p = head;
while(index<size && !p.data.equals(e))
{
p=p.next;
index++;
}

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
// -------------------------------------------------------------

}// end LinkedListD

public class chainDApp2019 {

public static void main(String args[])


{
LinkedListD d=new LinkedListD();

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy