Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite a C program to create a copy of a linked list
    primarykey
    data
    text
    <p>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</p> <pre><code>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; </code></pre> <p>My copy_list function:</p> <pre><code>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-&gt;name, current-&gt;name); newList-&gt;restp = NULL; tail = newList; } else { tail-&gt;restp = malloc(sizeof(name_node_t)); tail = tail-&gt;restp; strcpy(tail-&gt;name, current-&gt;name); tail-&gt;restp = NULL; } current = current-&gt;restp; } return(newList); } </code></pre> <p>Rest of code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; 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-&gt;name, "Eisenhower"); insertAfter(list.headp-&gt;restp, new_node); lastDelete(&amp;list); place_first(&amp;list.headp, "Mustafa"); printf("%s %s %s %s", list.headp-&gt;name, list.headp-&gt;restp-&gt;name, list.headp-&gt;restp-&gt;restp-&gt;name, list.headp-&gt;restp-&gt;restp-&gt;restp-&gt;name); list_two.headp = copy_list(list.headp); printf("%s %s %s %s", list_two.headp-&gt;name, list.headp-&gt;restp-&gt;name, list.headp-&gt;restp-&gt;restp-&gt;name, list.headp-&gt;restp-&gt;restp-&gt;restp-&gt;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-&gt;name, "Washington"); head-&gt;restp = second; strcpy(second-&gt;name, "Roosevelt"); second-&gt;restp = third; strcpy(third-&gt;name, "Kennedy"); third-&gt;restp = NULL; return(head); } void insertAfter(name_node_t* mynode,name_node_t* newNode) { newNode-&gt;restp = mynode-&gt;restp; mynode-&gt;restp = newNode; } void ListDelete(name_list_t* listP, char pname[]){ name_node_t *to_freep, *cur_nodep; if(strcmp(listP-&gt;headp-&gt;name, pname)){ to_freep = listP-&gt;headp; listP-&gt;headp = to_freep-&gt;restp; --(listP-&gt;size); } else { for (cur_nodep = listP-&gt;headp; cur_nodep-&gt;restp != NULL &amp;&amp; !strcmp(cur_nodep-&gt;restp-&gt;name, pname); cur_nodep = cur_nodep-&gt;restp) { if( cur_nodep-&gt;restp != NULL &amp;&amp; strcmp(cur_nodep-&gt;restp-&gt;name, pname)) { to_freep = cur_nodep-&gt;restp; cur_nodep-&gt;restp = to_freep-&gt;restp; free(to_freep); --(listP-&gt;size); } } } } void lastDelete(name_list_t* listP){ name_node_t *to_freep, *cur_nodep; for (cur_nodep = listP-&gt;headp; cur_nodep-&gt;restp != NULL; cur_nodep = cur_nodep-&gt;restp) {} to_freep = cur_nodep; cur_nodep-&gt;restp = to_freep-&gt;restp; free(to_freep); --(listP-&gt;size); } void place_first(name_node_t **headRef, char pname[]) { name_node_t *newNode = malloc(sizeof(name_node_t)); strcpy(newNode-&gt;name, pname); newNode-&gt;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 = &amp;newList; while (current != NULL) { printf("**"); place_first(lastPtr, current-&gt;name); lastPtr = &amp;((*lastPtr)-&gt;restp); current = current-&gt;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-&gt;name, head-&gt;name); newList-&gt;restp = copy_list(head-&gt;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-&gt;name, current-&gt;name); newList-&gt;restp = NULL; tail = newList; } else { tail-&gt;restp = malloc(sizeof(name_node_t)); tail = tail-&gt;restp; strcpy(tail-&gt;name, current-&gt;name); tail-&gt;restp = NULL; } current = current-&gt;restp; } return(newList); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload