Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:25:32+00:00 2026-05-27T07:25:32+00:00

I coded for doubly linked list implementation in C. In that, after making insertion

  • 0

I coded for doubly linked list implementation in C. In that, after making insertion of values, i am getting duplication of values. i.e. the last value given by me duplicated in all list items.

My code is as follows

header.h

#include<stdio.h>
#include<stdlib.h>
typedef struct doubly_list
{
 int id;
 char *name;
 struct doubly_list *next;
 struct doubly_list *prev;
}node;
void insertfirst(node **,int ,char *);
void insertlast(node **,int ,char *);

doubly_list_insert.c

#include"header.h"
    void insertfirst(node **head,int id,char *name)
    {
     node *tmp=(node *)malloc(sizeof(node));
     if(NULL == tmp)
     {
      printf("\nMemory allocation failed\n");
      exit(1);
     }
     tmp->id=id;
     tmp->name=name;
     tmp->prev=NULL;
     if(*head== NULL)
     {
      tmp->next=NULL;
      *head=tmp;
     }
     else
     {
      tmp->next=*head;
      (*head)->prev=tmp;
      *head=tmp;
     }
    }

    void insertlast(node **head,int id,char *name)
    {
     if(*head==NULL)
     {
      insertfirst(head,id,name);
      return;
     }
     node *last=*head;
     node *tmp=(node *)malloc(sizeof(node));
     if(NULL == tmp)
     {
      printf("\nMemory allocation failed\n");
      exit(1);
     }
     tmp->id=id;
     tmp->name=name;
     tmp->next=NULL;
     while(last->next!=NULL)
     {
      last=last->next;
     }
     last->next=tmp;
     tmp->prev=last;
    }

doubly_list_traverse.c

#include"header.h"
void traverse(node *head)
{
 node *tmp=head;
 if(head==NULL)
 {
  printf("\nList is empty\n");
  exit(1);
 }
 while(tmp!=NULL)
 {
  printf("%d --> %s\n",tmp->id,tmp->name);
  tmp=tmp->next;
 }
}

And, here comes the main file,

main.c

#include"header.h"
int main()
{
 int choice;
 int id;
 char name[15];
 node *root=NULL;
 system("clear");
 while(1)
 {
  printf("\n1.Insert First\n");
  printf("\n2.Insert Last\n");
  printf("\n3.Traverse\n");
  printf("\n4.Exit\n");
  printf("\nEnter your choice : ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
        printf("\nEnter the employee id : ");
        scanf("%d",&id);
        printf("\nEnter the employee name : ");
        scanf("%s",name);
        insertfirst(&root,id,name);
        break;
   case 2:
        printf("\nEnter the employee id : ");
        scanf("%d",&id);
        printf("\nEnter the employee name : ");
        scanf("%s",name);
        insertlast(&root,id,name);
        break;

   case 3:
        traverse(root);
        break;
   case 4:
        return 0;
        break;
   default:
        printf("\nPlease enter valid choices\n");
  }
 }
}

During execution its getting input from me properly,if i insert only one data either first or last.

But if i insert a second one, there comes the problem.
In my case, the id value remains the same. But the 2nd input’s name value is duplicated in 1st value.

Why this is happening? Is it anything wrong in passing arguments?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:25:32+00:00Added an answer on May 27, 2026 at 7:25 am

    When you create a new node, you set the node name by just copying the pointer to the name. You have to copy the string not the pointer. The strdup function is perfect for this:

    tmp->name=strdup(name);
    

    Remember to free the name when you free the nodes.

    Edit

    What happens when you call insertfirst the first time, is that the name field of the first node points to the name array in main. When you fetch the name for the second node, the contents of the array in main is updated with the new name, and since the pointer in the first node points to that array it seems like the name is duplicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my implementation of insertion sort in java for doubly linked list. I
I have a simple doubly linked list example that I'm working on, but for
Given a Circularly doubly linked list... How can i convert it into Binary Search
I have written my own linked list implementation in C, and it works fine
I have tried to implement doubly linked list in C++. Here is the code,
I have been working with a doubly linked list. Everything works OK with the
Im trying to remove a single node from a doubly threaded linked list. What
I am using GLib's doubly linked list structure, GList. I would like to know
I'm writing a doubly-linked list using TDD approach. This collection type is not thread
I'm trying to implement doubly-linked list in c++ and right now I'm adding iterator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.