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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:58:18+00:00 2026-05-23T20:58:18+00:00

I want to write a copy_list function that creates a linked list(the function result)

  • 0

I want to write a copy_list function that creates a linked list(the function result) with new nodes that contain the same data as the linked list referenced by the single argument of copy_list.But my copy_list function doesn’t work.It goes into infinite loop,While loop doesn’t quit.
My structures

typedef struct name_node_s {
  char name[11];
  struct name_node_s *restp;
}name_node_t;
typedef struct {
  name_node_t *headp;
  int size;
}name_list_t;

My copy_list function:

name_node_t *copy_list(name_node_t *head){
    name_node_t *current = head;
    name_node_t *newList = NULL;
    name_node_t *tail = NULL;

    while (current != NULL){
        if (newList == NULL) {
            newList = malloc(sizeof(name_node_t));
            strcpy(newList->name, current->name);
            newList->restp = NULL;
            tail = newList;
        }
        else {
            tail->restp = malloc(sizeof(name_node_t));
            tail = tail->restp;
            strcpy(tail->name, current->name);
            tail->restp = NULL;
        }
        current = current->restp;
    }
    return(newList);
}

Rest of code:

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

typedef struct name_node_s {
  char name[11];
  struct name_node_s *restp;
}name_node_t;
typedef struct {
  name_node_t *headp;
  int size;
}name_list_t;
name_node_t* presidents(void);
void insertAfter(name_node_t* mynode,name_node_t* newNode);
//void delete_last(name_node_t** headRef);
//void ListDelete(name_list_t* listP, char pname[]);
void lastDelete(name_list_t* listP);
void place_first(name_node_t **headRef, char pname[]);
name_node_t *copy_list(name_node_t *head);
int main(void)
{
  name_list_t list;
  name_list_t list_two;
  //name_node_t *np, *qp;
  list.headp = presidents();
  name_node_t *new_node;
  new_node = malloc(sizeof(name_node_t));
  strcpy(new_node->name, "Eisenhower");
  insertAfter(list.headp->restp, new_node);
  lastDelete(&list);
  place_first(&list.headp, "Mustafa");
  printf("%s %s %s %s", list.headp->name, list.headp->restp->name, list.headp->restp->restp->name, list.headp->restp->restp->restp->name);
  list_two.headp = copy_list(list.headp);
  printf("%s %s %s %s", list_two.headp->name, list.headp->restp->name, list.headp->restp->restp->name, list.headp->restp->restp->restp->name);

  return(0);
}
name_node_t* presidents(void)
{
  name_node_t* head = NULL;
  name_node_t* second = NULL;
  name_node_t* third = NULL;

  head = malloc(sizeof(name_node_t));
  second = malloc(sizeof(name_node_t));
  third = malloc (sizeof(name_node_t));

  strcpy(head->name, "Washington");
  head->restp = second;

  strcpy(second->name, "Roosevelt");
  second->restp = third;

  strcpy(third->name, "Kennedy");
  third->restp = NULL;

  return(head);
}
void insertAfter(name_node_t* mynode,name_node_t* newNode)
{
    newNode->restp = mynode->restp;
    mynode->restp = newNode;
}

void ListDelete(name_list_t* listP, char pname[]){
    name_node_t *to_freep, *cur_nodep;
    if(strcmp(listP->headp->name, pname)){
        to_freep = listP->headp;
        listP->headp = to_freep->restp;
        --(listP->size);
    }
    else {
        for (cur_nodep = listP->headp;
            cur_nodep->restp != NULL && !strcmp(cur_nodep->restp->name, pname);
            cur_nodep = cur_nodep->restp) {
                if( cur_nodep->restp != NULL && strcmp(cur_nodep->restp->name, pname)) {
                    to_freep = cur_nodep->restp;
                    cur_nodep->restp = to_freep->restp;
                    free(to_freep);
                    --(listP->size);
                }
            }
        }
    }
void lastDelete(name_list_t* listP){
    name_node_t *to_freep, *cur_nodep;
    for (cur_nodep = listP->headp;
            cur_nodep->restp != NULL;
            cur_nodep = cur_nodep->restp) {}
    to_freep = cur_nodep;
    cur_nodep->restp = to_freep->restp;
    free(to_freep);
    --(listP->size);
}
void place_first(name_node_t **headRef, char pname[]) {
    name_node_t *newNode = malloc(sizeof(name_node_t));
    strcpy(newNode->name, pname);
    newNode->restp = *headRef;
    *headRef = newNode;
}
/*name_node_t *copy_list(name_node_t *head) {
    name_node_t *current = head;
    name_node_t *newList = NULL;
    name_node_t **lastPtr;

    lastPtr = &newList;

    while (current != NULL) {
        printf("**");
        place_first(lastPtr, current->name);
        lastPtr = &((*lastPtr)->restp);
        current = current->restp;
    }
    return(newList);
}*/
/*name_node_t *copy_list(name_node_t *head) {
    if (head == NULL)
        return NULL;
    else {
        name_node_t *newList = malloc(sizeof(name_list_t));
        strcpy(newList->name, head->name);
        newList->restp = copy_list(head->restp);

        return(newList);
    }
}*/
/name_node_t *copy_list(name_node_t *head){
    name_node_t *current = head;
    name_node_t *newList = NULL;
    name_node_t *tail = NULL;

    while (current != NULL){
        if (newList == NULL) {
            newList = malloc(sizeof(name_node_t));
            strcpy(newList->name, current->name);
            newList->restp = NULL;
            tail = newList;
        }
        else {
            tail->restp = malloc(sizeof(name_node_t));
            tail = tail->restp;
            strcpy(tail->name, current->name);
            tail->restp = NULL;
        }
        current = current->restp;
    }
    return(newList);
}
  • 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-23T20:58:18+00:00Added an answer on May 23, 2026 at 8:58 pm

    In lastDelete(), this loop:

    for (cur_nodep = listP->headp;
                cur_nodep->restp != NULL;
                cur_nodep = cur_nodep->restp) {}
    

    … stops at the last node in the list. Afterwards you never set restp to NULL in the second-to-last element. You only work on the last one as to_freep and cur_nodep point to the same element.

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

Sidebar

Related Questions

I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a command that specifies the word under the cursor in
I want to write a little DBQuery function in perl so I can have
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div
I want to write something that acts just like confirm() in javascript, but I
What I want to have is a box that displays a list of messages
I'm trying to write a function in jQuery that will split lists - or
I have a linked list of Nodes, each Node is defined as: struct Node

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.