0

i has defined a list using :

typedef struct st_list {
Node * by_name ;
Node * by_age;
} SortedList ;

After that I defined the node using :

typedef struct st_node {
char name [100];
unsigned int age;
struct st_node * suivant_nom ;
struct st_node * suivant_age ;
} Node ; 

After that i defined a function to insert an element in this list :

void insert (SortedList* l , char* name , int age ){
     Node *N,*A,*tmp,*new;
     N=l->by_name;
     A=l->by_age;
     tmp=NULL;
     new= (Node*)malloc(sizeof(Node));
     new->age=age;
     new->name=strcpy(new->name,name);
     if (N==NULL) {N=new;}
     while ((N!=NULL) & strcpy(name,N->name)<0){
          tmp=N;
          N=N->suivant_nom;}
     tmp->suivant_nom=new;
     new->suivant_nom=N;
    while ((N!=NULL) & strcpy(name,N->name)<0){
          tmp=N;
          N=N->suivant_nom;}
     tmp->suivant_age=new;
     new->suivant_age=N;}

After that i defined also a function with this prototype :

Node* copyNode(None* p) ;

Which copy a node and return a pointer to a copy of it .

What i want to do right now is to implement a function that make a copy of a list given as argument , it's prototype will be :

SortedList* duplicatelist( SortedList l) ;

This function will return a pointer to the copy of the SortedList l ; I know an Algorithm In C which copy a LinkedList , like How do you copy a linked list into another list? , but there we can not use this method because the list is double sorted . I tried to use an array and put all the elements of the list given in argument , and make a for loop such as that tab[i]->suivant_age=tab[i+1] but there again , we can implement the sorting by age . I hope i was clear and i explaned well my problem ?

10
  • what did you try?
    – OznOg
    Oct 18, 2018 at 17:24
  • @OznOg I tried to use an array and put all the elements of the list given in argument , and make a for loop such as that tab[i]->suivant_age=tab[i+1] but there again , we can implement the sorting by age Oct 18, 2018 at 17:26
  • The Pico style of indentation with the close brace hidden on the right is not a conventional C style. Please use Allman (my choice) or 1TBS — see Wikipedia on Indentation Styles. Oct 18, 2018 at 17:48
  • @JonathanLeffler The problem is not in this function , the main reason why i am asking there is for the function duplicate function Oct 18, 2018 at 17:48
  • OK; if the problem isn't in this code, why are you showing it to us? Where is your code that does attempt to handle the copying of a complete list? Please create an MCVE (minimal reproducible example) showing that code in particular. I think you should be able to handle it by creating a second list structure, then iterating over one (it doesn't matter which) of the lists in the original, repeatedly inserting into the copy. Oct 18, 2018 at 17:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.