Skip to content

Instantly share code, notes, and snippets.

@armsp
Last active May 9, 2018 18:16
Show Gist options
  • Save armsp/5990976fba13bed0eeccdbb6d50b7a23 to your computer and use it in GitHub Desktop.
Save armsp/5990976fba13bed0eeccdbb6d50b7a23 to your computer and use it in GitHub Desktop.
Doubly linked list implemented in C without any global variables.
#include <stdio.h>
#include <stdlib.h>
#include "doublyLinkedList.h"
Node* createnode(){
Node* node = malloc(sizeof(*node));
return node;
}
void addNode(Node** head, int info){
Node* node = createnode();
node->key = info;
node->next = NULL;
if(*head == NULL){
*head = createnode();
node->prev = NULL;
}
else{
node->prev = *head;
(*head)->next = node;
}
*head = node;
}
void deleteHeadNode(Node** head){
Node* tmp = *head;
(*head)->prev->next = NULL;
(*head) = (*head)->prev;
tmp->prev = NULL;
free(tmp);printf("Head Node deleted.\n");
}
void insertNodeBetween(Node** head, int info, int position){
Node* tmp = *head;
int counter = 1;
while(tmp->prev != NULL){
tmp = tmp->prev;
counter++;
}
if(position > counter){
printf("Position exceeds the number of elements. Cannot insert element.\n");
return ;
}
else{
while(--position){
tmp = tmp->next;
}
Node* new = malloc(sizeof(*new));
new->key = info;
new->next = tmp->next;
new->prev = tmp;
tmp->next->prev = new;
tmp->next = new;
}
}
void addNodeTail(Node** head, int data){
Node* tmp = *head;
while(tmp->prev != NULL){
tmp = tmp->prev;
}
Node* new = malloc(sizeof(*new));
new->key = data;
new->prev = NULL;
new->next = tmp;
tmp->prev = new;
}
void deleteNode(Node** head, int position){
Node* tmp = *head;
int counter = 1;
int n = position;
while(tmp->prev != NULL){
tmp = tmp->prev;
counter++;
}
if(position > counter){
printf("Position exceeds the number of elements. Unable to delete.\n");
return ;
}
else if(position == 1){
tmp->next->prev = NULL;
tmp->next = NULL;
free(tmp);
printf("Deleted tail node\n");
}
else{
while(--n){
tmp = tmp->next;
}
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
tmp->next = NULL;
tmp->prev = NULL;
free(tmp);printf("Element deleted.\n");
}
}
void getNode(Node** head, int position){
Node* tmp = *head;
int counter = 1;
int n = position;
while(tmp->prev != NULL){
counter++;
tmp = tmp->prev;
}
if(position > counter){
printf("Position exceeds the number of elements. Cannot fetch node.\n");
return ;
}
else{
while(--n){
//printf("n = %d ", n);
tmp = tmp->next;
}
printf("Node at %d is %d\n",position, tmp->key);
}
}
void searchNode(Node** head, int searchfor){
Node* tmp = *head;
int searchswitch = 0;
while(tmp != NULL){
if(tmp->key == searchfor){
printf("Element %d found!!\n",searchfor);
searchswitch = 1;
}
tmp = tmp->prev;
}
if(!searchswitch){
printf("Element %d NOT found.\n",searchfor);
}
}
void printRev(Node* head){
Node* tmp = head;
while(tmp != NULL){
printf("<-[%d]",(tmp)->key);
tmp = (tmp)->prev;
}
printf("\n");
}
void printFwd(Node* head){
Node* tmp = head;
while(tmp->prev != NULL){
tmp = (tmp)->prev;
}
while(tmp != NULL){
printf("[%d]->", tmp->key);
tmp = tmp->next;
}
printf("\n");
}
#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H
typedef struct node {
int key;
struct node* next;
struct node* prev;
} Node;
Node* createnode();
void addNode(Node** head, int info);
void deleteHeadNode(Node** head);
void deleteNode(Node** head, int position);
void insertNodeBetween(Node** head, int info, int position);
void addNodeTail(Node** head, int data);
void getNode(Node** head, int position);
void searchNode(Node** head, int searchfor);
void printRev(Node* head);
void printFwd(Node* head);
#endif
#include <stdlib.h>
#include "doublyLinkedList.h"
int main(){
Node* head = NULL;
addNode(&head, 21);
addNode(&head, 33);
addNode(&head, 41);
addNode(&head, 56);
addNode(&head, 78);
addNode(&head, 99);
addNode(&head, 700);
printFwd(head);
deleteHeadNode(&head);
printFwd(head);
insertNodeBetween(&head,889,3);
insertNodeBetween(&head,92,3);
insertNodeBetween(&head,770,4);
insertNodeBetween(&head,770,64);
printFwd(head);
searchNode(&head, 78);
searchNode(&head, 99999);
addNodeTail(&head, 111111);
addNodeTail(&head, 3331);
printFwd(head);
deleteNode(&head, 1);
deleteNode(&head, 7);
deleteNode(&head, 97);
insertNodeBetween(&head, 101010, 7);
getNode(&head, 5);
getNode(&head, 1);
getNode(&head, 12);
printFwd(head);
printRev(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment