Created
August 4, 2018 05:48
-
-
Save rafiulgits/54aa1ecee9b531cb33c1b7eeb4f68c66 to your computer and use it in GitHub Desktop.
Doubly Linked List with C. Applications: Operating System management, Browser page path, File Explorer path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Doubly Linked list | |
* @copyright: Rafiul Islam | |
* | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
/* | |
* A structure with 2 pointer that link next and previous node | |
* and a integer value container. | |
* | |
* #Note#: Linked list insert by front. There next means initial | |
* last node that inserted before current and previous means | |
* initial next node that inserted after current one. | |
*/ | |
struct List | |
{ | |
int value; | |
struct List *next; | |
struct List *prev; | |
}; | |
/* | |
* Create a head pointer for pointing current node and | |
* initialized it with NULL (@see main function) | |
*/ | |
struct List *head; | |
/* | |
* Function prototype | |
*/ | |
void add(int item); | |
void pop_back(); | |
void showCurrent(); | |
void main() | |
{ | |
head = NULL; //head pointer initialization | |
/* | |
* This block should print: | |
* Current: 20 | |
* Next: 10 | |
* Previous: No element | |
*/ | |
add(10); | |
add(20); | |
showCurrent(); | |
/* | |
* This block should print: | |
* Current: 10 | |
* Next: No element | |
* Previous: No element | |
*/ | |
pop_back(); | |
showCurrent(); | |
/* | |
* @see Head link change | |
* This block should print: | |
* Current: 30 | |
* Next: 40 | |
* Previous: 20 | |
*/ | |
add(20); | |
add(30); | |
add(40); | |
head = head->next; | |
showCurrent(); | |
} | |
void add(int item) | |
{ | |
/* | |
* Create a new member node, set the item value. | |
* member->next should be current head | |
* member->previous should be null always | |
* when current head has any node then this current node | |
* should link its previous to member by this way | |
* head->previous(current->previous) = member | |
* Finally head will point new member by head=member | |
*/ | |
struct List *member = (struct List*)malloc(sizeof(struct List)); | |
member->value = item; | |
member->next = head; | |
member->prev = NULL; | |
if(head != NULL) | |
head->prev = member; | |
head = member; | |
} | |
void pop_back() | |
{ | |
/* | |
* When list is not empty then point the head with a new | |
* pointer and move head pointer to its next one. Then release | |
* the last element with the new pointer.And set current | |
* head->previous = NULL as there are no more element on this way. | |
*/ | |
if(head == NULL) | |
{ | |
puts("No element available"); | |
return; | |
} | |
struct List *member = (struct List*)malloc(sizeof(struct List)); | |
member = head; | |
head = member->next; | |
free(member); | |
if(head == NULL) | |
return; | |
head->prev = NULL; | |
} | |
void showCurrent() | |
{ | |
if(head == NULL) | |
{ | |
puts("List empty\n-------------------------------"); | |
} | |
else | |
{ | |
printf("Current value: %d\n",head->value); | |
head->prev == NULL ? | |
puts("No previous"): | |
printf("Previous value: %d\n",head->prev->value); | |
head->next == NULL ? | |
puts("No next"): | |
printf("Next value: %d\n",head->next->value); | |
puts("----------------------------------------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment