Inserting a node before a given node in doubly linked list

oldDoctor

I am trying to insert a node before a given node. But I am not able to get the required output.

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

struct node{

    int data;
    struct node* prev;
    struct node* next;
};

void insert_beg(struct node** head, int new_data){
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp->data = new_data;

    if(*head == NULL){

        temp->next = *head;
        temp->prev = NULL;          
        *head = temp;
    }
    else{
        temp->next = *head;     
        (*head)->prev = temp;
        *head = temp;
     }
}

void insert_before(struct node* next_node,int new_data){
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp->data = new_data;

    if(next_node == NULL)
        printf("Invalid!!!!");


    temp->prev = next_node->prev;
    temp->next = next_node;
    next_node->prev = temp;

    if(temp->prev!=NULL)
        temp->prev->next = temp;
}

void printList(struct node* head){

    if(head == NULL)
        printf("The list is empty\n"); 
    else
        {
            while(head!=NULL){

                printf("%d\n",head->data);              
                head = head->next;              
              }
         }
}

int main(){

    struct node* head = NULL;   
    printList(head);    
    insert_beg(&head,10);
    insert_beg(&head,20);
    insert_before(head,70); 
    insert_beg(&head,30);

    printList(head);
}

Here I am trying to insert a node(with data = 70) before 20.

Output: 30,20,10

Expected Output: 30,70,20,10

dbush

When you call insert_before, if the given node is the head, then the new node will be the new head. So you need to pass the the address of head in order to modify it.

What you have right now looks like this:

head
  |
  v
------          ------          ------
- 30 -   --->   - 20 -   --->   - 10 - 
------   <---   ------   <---   ------
                  ^
------            |
- 70 -   ---------|
------

To fix this, include the address of head in the parameters to insert_before.

void insert_before(struct node **head, struct node *next_node, int new_data){
    struct node* temp = malloc(sizeof(struct node));   // don't cast the return value of malloc
    temp->data = new_data;

    if(next_node == NULL)
        printf("Invalid!!!!");


    temp->prev = next_node->prev;
    temp->next = next_node;
    next_node->prev = temp;

    if(temp->prev!=NULL) {
        temp->prev->next = temp;
    } else {
        *head = temp;
    }
}

Then call it like this:

insert_before(&head,head,70);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting a new node into a doubly linked list after a given node

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Inserting a node at the end of a doubly linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

search for a node at a given position doubly linked list

From Dev

Remove a node after a given node in a doubly linked list

From Dev

Insert a node in a doubly linked list after a given node

From Dev

Deleting a node in a doubly linked list

From Dev

Inserting Node into a Linked List

From Dev

inserting node to linked list in c

From Dev

Inserting Node in a Sorted linked list

From Dev

inserting node at beginning of linked list

From Dev

Inserting a node at the end of linked list

From Dev

Inserting a node at the end of a linked list

From Dev

inserting node at beginning of linked list

From Dev

Inserting a node in the end of a linked list

From Dev

How to append a node in a doubly linked list in Java?

From Dev

Insert Node in a Sorted Doubly linked list

From Dev

Doubly linked list, add node in-order

From Dev

Removing/undoing the only node in a doubly linked list

From Dev

Python Doubly Linked List Node Removal

From Dev

Node deletion in a doubly-linked list

From Dev

Doubly Linked List node's next is private

From Dev

Doubly linked list adding node at nth location

From Dev

How to append a node in a doubly linked list in Java?

From Dev

delete node in doubly linked list (Data Structures)

From Dev

Java - Delete Node From Doubly Linked List

From Dev

python doubly linked list - insertAfter node

From Dev

Insertion before a node in Doubly link list

Related Related

HotTag

Archive