# Doubly Linked List Data Structure

## What is Doubly Linked List?

Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, forward as well as backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.

As per the above illustration, following are the important points to be considered.

• Doubly Linked List contains a link element called first and last.

• Each link carries a data field(s) and a link field called next.

• The last link carries a link as null to mark the end of the list.

## Basic Operations in Doubly Linked List

Following are the basic operations supported by a list.

• Insertion − Adds an element at the beginning of the list.

• Insert Last − Adds an element at the end of the list.

• Insert After − Adds an element after an item of the list.

• Deletion − Deletes an element at the beginning of the list.

• Delete Last − Deletes an element from the end of the list.

• Delete − Deletes an element from the list using the key.

• Display forward − Displays the complete list in a forward manner.

• Display backward − Displays the complete list in a backward manner.

## Doubly Linked List - Insertion at the Beginning

In this operation, we create a new node with three compartments, one containing the data, the others containing the address of its previous and next nodes in the list. This new node is inserted at the beginning of the list.

### Algorithm

1. START
2. Create a new node with three variables: prev, data, next.
3. Store the new data in the data variable
4. If the list is empty, make the new node as head.
5. Otherwise, link the address of the existing first node to the
next variable of the new node, and assign null to the prev variable.
6. Point the head to the new node.
7. END

### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
}

### Output

Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
return 0;
}

### Output

Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
//Java code for doubly linked list
import java.util.*;
class Node {
public int data;
public int key;
public Node next;
public Node prev;
public Node(int data, int key) {
this.data = data;
this.key = key;
this.next = null;
this.prev = null;
}
}
public class Main {
static Node last = null;
static Node current = null;
// is list empty
public static boolean is_empty() {
}
public static void print_list() {
while (ptr != null) {
System.out.println("(" + ptr.key + "," + ptr.data + ")");
ptr = ptr.next;
}
}
//insert link at the first location
public static void insert_first(int key, int data) {
Node link = new Node(data, key);
if (is_empty()) {
} else {
}
//point it to old first link
//point first to new first link
}
public static void main(String[] args) {
insert_first(1, 10);
insert_first(2, 20);
insert_first(3, 30);
insert_first(4, 1);
insert_first(5, 40);
insert_first(6, 56);
print_list();
}
}

### Output

#Python code for doubly linked list
class Node:
def __init__(self, data=None, key=None):
self.data = data
self.key = key
self.next = None
self.prev = None
last = None
current = None
#is list empty
def is_empty():
def print_list():
while ptr != None:
print(f"({ptr.key},{ptr.data})")
ptr = ptr.next
#insert link at the first location
def insert_first(key, data):
if is_empty():
else:
#point it to old first link
#point first to new first link
insert_first(1,10)
insert_first(2,20)
insert_first(3,30)
insert_first(4,1)
insert_first(5,40)
insert_first(6,56)
print_list()

### Output

(6,56) (5,40) (4,1) (3,30) (2,20) (1,10)

## Doubly Linked List - Insertion at the End

In this insertion operation, the new input node is added at the end of the doubly linked list; if the list is not empty. The head will be pointed to the new node, if the list is empty.

### Algorithm

1. START
2. If the list is empty, add the node to the list and point
3. If the list is not empty, find the last node of the list.
4. Create a link between the last node in the list and the
new node.
5. The new node will point to NULL as it is the new last node.
6. END

### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//insert link at the last location
void insertLast(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

//mark old last node as prev of new link
}

//point last to new last node
}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertLast(5,40);
insertLast(6,56);
printList();
}

### Output

Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//insert link at the last location
void insertLast(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

//mark old last node as prev of new link
}

//point last to new last node
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertLast(5,40);
insertLast(6,56);
printList();
return 0;
}

### Output

Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)
import java.util.*;
class Node {
public int data;
public int key;
public Node next;
public Node prev;
public Node(int data, int key) {
this.data = data;
this.key = key;
this.next = null;
this.prev = null;
}
}
public class Main {
static Node last = null;
static Node current = null;
public static boolean isEmpty() {
}
public static void printList() {
while (ptr != null) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
}
public static void insertFirst(int key, int data) {
Node link = new Node(data, key);
if (isEmpty()) {
} else {
}
}
public static void insertLast(int key, int data) {
Node link = new Node(data, key);
if (isEmpty()) {
} else {
}
}

public static void main(String[] args) {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertLast(5,40);
insertLast(6,56);
printList();
}
}

