Key operations on a Doubly Linked List include:
This is General Representation Of Doubly Linked List. Each node has two links: This allows for more flexibility in list manipulation but requires more memory per node.
The first node's previous pointer and the last node's next pointer both point to null.
xxxxxxxxxx
Define Node Structure:
Node:
- data: Integer
- next: Node
- prev: Node
First we write the Pseudocode for Doubly Linked List Operations
Node Structure
Define Node Structure:
Node:
- data: Integer
- next: Node
- prev: Node
Initialize Head and Tail Pointers
Initialize:
- head = null
- tail = null
xxxxxxxxxx
Function addNodeAtLast(data: Integer):
Create newNode with given data
If head is null:
head = newNode
tail = newNode
Return
Else:
Set tail.next to newNode
Set newNode.prev to tail
Set tail to newNode
xxxxxxxxxx
Function addNodeAtFirst(data: Integer):
Create newNode with given data
If head is null:
head = newNode
tail = newNode
Return
Else:
Set newNode.next to head
Set head.prev to newNode
Set head to newNode
xxxxxxxxxx
Function insertAt(index: Integer, data: Integer):
If index < 0:
Print "Invalid index"
Return
Create newNode with given data
Set current to head
Initialize counter j to 0
While j < index - 2:
If current is null:
Print "Invalid index"
Return
Set current to current.next
Increment j
If current.next is not null:
Set newNode.next to current.next
Set current.next.prev to newNode
Set newNode.prev to current
Set current.next to newNode
xxxxxxxxxx
Function printDLLForward(head: Node):
Set current to head
While current is not null:
Print current.data followed by "->"
Set current to current.next
Print "null"
xxxxxxxxxx
Function printDLLReverse(tail: Node):
Set current to tail
While current is not null:
Print current.data followed by "->"
Set current to current.prev
Print "null"
Code for Insertions In Doubly Linked List
package ds.linked_list.doubly_linked_list;
public class OperationsOnDLL
{
private static DLLNode dllNodeHead = null;
private static DLLNode dllNodeTail = null;
public static class DLLNode {
int data;
DLLNode next;
DLLNode prev;
DLLNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
public static void addNodeAtLast(int data)
{
DLLNode newNode = new DLLNode(data);
if(dllNodeHead == null)
{
dllNodeHead = newNode;
dllNodeTail = newNode;
return;
}
dllNodeTail.next = newNode;
newNode.prev = dllNodeTail;
dllNodeTail = newNode;
}
public static void addNodeAtFirst(int data)
{
DLLNode newNode = new DLLNode(data);
if(dllNodeHead == null)
{
dllNodeHead = newNode;
dllNodeTail = newNode;
return;
}
newNode.next = dllNodeHead;
dllNodeHead.prev = newNode;
dllNodeHead = newNode;
}
public static void insertAt(int index, int data)
{
if(index < 0)
{
System.out.println("Invalid index");
return;
}
DLLNode newNode = new DLLNode(data);
DLLNode curr = dllNodeHead;
int j = 0;
while( j < (index-2) )
{
if(curr == null) {
System.out.println("Invalid index");
return;
}
curr = curr.next;
j++;
}
if(curr.next != null) {
newNode.next = curr.next;
curr.next.prev = newNode;
}
newNode.prev = curr;
curr.next = newNode;
}
public static void printDLLForward(DLLNode dllNodeHead)
{
DLLNode current = dllNodeHead;
while(current != null)
{
System.out.print(current.data + "->");
current = current.next;
}
System.out.print("null");
}
public static void printDLLReverse(DLLNode tail)
{
DLLNode prev = tail;
while(prev != null)
{
System.out.print(prev.data + "->");
prev = prev.prev;
}
System.out.print("null");
}
public static void main(String[] args)
{
OperationsOnDLL.addNodeAtLast(10);
OperationsOnDLL.addNodeAtLast(20);
OperationsOnDLL.addNodeAtFirst(4);
OperationsOnDLL.addNodeAtFirst(12);
OperationsOnDLL.addNodeAtFirst(5);
OperationsOnDLL.addNodeAtFirst(14);
OperationsOnDLL.insertAt(4, 200);
System.out.println("Forward Direction");
OperationsOnDLL.printDLLForward(dllNodeHead);
System.out.println();
System.out.println("Reverse Direction");
OperationsOnDLL.printDLLReverse(dllNodeTail);
}
}
Forward Direction
14->5->12->200->4->10->20->null
Reverse Direction
20->10->4->200->12->5->14->null