Thread: Copying a linked list into another linked list

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    Copying a linked list into another linked list

    Hi,
    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
    My structures:
    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;
    Copy_list function:
    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->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);
    }
    And rest of code:
    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);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What did you want to actually ask?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    My function(copy_list) doesn't work.I can't figure out where is the problem.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    What is the exact problem, how does it not work?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh sure! Now Andrew wants the problem to actually be described for him!

    So spoiled! < LOL >

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Adak View Post
    Oh sure! Now Andrew wants the problem to actually be described for him!

    So spoiled! < LOL >
    LOL..... I know I am such a prima donna!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct node *ldupe( struct node *o )
    {
        struct node *n = o;
        if( o )
        {
            n = malloc( sizeof *n );
            if( n )
            {
                *n = *o;
                n->next = ldupe( o->next );
            }
        }
        return n;
    }
    Hm... that looks fun.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    LOL..... I know I am such a prima donna!
    Heck man... we already knew that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM