Skip to content

Instantly share code, notes, and snippets.

@Kielan
Last active December 12, 2022 23:44
Show Gist options
  • Save Kielan/0e7a7ed5df493cd07d08884aa2227d57 to your computer and use it in GitHub Desktop.
Save Kielan/0e7a7ed5df493cd07d08884aa2227d57 to your computer and use it in GitHub Desktop.
/*
Doubly linked list
Navigation possible in both directions
*/
#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 *first = NULL;
struct node *last = NULL;
struct node *current = NULL;
book isEmpty() {
return head == NULL;
}
int length() {
int length = 0;
struct node *current;
for(current = first; current != NULL; current = current->next){
length++;
}
return length;
}
struct node* returnListDisplayForward() {
struct node* node_current = first;
struct node* doubly_linked_list_items = (struct node*) malloc(sizeof(struct node));
int n = 0;
while(node_current != NULL) {
doubly_linked_list_items[n].data = node_current->data;
doubly_linked_list_items[n].key = node_current->key;
node_current->next;
n=n+1;
}
return doubly_linked_list_items;
}
struct node* returnListDisplayBackward() {
struct node* node_current = last;
struct node* doubly_linked_list_items = (node*)calloc(length(), sizeof(struct node));
int n = length()-1;
while(node_current != NULL) {
doubly_linked_list_items[n].data = node_current->data;
doubly_linked_list_items[n].key = node_current->key;
node_current->prev;
n=n+1;
}
return doubly_linked_list_items;
}
//insert node at the first index
void insertFirst(int key, int data) {
//create a node
struct node* node_item = (struct node*) malloc(sizeof(struct node));
node_item->key = key;
node_item->data = data;
if(isEmpty()) {
//make it the last node
last = node_item;
} else {
//update first prev node
first->prev = node_item;
}
//point next to first
node_item->next = first;
//assign first to new first node_item
first = node_item;
}
//insert link at the last location
void insertLast(int key, int data) {
//create a node_item
struct node *node_item = (struct node*) malloc(sizeof(struct node));
node_item->key = key;
node_item->data = data;
if(isEmpty()) {
//make it the last node of list
last = node_item;
} else {
//make node_otem a new last node
last->next = node_item;
//mark old last node as prev of new node_item
node_item->prev = last;
}
//point last to new last node
last = node_item;
}
//delete first item
struct node* deleteFirst() {
//save reference to first link
struct node* tempNode = first;
//if only one link
if(first->next == NULL){
last = NULL;
} else {
first->next->prev = NULL;
}
first = first->next;
//return the deleted link
return tempNode;
}
//delete link at the last location
struct node* deleteLast() {
//save reference to last link
struct node *tempNode = last;
//if only one link
if(first->next == NULL) {
first = NULL;
} else {
last->prev->next = NULL;
}
last = last->prev;
//return the deleted node
return tempNode;
}
//delete a link with given key
struct node* delete(int key) {
//start from the first link
struct node* current = head;
struct node* previous = NULL;
//if list is empty
if(first == NULL) {
return NULL;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == first) {
//change first to point to next link
first = first->next;
} else {
//bypass the current link
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) {
//start from the first link
struct node *current = first;
//if list is empty
if(first == NULL) {
return false;
}
//navigate through list
while(current->key != key) {
//if it is last node
if(current->next == NULL) {
return false;
} else {
//move to next link
current = current->next;
}
}
//create a link
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = newKey;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
void 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment