0% found this document useful (0 votes)
11 views

Creation and Insertion in Doubly Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Creation and Insertion in Doubly Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Creation and insertion in Doubly Linked list

// Online C compiler to run C program online

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node *nextAddr;

struct node *prevAddr;

};

void create (struct node **headPointer , int data)

struct node *node;

node = (struct node *)malloc(sizeof(struct node));

node -> data = data;

node -> nextAddr = NULL;

node -> prevAddr = NULL;

//nothing in linked list

if (*headPointer == NULL)

*headPointer = node;

return;

//if list is not empty

struct node *tempNode;

tempNode = *headPointer;

while (tempNode -> nextAddr != NULL)

tempNode = tempNode -> nextAddr;


}

tempNode -> nextAddr = node;

node -> prevAddr = tempNode;

void insert (struct node **headPointer , int index , int data)

int sizeIndex = 0;

//create new node for the index

struct node *node;

node = (struct node *)malloc(sizeof(struct node));

node -> data = data;

node -> nextAddr = NULL;

node -> prevAddr = NULL;

struct node *tempNode;

tempNode = *headPointer;

while (tempNode -> nextAddr != NULL)

tempNode = tempNode -> nextAddr;

sizeIndex++;

if (index > sizeIndex){

printf("out of bounds");

return;

}
tempNode = *headPointer;

struct node *nextIndex;

for (int i = 0; i <= index ; i++)

// printf("%d\n" , tempNode -> data);

if (i == index - 1)

// tempNode = tempNode -> nextAddr;

nextIndex = tempNode -> nextAddr;

tempNode -> nextAddr = node;

node -> prevAddr = tempNode;

if (i == index)

node -> nextAddr = nextIndex;

return;

tempNode = tempNode -> nextAddr;

void printLinkList(struct node **headPointer)

struct node *tempNode;

tempNode = *headPointer;
while (tempNode -> nextAddr != NULL)

printf("%d\n" , tempNode -> data);

tempNode = tempNode -> nextAddr;

printf("%d\n" , tempNode -> data);

int main() {

struct node *head;

create(&head , 10);

create(&head , 20);

create(&head , 50);

create(&head , 30);

insert(&head , 3 , 100);

printLinkList(&head);

return 0;

You might also like