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 6219301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:45:21+00:00 2026-05-24T07:45:21+00:00

I have the following C code to copy a linked list(taken from Stanford CS

  • 0

I have the following C code to copy a linked list(taken from Stanford CS Library files):

struct Node* CopyList(struct Node* head)
{
       struct Node* current = head;
       struct Node* newList= NULL;
       struct Node* tail= NULL;

       while (current !=NULL)
       {
             if(newList==NULL)
             {
                 newList=malloc(sizeof(struct Node));
                 newList->data=current->data;
                 newList->next=NULL;
                 tail= newList;
             }
             else
             {
                 tail= malloc(sizeof(struct Node));
                 tail= tail->next;
                 tail->data=current->data;
                 tail->next = NULL;
             }
             current= current->next;
       }
       return(newList);
}

I have the following as a part of my main function:

struct Node* head = NULL;
for (i=3; i >=1;i--)   //insert 3 elements into the linked list
   {                   //push inserts elements in the front
       Push(&head,i); 
   } //now a linked list 1->2->3->NULL is formed

struct Node* newlst= CopyList(head); // copies contents into a new linked list

I am compiling the code using Bloodshed Dev C++. I don’t get any compilation errors but when I run it, it just crashes. What could be the issue with this? Am I passing the right parameter to the CopyList function?

  • 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-24T07:45:22+00:00Added an answer on May 24, 2026 at 7:45 am

    Your problem lies here, in the else bit:

    tail = malloc (sizeof (struct Node));
    tail = tail->next;
    tail->data = current->data;
    tail->next = NULL;
    

    You are allocating a new node and setting tail to point to it (in that first line). Then you are using tail as if it’s the old tail. Specifically, that second line will give you a rogue pointer (as you haven’t initialised the new node with valid pointers), which will probably crash in the third line when you try to dereference it.

    You need something like:

    // First, set up the new node.
    
    newList = malloc (sizeof (struct Node));
    newList->data = current->data;
    newList->next = NULL;
    
    // Then, adjust the tail pointers.
    
    tail->next = newList;
    tail = newList;
    

    Actually, looking back at your code, what you probably intended was:

    tail->next = malloc (sizeof (struct Node)); // use tail->next, not tail.
    tail = tail->next;
    tail->data = current->data;
    tail->next = NULL;
    

    which achieves the same result.

    I suppose I should mention that you really ought to check the return values from malloc in case you run out of memory. You can do this with something like:

    tail->next = malloc (sizeof (struct Node)); // use tail->next, not tail.
    if (tail->next == NULL) {
        // do something here for recovery.
        return;
    }
    // Only continue here if the allocation worked.
    tail = tail->next;
    tail->data = current->data;
    tail->next = NULL;
    

    Without checks like that, you will get crashes when you run out of memory.

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

Sidebar

Related Questions

I have the following code to copy files sprintf(command, copy /Y %s %s, sourceFile,
Hi i have the following code: public List<Person> findAll() { List<Person> copy = new
I have a linked list of Nodes, each Node is defined as: struct Node
I have the following code where I make an copy of my database that
I have something like the following code:: <xsl:variable name=sample> <xsl:copy-of select=//foo> <xsl:copy-of select=//bar> </xsl>
I have the following block of jQuery code that I'm using to copy some
I have the following code: HTML: <label id=copyAddress class=copyAddress onclick=CopyAddress(this);> Copy Address </label> JS:
I am using the following code to copy a folder and it's contents from
I have the following code snippet (copy and paste into kaxaml, xamlpad, etc to
I have the following code (almost an exact copy of the Chat server example

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.