### Output

Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)
class Node:
def __init__(self, data=None, key=None):
self.data = data
self.key = key
self.next = None
self.prev = None
last = None
current = None
def isEmpty():
def printList():
while ptr != None:
print(f"({ptr.key},{ptr.data})", end=" ")
ptr = ptr.next
def insertFirst(key, data):
if isEmpty():
else:
def insertLast(key, data):
if isEmpty():
else:
insertFirst(1,10)
insertFirst(2,20)
insertFirst(3,30)
insertFirst(4,1)
insertLast(5,40)
insertLast(6,56)
printList()

### Output

Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)

## Doubly Linked List - Deletion at the Beginning

This deletion operation deletes the existing first nodes in the doubly linked list. The head is shifted to the next node and the link is removed.

### Algorithm

1. START
2. Check the status of the doubly linked list
3. If the list is empty, deletion is not possible
4. If the list is not empty, the head pointer is
shifted to the next node.
5. END

### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//delete first item
struct node* deleteFirst(){

last = NULL;
} else {
}

}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
printf("\nList after deleting first record: \n");
deleteFirst();
printList();
}

### Output

Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
List after deleting first record: (5,40) (4,1) (3,30) (2,20) (1,10)
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//delete first item
struct node* deleteFirst(){

last = NULL;
} else {
}

}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
printf("\nList after deleting first record: \n");
deleteFirst();
printList();
return 0;
}

### Output

(6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
List after deleting first record:
(5,40) (4,1) (3,30) (2,20) (1,10)
//Java code for doubly linked list
import java.util.*;
class Node {
public int data;
public int key;
public Node next;
public Node prev;
public Node(int data, int key) {
this.data = data;
this.key = key;
this.next = null;
this.prev = null;
}
}
public class Main {
public static Node head = null;
public static Node last = null;
public static Node current = null;
//is list empty
public static boolean isEmpty() {
}
public static void printList() {
while (ptr != null) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
}
//insert link at the first location
public static void insertFirst(int key, int data) {
Node link = new Node(data, key);
if (isEmpty()) {
} else {
}
//point it to old first link
}
//delete the first item
public static Node deleteFirst() {
last = null;
} else {
}
}
public static void main(String[] args) {
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
printList();
System.out.print("\nList after deleting first record: \n");
deleteFirst();
printList();
}
}

### Output

(6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
List after deleting first record:
(5,40) (4,1) (3,30) (2,20) (1,10)
#Python code for doubly linked list
class Node:
def __init__(self, data=None, key=None):
self.data = data
self.key = key
self.next = None
self.prev = None
last = None
current = None
#is list empty
def isEmpty():
def printList():
while ptr != None:
print(f"({ptr.key},{ptr.data}) ", end="")
ptr = ptr.next
#insert link at the first location
def insertFirst(key, data):
if isEmpty():
else:
#point it to old first link
#delete first item
def deleteFirst():
last = None
else:
insertFirst(1,10)
insertFirst(2,20)
insertFirst(3,30)
insertFirst(4,1)
insertFirst(5,40)
insertFirst(6,56)
printList()
print("\nList after deleting first record:")
deleteFirst()
printList()

### Output

(6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
List after deleting first record:
(5,40) (4,1) (3,30) (2,20) (1,10)

## Doubly Linked List - Complete Implementation

Following are the complete implementations of Doubly Linked List in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}

//display the list in from first to last
void displayForward(){

//start from the beginning

//navigate till the end of the list
printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}

//display the list from last to first
void displayBackward(){

//start from the last
struct node *ptr = last;

//navigate till the start of the list
printf("\n[ ");
while(ptr != NULL) {

//print data
printf("(%d,%d) ",ptr->key,ptr->data);

//move to next item
ptr = ptr ->prev;
printf(" ");
}
printf(" ]");
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//insert link at the last location
void insertLast(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

//mark old last node as prev of new link
}

//point last to new last node
}

//delete first item
struct node* deleteFirst(){

last = NULL;
} else {
}

}

//delete link at the last location
struct node* deleteLast(){

} else {
last->prev->next = NULL;
}
last = last->prev;

}

//delete a link with given key
struct node* delete(int key){

struct node* previous = NULL;

//if list is empty
return NULL;
}

