Overview
- Doubly linked list
- Node
- Insertion
- Deletion
Doubly Linked list
- A doubly linked list is a type of linked list where each node maintains a reference to both the next node and the previous node in the sequence. This feature allows for both forward and backward traversal of the list. The first node’s previous reference and the last node’s next reference point to null
- Below is an example implementation of a doubly linked list in Java:
Representation Of The Node In A Doubly Linked List
struct node
{
int data;
struct node *next;
struct node *prev;
}
In the above representation, we have defined a user-defined structure named a node with three members, one is data of integer type, and the other two are the pointers, i.e., next and prev of the node type. The next pointer variable holds the address of the next node, and the prev pointer holds the address of the previous node. The type of both the pointers, i.e., next and prev is struct node as both the pointers are storing the address of the node of the struct node type.
Node
class node
{
node prev;
int item;
node next ;
}
Insertion
- At first
- At Last
- After a node
Inset As A First Node
node n = new node() ;
n .item = data;
n.prev = null;
n .next = start;
if ( start != null)
start.prev= n ;
start = n
Insert At Last
node n = new node() ;
n. item = data;
n .next = null;
if( start == null)
{
n.prev=null
start=n;
}
else
{
temp=start;
while(temp.next!=null)
temp=temp.next;
temp.next=n;
}
Insert After A Node
Deletion
- Delete First node
- Delete Last node
- Delete Specific node