Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / linked list doubt... Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
haba

April 17, 2005, 04:24 PM

Hi,

This is what I have:

  1.  
  2. struct node
  3. {
  4.    int age;
  5.    char name[20];
  6.    struct node *next;
  7. };
  8.  
  9. void Insert(struct node **lst, int age, char name[])
  10. {
  11.    if(*lst == NULL)
  12.    {
  13.       *lst = (struct node *)malloc(sizeof(struct node));
  14.       if(*lst == NULL) return;
  15.       (*lst)->age = age;
  16.       strcpy((*lst)->name, name);
  17.       (**lst).next = NULL;
  18.    }
  19.    else
  20.       Insert(&(**lst).next, age, name);
  21. }
  22.  


function Insert, inserts a new node at the end of the list... How can I change the function to insert a new node alphabetically, based on name (using strcmp) ? (like, if current name is alphabetically bigger than new name, then, insert node before current node...)

Thanks

 
Bramz

April 17, 2005, 04:29 PM

We're not going to do your homework ...

 
haba

April 17, 2005, 05:24 PM

its not my homework, its a programming doubt... which is suitable to a programming forum... i believe

 
Reedbeta

April 17, 2005, 05:31 PM

This is a question you should be able to answer yourself with a little thought. Think about how the Insert function works! What does it have to do before it actually inserts the new node?

 
timm

April 17, 2005, 05:32 PM

INSERT THE NAMES IN ALPHABETICAL ORDER.

 
Bramz

April 17, 2005, 06:57 PM

if you had written that particular part of code yourself, you would know the answer.

 
haba

April 18, 2005, 03:10 AM

shouldn't this work (to insert at the end of the list):

  1.  
  2. void Insert(int age, char name[])
  3. {
  4.    struct node *x;
  5.    struct node *prev = head;
  6.  
  7.    while(prev->next != z)
  8.       prev = prev->next;
  9.  
  10.    x = (struct node *)malloc(sizeof *x);
  11.    x->age = age;
  12.    strcpy(x->name, name);
  13.    prev->next = x;
  14.    x->next = z;
  15. }
  16.  


where head is initialized like head->next = z and z->next = z ...

Shouldn't it work?

Thanks again

 
Chris

April 18, 2005, 03:24 AM

Try yourself: does it work ?

Where does it compare names for alphabetical order ?
Where's z declared ?

--> No, it cannot work. It won't even compile.

Try harder.

 
haba

April 18, 2005, 03:32 AM

im not trying to sort the nodes with this function... just adding new nodes to the end of the list.
z is declared like:

  1.  
  2. struct node *head, *z;
  3.  
  4. void Initialize()
  5. {
  6.    head = (struct node *)malloc(sizeof *head);
  7.    z = (struct node *)malloc(sizeof *z);
  8.    head->next = z;
  9.    z->next = z;
  10. }
  11.  


it compiles, it just doesn't do anything...

 
Danny Chapman

April 18, 2005, 04:18 AM

Don't just change it - rewrite from scratch.

A recursive list insert function... that's pretty silly (in my opinion). It will be slow (function call per element in the list) and will use memory proportional to the length of the list, which is totally unnecessary.

 
haba

April 18, 2005, 04:56 AM

got it working already:

  1.  
  2. void Initialize()
  3. {
  4.         head = (struct node *)malloc(sizeof *head);
  5.         z = (struct node *)malloc(sizeof *z);
  6.         head->next = z;
  7.         z->next = z;
  8. }
  9.  
  10. void Insert(int age, char name[])
  11. {
  12.         struct node *x;
  13.         struct node *current = head;
  14.  
  15.         while(current->next != z && strcmp(current->next->name, name) < 0)
  16.                 current = current->next;
  17.  
  18.     x = (struct node *)malloc(sizeof *x);
  19.         if(x == NULL) return;
  20.         x->age = age;
  21.         strcpy(x->name, name);
  22.         x->next = current->next;
  23.         current->next = x;
  24. }
  25.  


thanks

 
Dave

April 18, 2005, 05:31 AM

Use C++ and std::list

 
This thread contains 12 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.