In doubly linked list we have two portion reserved for addresses one for the next address and other for the previous address.
Doubly linked list hold the reference for both the list either previous to it or next to it and the middle portion contains the data for the node.
Basic Operations
Insertion − add element at beginning of the list.
Deletion − delete an element from the beginning of the list.
Insert Last − add an element in the end of the list.
Delete Last − delete an element from the end of the list.
Insert After − add an element after an item of the list.
Delete − delete an element from the list .
Display forward − displaying complete list in going forward.
Display backward − displaying complete list in going backward.
Insertion in Doubly Linked List
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
}else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
//point first to new first link
head = link;
}
Deletion in Doubly Linked List
While deletion just free the previous position by giving its position to the previous node and the next position to another if it exists otherwise set NULL for both
//delete first item
struct node* deleteFirst() {
//save reference to first link
struct node *tempLink = head;
//if only one link
if(head->next == NULL) {
last = NULL;
}else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
0 Comment(s)