C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 10:17 AM   #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
calc is offline   Reply With Quote
Old 07-04-2009, 10:30 AM   #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   Reply With Quote
Old 07-04-2009, 10:33 AM   #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   Reply With Quote
Old 07-04-2009, 10:38 AM   #4
gcc -Wall -pedantic *.c
 
Join Date: Jan 2009
Location: London
Posts: 52
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.
flexo87 is offline   Reply With Quote
Old 07-04-2009, 10:44 AM   #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?
calc is offline   Reply With Quote
Old 07-04-2009, 10:49 AM   #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   Reply With Quote
Old 07-04-2009, 10:53 AM   #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   Reply With Quote
Old 07-04-2009, 10:57 AM   #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;
}
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
flexo87 is offline   Reply With Quote
Old 07-04-2009, 10:58 AM   #9
Registered User
 
Join Date: Jun 2009
Posts: 12
tanx man y r a lifesaver
calc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22