Thread: linked list initialization

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    linked list initialization

    can i initializing one list with another list?
    if yes to where my newlist->next should point (relative to the old list or not!)?

    tanq

  2. #2
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    What do you mean?.. do you want an empty list point to another? in that case you would assign the head of the old list to the head of the new one.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    no,i have a list, and i want to copy it to another empty list and make some action which i dont want to effect the origin(old list) list and i dont know how!

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Quote Originally Posted by calc View Post
    no,i have a list, and i want to copy it to another empty list and make some action which i dont want to effect the origin(old list) list and i dont know how!
    If you copy it you won't affect anything, but you have to go through the list and, copy each node, and put it into the new list. you can't just assignment newlist->next.... because you would work on the same structure.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    Code:
    typedef struct Person {
        int id;
        char* name;
        struct PersonList* friends;
    } Person;
    
    typedef struct PersonList {
        Person* person;
        struct PersonList* next;
    } PersonList;

    Code:
    void copy(PersonList* allPersons)
    {
    	int max=0,numfr=0;
    	PersonList *newlist=0;
    	newlist=(PersonList*) malloc(sizeof(PersonList));
    	while(allPersons)
    	{
    		newlist->person=(Person*)malloc(sizeof(Person));
            newlist->person->name=(char*)malloc(100*sizeof(char));
    		newlist->person->friends=(PersonList*) malloc(sizeof(PersonList));
    		newlist->person=allPersons->person;
    		strcpy(newlist->person->name,allPersons->person->name);
    		newlist->person->friends=allPersons->person->friends;
    		newlist->person->id=allPersons->person->id;
    		newlist->next=   //i dont know what to do here!!!!!<<<<<<<<<<<<<
    		allPersons=allPersons->next;
    
    	}
    }
    do u have any solution?

  6. #6
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    yes, why does your function not return anything? in that function you can modify the list but you can't change the value of "PersonList* allPersons" which is a pointer. thus the assignment "allPersons=allPersons->next;" modifies nothing.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    u say even if i change allPersons its wont affect the origin list?

  8. #8
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Code:
    PersonList *copyt(PersonList *allPersons) {
         	int max=0,numfr=0;
    	PersonList *newlist=0;
    	
    	while(allPersons)
    	{
                    newlist=(PersonList*) malloc(sizeof(PersonList));
    		newlist->person=(Person*)malloc(sizeof(Person));
                   newlist->person->name=(char*)malloc(100*sizeof(char));
                   /* you shouldn't allocate memory for the friends cause then you should copy also that list */
    		//wrong newlist->person->friends=(PersonList*) malloc(sizeof(PersonList));
                   // here you are just copyng an address not the structure
    		//wrong newlist->person=allPersons->person;
    		strcpy(newlist->person->name,allPersons->person->name);
    		newlist->person->friends=allPersons->person->friends;
    		newlist->person->id=allPersons->person->id;
    		newlist=newlist->next;
    		allPersons=allPersons->next;
    
    	}
            return newlist;
    }
    Now should be ok! ;-)

    Sorry i forgot that you have to use a tmp variable to store the address of the newlist head, otherwise that function will return the last node (NULL!!)
    Last edited by flexo87; 07-04-2009 at 11:08 AM. Reason: MISSAT

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    tanx man y r a lifesaver

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM