Link List ???

This is a discussion on Link List ??? within the C Programming forums, part of the General Programming Boards category; Can anyone please help me with link_it() and list_it(). I need to store, sort and link the data going in. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Question Link List ???

    Can anyone please help me with link_it() and list_it().
    I need to store, sort and link the data going in. Then I need to print the results to the screen. I am sure I can get list_it to work, but I have totally goofed on Link_it... any help would be greatful.
    The header file is at the bottom...
    Thanks.


    Code:
    #include"header.h"
    
    int main(void)  {
    
    record_store records[SIZE],      /* array of records to fill, sort, search */
             target;             /* record to search for in sorted array   */
    
    not_found_t *first;          /* ptr to alphabetic first node in        */
                                 /*    linked not-found list               */    
    
    int  count,                  /* number of records actually read        */
         position;               /* index of found target in records       */
    
         /*--- Fill the array of records, sort it, display sorted list.    */
         fill_record(records, &count);
         sort_input(records, count);
         puts(" ");  puts("Sorted List:");
         print_to_screen(records, count);  puts(" ");
    
         /*--- Create empty list for not-found target records. */
         first = NULL;  
    
         /*--- Read targets and search the list; link not-found targets.   */
         scanf("%d %lf\n", &target.code, &target.percent);        
         gets(target.string);
    
         while(strcmp(target.string, SEARCH_SENTINEL))  {
             position = binary_search(records, count, target);
             if(position >= 0)  
                 printf("Found at position %2d:  %s\n", position, target.string);
             else 
                 first = link_it(first, target);                 
    
             scanf("%d %lf\n", &target.code, &target.percent);  
             gets(target.string);
         }         
    
         /*--- Display sorted list of not-found targets. */
         puts(" ");  puts("Sorted Linked List:");
         list_it(first);
    }
    
    
    /*********************************** FILL RECORDS ****/
    
    void fill_record(record_store records[], int *count)  {
    
      int i,code,z;
      char temp[LENGTH];
      double percent;
    
      *count=0;
      scanf("%d %lf\n", &code,&percent);
      gets(temp);
       for (i=0; ((i<SIZE)&&(strcmp(temp,FILL_SENTINEL)!=0)); i++)  {
       	 if (strcmp(temp,FILL_SENTINEL)!=0)  {
           strcpy(records[i].string,temp);
           records[i].code=code;
           records[i].percent=percent;
           (*count)++;
           if(i+1 < SIZE){
             scanf("%d %lf\n",&code,&percent);
             gets(temp);
           }	
         }
       }
    }
    
    /****************************** SORT INPUT****/
    
    void sort_input(record_store records[], int count)  {
    
    char temp[LENGTH];
    char min[LENGTH];
    int i,z,minimum;
    
      for (i=0; i<count; i++)  {
         minimum = i;
         strcpy(temp, records[i].string);
         strcpy(min, records[i].string);
    
         for (z=i; z<count; z++)  {
    	     if (strcmp(min, records[z].string)>0)  {
    	     strcpy(min, records[z].string);
    	     minimum=z;
    	     }
         }
      strcpy(temp, records[minimum].string);
      strcpy(records[minimum].string, records[i].string);
      strcpy(records[i].string, temp);
      }
    }
    
    
    
    /*******************************  PRINT TO SCREEN****/
    
    
    
    void print_to_screen(record_store records[], int count)  {
     int i;
      for (i=0; i<count; i++)  {
        printf("%d %lf ",records[i].code, records[i].percent);
        puts(records[i].string);
      }
    }
    
    /******************************** BINARY SEARCH ****/
    
    int binary_search(record_store records[], int count, record_store target)  {
    
     int middle;
     int bottom=0, top=count-1;
    
      while (bottom<=top)  {
        middle=(bottom+top)/2;
        if (strcmp(records[middle].string,target.string)==0)  {
    	return(middle);
        }
        else if ((strcmp(records[middle].string,target.string)>0))
    	top=(middle-1);
        else
           bottom=(middle+1);
      }
      return (-1);
    }
    
    
    /************************************ LINK IT ****/
    
    not_found_t * link_it(not_found_t *first, record_store target)  { 
      not_found_t *ptr;
     
        ptr=malloc(sizeof(not_found_t));
      /*if (first==NULL)  {
        ptr->code=target.code;
        ptr->percent=target.percent;
        strcpy(ptr->string,target.string);
        ptr->next=NULL;
        return(ptr);
      }
      else if (strcmp((ptr->string),(target.string)<0)) {  
        ptr->next=target; 
        ptr->code=target.code;
        ptr->percent=target.percent;
        strcpy(ptr->string,target.string);
        ptr->next=first;
        target.next=NULL;
      }
      else  {
         first->next=target;
         ptr->code=target.code;
         ptr->percent=target.percent;
         strcpy(ptr->string,target.string);
         target.next=ptr;
      }
     */ 
    }
    
    /************************************ LIST IT ****/
    
    void list_it(not_found_t *first)  {
    }
    Here is the Header File
    Code:
    /************************************ HEADER.H ****/
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZE             10
    #define LENGTH           21
    
    #define FILL_SENTINEL "HALT_FILL"
    #define SEARCH_SENTINEL "HALT_SEARCH"
    
    typedef struct  {
        char   string[LENGTH];
        int    code;
        double percent;
    }  record_store;
    
    typedef struct not_found_s  {
        char                string[LENGTH];
        int                 code;
        double              percent;
        struct not_found_s  *next;
    }  not_found_t;
    
    void fill_record(record_store[], int *);
    void sort_input(record_store[], int);
    void print_to_screen(record_store[], int);
    int  binary_search(record_store[], int, record_store);
    not_found_t * link_it(not_found_t *, record_store);   
    void list_it(not_found_t *);

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Whoa! You go me confused. Why are you using a search you really do not need to with LL. Here is some code I gave to my class to insert items into a linked list. Also, check your declarations.
    Code:
    /* Insert a new value into the list in sorted order */
    void insert( ListNodePtr *sPtr, char value )
    { 
       ListNodePtr newPtr, previousPtr, currentPtr;
    
       newPtr = malloc( sizeof( ListNode ) );
    
       if ( newPtr ) { 
          newPtr->data = value;
          newPtr->nextPtr = NULL;
    
          previousPtr = NULL;
          currentPtr = *sPtr;
    
          while ( currentPtr != NULL && value > currentPtr->data ) { 
             previousPtr = currentPtr;
             currentPtr = currentPtr->nextPtr;
          }
    
          if ( previousPtr == NULL ) { 
             newPtr->nextPtr = *sPtr;
             *sPtr = newPtr;
          }
          else { 
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = currentPtr;
          }
       }
       else
          printf( "%c not inserted. No memory available.\n", value );
    }
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    thanks,

    Is there anyway to help me with my code??? We had to use the first sort to physically order the list now we are using link list to logically order the list of not found items.??? does that help?

    Thanks for your help though...
    I'll try to incorporate it in and get it posted, if you get it before me though, any help would be appreciated....

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Can anyone help??

    thanks for the code earlier... I have tried for hours to work it in and am basically at a standstill.. can anyone give me a hand with this? Thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 10:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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