Skip to content

Commit

Permalink
Add Doubly Linked List DS (NITSkmOS#468)
Browse files Browse the repository at this point in the history
doubly_linked_list.c: Add Doubly Linked List

This adds Doubly Linked List that can be traversed in both forward and
backward directions. There are functions that insert nodes and prints
the list.

Closes NITSkmOS#31
  • Loading branch information
yog-singh authored and sangamcse committed Oct 29, 2018
1 parent b950e53 commit 94e52aa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This repository contains examples of various algorithms written on different pro
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | [:octocat:](avl_tree/Cpp) | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | [:octocat:](binary_search_tree/Cpp) | | |
| [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree) | | | [:octocat:](fenwick_tree/java) | [:octocat:](fenwick_tree/Python) |

| [Doubly Linked List](https://en.wikipedia.org/wiki/Doubly_linked_list) | [:octocat:](doubly_linked_list/c) | | | |

## Sample Run

Expand Down
73 changes: 73 additions & 0 deletions doubly_linked_list/c/doubly_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>

//Node struct for the Linked List
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

//An empty list is created.
void create_list(struct Node **head_ref) {
*head_ref = NULL;
printf("List created successfully.");
return;
}

/* Given reference to the head of the list, this function
inserts a node at the beginning of the List*/
void insert(struct Node **head_ref, int new_data) {
//Allocate memory for the node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}

/*Given reference to the head of the list, insert_before()
adds a node before a given node*/
void insert_before(struct Node *next_node, int new_data) {
if (next_node == NULL) {
printf("The given Next node is NULL");
return;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
}

// Print the list in both directions
void print_list(struct Node *node) {
struct Node* last;
printf("\nTraversal in forward direction \n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\nTraversal in reverse direction \n");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}
printf("\n");
}

int main() {
struct Node *head;
create_list(&head);
insert(&head, 7);
insert(&head, 1);
insert(&head, 4);
insert_before(head->next, 8);
print_list(head);
return 0;
}

0 comments on commit 94e52aa

Please sign in to comment.