Archived

This topic is now archived and is closed to further replies.

Linked List

This topic is 6390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//THIS IS FOR SINGLE LINKED LISTS// Hello, I just searched through past LINKED LIST threads and got some help, however questions still remain. What's the difference between: a. head b. head->next; since head is a POINTER, doesn't 'head' point to the next struct or node anyways? Then what's head->next doing? Secondly, when you say something like: head = new; new = head; to simply begin a linked list, are you assigning head to point to new, or are you saying that head IS now new? and so there is only one pointer there, and that is the new one which has been assigned to head? I don't understand the basics (I've reread my book but I don't get it apparently). Here is a rather large function with a similar question. If you see what I don't get, I'd appreciate any tips. THANKS!
                
/*========================================================*
 * Function: add_to_list() sorts char's alphabetically in LL
 * Purpose : Inserts new link in the list
 * Entry   : int ch = character to store
 *           LISTPTR first = address of original head pointer
 * Returns : Address of head pointer (first)
 *========================================================*/

LISTPTR add_to_list( int ch, LISTPTR first )
{
   LISTPTR new_rec = NULL;       
   LISTPTR tmp_rec = NULL;       
   LISTPTR prev_rec = NULL;


   new_rec = (LISTPTR)malloc(sizeof(LIST));
   if (!new_rec)      
   {
      printf("\nUnable to allocate memory!\n");
      exit(1);
   }

   
   new_rec->ch = ch;
   new_rec->next_rec = NULL;

   if (first == NULL)   
   {
       first = new_rec;
       new_rec->next_rec = NULL;  
   }
   else    
   {
      
      if ( new_rec->ch < first->ch)
      {
         new_rec->next_rec = first;
         first = new_rec;
      }
      else   
      {
         tmp_rec = first->next_rec; 
         prev_rec = first;

         

         if ( tmp_rec == NULL )
         {
             
             prev_rec->next_rec = new_rec; //WHAT'S THE DIFF BETWEEN PREV_REC->NEXT_REC AND TMP_REC WHEN YOU LOOK AT WHAT THEY REALLY ARE?  I DON'T GET IT.

         }
         else
         {
            
            while (( tmp_rec->next_rec != NULL))
            {
               if( new_rec->ch < tmp_rec->ch )
               {
                  new_rec->next_rec = tmp_rec;
                  if (new_rec->next_rec != prev_rec->next_rec)
                  {
                     printf("ERROR");
                     getc(stdin);
                     exit(0);
                  }
                  prev_rec->next_rec = new_rec;
                  break;   
               }
               else
               {
                 tmp_rec = tmp_rec->next_rec;
                 prev_rec = prev_rec->next_rec;
               }
            }

            
            if (tmp_rec->next_rec == NULL)
            {
               if (new_rec->ch < tmp_rec->ch ) 
               {
                  new_rec->next_rec = tmp_rec;
                  prev_rec->next_rec = new_rec;
               }
               else    
               {
                  tmp_rec->next_rec = new_rec;
                  new_rec->next_rec = NULL;  
               }
            }
         }
      }
   }
   return(first);
}

                
Edited by - boltthrower on 6/16/00 11:35:48 AM Edited by - boltthrower on 6/16/00 11:37:54 AM Edited by - boltthrower on 6/16/00 1:47:23 PM

Share this post


Link to post
Share on other sites
Well, I´m too lazy to look at your program now, but for me a linked list must be so(I´m gonna use a int list):
class int_linked_list{
int value;
int * pointer_to_next;
{;

Then you write a lot of internal functions for the class, like we gotta do everytime. Unfortunately, I´m not yet that good at programming, so I just used C++ to explain the method

Thanks, Arthur(rockslave)

Share this post


Link to post
Share on other sites
quote:

What''s the difference between:
a. head
b. head->next;



head is a pointer to a structure, next is a member of this structure, so next is a pointer to the second object of the list.

Visit our homepage: www.rarebyte.de.st

GA

Share this post


Link to post
Share on other sites
quote:

Secondly, when you say something like: head = new;
new = head;



I think you should

head = new xxx;

xxx *x = head;

for(i=0;i < number_of_elements;i++)
{
x->next = new xxx;
x = x->next;
}



(where xxx is your struct type)

Visit our homepage: www.rarebyte.de.st

GA

Edited by - ga on June 16, 2000 3:22:37 PM

Share this post


Link to post
Share on other sites