# Doubly Linked List Example Using C Program

## Introduction

In this post we will create doubly linked list example using C program. This example shows how to create, insert, append, remove nodes in doubly linked list using C program. We will use here Structure, which is a composite data type, in which we can define all data types under the same name or object. Size of the Structure is determined by computing the size of all data types, plus any internal padding. The key word struct is used to declare the Structure. In doubly linked list there are two links – one is left and another is right link.

## Example with Source Code

The complete source code for doubly linked list is given below:

``````/*
* Author: https://www.roytuts.com
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
struct node *left;
int data;
struct node *right;
} node;

/*
*Create a new node
*/
node* createSingleNode(int data) {
//allocate the memory for new node
node* newNode = (node*) malloc(sizeof (node));

//error allocating memory to new node
if (newNode == NULL) {
printf("Error creating new node. So exiting...\n");
exit(0);
}

//put the data
newNode->data = data;

//point to the left and right node
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

/*
*Append new node at the end of the list
*/
node* appendNode(node* head, int data) {
return NULL;
}

//point to the first node

//go to the last node
while (current->right != NULL) {
current = current->right;
}

//create the new node
node* newNode = createSingleNode(data);

//point current node to the new node
current->right = newNode;
newNode->left = current;
newNode->right = NULL;

}

/*
* Create multiple nodes
*/
node* createMultipleNodes() {
int i, noOfNodes, data;

printf("Enter no. of nodes to be created: ");
scanf("%d", &noOfNodes);

printf("Enter data for node: ");
scanf("%d", &data);

for (i = 1; i < noOfNodes; i++) {
printf("Enter data for node: ");
scanf("%d", &data);
}
}

/*
* Insert new node into the existing linked list
*/
node* insertNode(node* head, int position, node* newNode) {
int j;
node *current, *temp;

return NULL;
}

if (newNode == NULL) {
}

//if new node to be inserted at first position
if (position == 0) {
}

//point to the first node

for (j = 1; j < position && current != NULL; j++) {
current = current->right;
}

if (current != NULL) {
temp = current;
newNode->right = temp->right;
newNode->left = temp->left;
temp->right = newNode;
}

return NULL;
}

/*
*Remove node at specified position
*/
node* removeNode(node* head, int position) {
int j;
node *current, *temp;

return NULL;
}

if (position == 0) {
free(current);
}

//make next pointer of temp node to NULL
temp = current->right;

for (j = 1; j < position && temp != NULL; j++) {
current = temp;
temp = temp->right;
}

if (current != NULL) {
current->right = temp->right;
temp->left = NULL;
temp->right = NULL;
free(temp);
}

return NULL;
}

/*
*/
node* concatenate(node* p, node* q) {
node* r;
if (p = NULL) {
p = q;
}

r = p;

while (r->right != NULL) {
r = r->right;
}

q->left = r;
r->right = q;

return p;
}

/*
*/
}
node *p;
while (p->right != NULL) {
node *temp = p->right;
p->right = p->left;
p->left = temp;
p = temp;
}
p->right = p->left;
p->left = NULL;
return p;
}

/*
*/
return;
}

printf("\n");

//traverse and print data
while (current != NULL) {
printf("%d", current->data);
current = current->right;
if (current != NULL) {
printf("<->");
}
}

printf("\n");

}

void main() {
//create single node
//append node one after another

//create multiple nodes
//print nodes

//create single node
node* newNode = createSingleNode(25);
//insert new node at position 2
//print nodes
printf("\nLinkedList after inserting new node at position 2");

//remove node from position 2
//print nodes
printf("\nLinkedList after removing node from position 2");

//print nodes
}``````

## Testing

When you run the above program you will see below output:

``````LinkedList with created nodes
5<->10<->15<->20<->25
Enter no. of nodes to be created: 5
Enter data for node: 1
Enter data for node: 2
Enter data for node: 3
Enter data for node: 4
Enter data for node: 5

1<->2<->3<->4<->5

LinkedList after inserting new node at position 2
1<->2<->25<->3<->4<->5

LinkedList after removing node from position 2
1<->2<->3<->4<->5