//navigate through list
while(current->key != key) {

//if it is last node
if(current->next == NULL) {
return NULL;
} else {

previous = current;

current = current->next;
}
}

//found a match, update the link

//change first to point to next link
} else {

current->prev->next = current->next;
}
if(current == last) {

//change last to point to prev link
last = current->prev;
} else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data){

//if list is empty
return false;
}

//navigate through list
while(current->key != key) {

//if it is last node
if(current->next == NULL) {
return false;
} else {

current = current->next;
}
}

struct node *newLink = (struct node*) malloc(sizeof(struct node));
if(current == last) {
} else {
}
return true;
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
delete(4);
displayForward();
}

### Output

List (First to Last):
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10)  ]

List (Last to first):
[ (1,10)  (2,20)  (3,30)  (4,1)  (5,40)  (6,56)   ]
List , after deleting first record:
[ (5,40) (4,1) (3,30) (2,20) (1,10)  ]
List , after deleting last record:
[ (5,40) (4,1) (3,30) (2,20)  ]
List , insert after key(4) :
[ (5,40) (4,1) (4,13) (3,30) (2,20)  ]
List , after delete key(4) :
[ (5,40) (4,13) (3,30) (2,20)  ]
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
using namespace std;
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};

struct node *last = NULL;
struct node *current = NULL;

//is list empty
bool isEmpty(){
}
//display the list in from first to last
void displayForward(){

//start from the beginning

//navigate till the end of the list
cout << "\n[ ";
while(ptr != NULL) {
cout << "(" << ptr->key << "," << ptr->data << ")";
ptr = ptr->next;
}
cout << " ]" << endl;
}

//display the list from last to first
void displayBackward(){

//start from the last
struct node *ptr = last;

//navigate till the start of the list
cout << "\n[ ";
while(ptr != NULL) {

//print data
cout << "(" << ptr->key << "," << ptr->data << ")";

//move to next item
ptr = ptr ->prev;
cout << " ";
}
cout << " ]" << endl;
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

}

//point it to old first link

//point first to new first link
}

//insert link at the last location
void insertLast(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if(isEmpty()) {

} else {

//mark old last node as prev of new link
}

//point last to new last node
}

//delete first item
struct node* deleteFirst(){

last = NULL;
} else {
}

}

//delete link at the last location
struct node* deleteLast(){

} else {
last->prev->next = NULL;
}
last = last->prev;

}

//delete a link with given key
struct node* deletenode(int key){

struct node* previous = NULL;

//if list is empty
return NULL;
}

//navigate through list
while(current->key != key) {

//if it is last node
if(current->next == NULL) {
return NULL;
} else {

previous = current;

current = current->next;
}
}

//found a match, update the link

//change first to point to next link
} else {

current->prev->next = current->next;
}
if(current == last) {

//change last to point to prev link
last = current->prev;
} else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data){

//if list is empty
return false;
}

//navigate through list
while(current->key != key) {

//if it is last node
if(current->next == NULL) {
return false;
} else {

current = current->next;
}
}

struct node *newLink = (struct node*) malloc(sizeof(struct node));
if(current == last) {
} else {
}
return true;
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4, 7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
deletenode(4);
displayForward();
return 0;
}

### Output

List (First to Last):
[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]

List (Last to First):
[ (1, 10) (2, 20) (3, 30) (4, 1) (5, 40) (6, 56) ]
List, after deleting first record:
[ (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]
List, after deleting last record:
[ (5, 40) (4, 1) (3, 30) (2, 20) ]
List, insert after key(4):
[ (5, 40) (4, 1) (7, 13) (3, 30) (2, 20) ]
List, after delete key(4):
[ (5, 40) (7, 13) (3, 30) (2, 20) ]
class Node {
int data;
int key;
Node next;
Node prev;

public Node(int key, int data) {
this.key = key;
this.data = data;
this.next = null;
this.prev = null;
}
}

Node last;

boolean isEmpty() {
}

void displayForward() {
System.out.print("[ ");
while (ptr != null) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
System.out.println("]");
}

void displayBackward() {
Node ptr = last;
System.out.print("[ ");
while (ptr != null) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.prev;
}
System.out.println("]");
}

void insertFirst(int key, int data) {
Node link = new Node(key, data);
if (isEmpty()) {
} else {
}
}

void insertLast(int key, int data) {
Node link = new Node(key, data);
if (isEmpty()) {
} else {
}
}

Node deleteFirst() {
if (isEmpty()) {
return null;
}
last = null;
} else {
}
}

Node deleteLast() {
if (isEmpty()) {
return null;
}
} else {
last.prev.next = null;
}
last = last.prev;
}

Node delete(int key) {
Node previous = null;
return null;
}
while (current.key != key) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
} else {
current.prev.next = current.next;
}
if (current == last) {
last = current.prev;
} else {
current.next.prev = current.prev;
}
return current;
}

boolean insertAfter(int key, int newKey, int data) {
return false;
}
while (current.key != key) {
if (current.next == null) {
return false;
} else {
current = current.next;
}
}
Node newLink = new Node(newKey, data);
if (current == last) {
} else {
}
return true;
}
}

public class Main {
public static void main(String[] args) {
dll.insertFirst(1, 10);
dll.insertFirst(2, 20);
dll.insertFirst(3, 30);
dll.insertFirst(4, 1);
dll.insertFirst(5, 40);
dll.insertFirst(6, 56);
System.out.println("List (First to Last):");
dll.displayForward();
System.out.println();
System.out.println("List (Last to First):");
dll.displayBackward();
System.out.println("List, after deleting first record:");
dll.deleteFirst();
dll.displayForward();
System.out.println("List, after deleting last record:");
dll.deleteLast();
dll.displayForward();
System.out.println("List, insert after key(4):");
dll.insertAfter(4, 7, 13);
dll.displayForward();
System.out.println("List, after delete key(4):");
dll.delete(4);
dll.displayForward();
}
}

### Output

List (First to Last):
[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]

List (Last to First):
[ (1, 10) (2, 20) (3, 30) (4, 1) (5, 40) (6, 56) ]
List, after deleting first record:
[ (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]
List, after deleting last record:
[ (5, 40) (4, 1) (3, 30) (2, 20) ]
List, insert after key(4):
[ (5, 40) (4, 1) (7, 13) (3, 30) (2, 20) ]
List, after delete key(4):
[ (5, 40) (7, 13) (3, 30) (2, 20) ]
class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
self.prev = None

def __init__(self):
self.last = None

def is_empty(self):

def display_forward(self):
print("[", end=" ")
while ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.next
print("]")

def display_backward(self):
ptr = self.last
print("[", end=" ")
while ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.prev
print("]")

def insert_first(self, key, data):
if self.is_empty():
else:

def insert_last(self, key, data):
if self.is_empty():
else:

def delete_first(self):
if self.is_empty():
return None
self.last = None
else:

def delete_last(self):
if self.is_empty():
return None
else:
self.last.prev.next = None
self.last = self.last.prev

def delete(self, key):
while current and current.key != key:
current = current.next
if current is None:
return None
else:
current.prev.next = current.next
if current == self.last:
self.last = current.prev
else:
current.next.prev = current.prev
return current

def insert_after(self, key, new_key, data):
while current and current.key != key:
current = current.next
if current is None:
return False
if current == self.last:
else:
return True

# Example usage
dll.insert_first(1, 10)
dll.insert_first(2, 20)
dll.insert_first(3, 30)
dll.insert_first(4, 1)
dll.insert_first(5, 40)
dll.insert_first(6, 56)
print("List (First to Last):")
dll.display_forward()
print()
print("List (Last to First):")
dll.display_backward()
print("List, after deleting first record:")
dll.delete_first()
dll.display_forward()
print("List, after deleting last record:")
dll.delete_last()
dll.display_forward()
print("List, insert after key(4):")
dll.insert_after(4, 7, 13)
dll.display_forward()
print("List, after delete key(4):")
dll.delete(4)
dll.display_forward()

### Output

List (First to Last):
[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]

List (Last to First):
[ (1, 10) (2, 20) (3, 30) (4, 1) (5, 40) (6, 56) ]
List, after deleting first record:
[ (5, 40) (4, 1) (3, 30) (2, 20) (1, 10) ]
List, after deleting last record:
[ (5, 40) (4, 1) (3, 30) (2, 20) ]
List, insert after key(4):
[ (5, 40) (4, 1) (7, 13) (3, 30) (2, 20) ]
List, after delete key(4):
[ (5, 40) (7, 13) (3, 30) (2, 20) ]