![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 12
| linked list initialization if yes to where my newlist->next should point (relative to the old list or not!)? tanq |
| calc is offline | |
| | #2 |
| gcc -Wall -pedantic *.c Join Date: Jan 2009 Location: London
Posts: 52
| 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. |
| flexo87 is offline | |
| | #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! |
| calc is offline | |
| | #4 |
| gcc -Wall -pedantic *.c Join Date: Jan 2009 Location: London
Posts: 52
| 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. |
| flexo87 is offline | |
| | #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;
}
}
|
| calc is offline | |
| | #6 |
| gcc -Wall -pedantic *.c Join Date: Jan 2009 Location: London
Posts: 52
| 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. |
| flexo87 is offline | |
| | #7 |
| Registered User Join Date: Jun 2009
Posts: 12
| u say even if i change allPersons its wont affect the origin list? |
| calc is offline | |
| | #8 |
| gcc -Wall -pedantic *.c Join Date: Jan 2009 Location: London
Posts: 52
| 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;
}
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 |
| flexo87 is offline | |
| | #9 |
| Registered User Join Date: Jun 2009
Posts: 12
| tanx man y r a lifesaver |
| calc is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ Linked list program need help !!! | dcoll025 | C++ Programming | 1 | 04-20-2009 10:03 AM |
| Following CTools | EstateMatt | C Programming | 5 | 06-26-2008 10:10 AM |
| Reverse function for linked list | Brigs76 | C++ Programming | 1 | 10-25-2006 10:01 AM |
| Template Class for Linked List | pecymanski | C++ Programming | 2 | 12-04-2001 09:07 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |