Thread: printing list

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    printing list

    Hey guys in my displaySummary function im trying to print out

    char categoryID[ID_LEN + 1];
    char categoryName[MAX_NAME_LEN + 1];
    char drinkType;

    but im unsure how to access the above struct variables using the variables
    below so then i can print out what is stored in them using a linked list

    GJCType* menu, char categoryType


    Code:
    void displaySummary(GJCType* menu, char categoryType)
    {
    
        CategoryTypePtr currentCat;
        ItemType currentItem;
        
        /* checking for a NULL ERROR*/
        assert(menu != NULL);
        
        /*print the summary of the list*/
      
    }
    
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
    
       CategoryType *currentCat, *prevCat, *newCat;
       ItemType *currentItem, *prevItem, *newItem;
       
       FILE *fp1;
       FILE *fp2;
       
       char *token, *line;
       char array[BUFFER_SIZE + 2];
       double d;
       unsigned doll;
       int i;
       char nc_array[NC_ARRAY_SIZE + 1];
       
       /* Open menu file for reading. */
       fp1 = fopen(menuFile, "r");
       
       /* check if fp1 menu file exists*/
       if(fp1 == NULL)
       {
          printf("file %s does not exist \n", menuFile);
          exit(0);
          
       }
       
       /* initialize the previous node to NULL*/
       prevCat = NULL;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp1)) != NULL)
       {
          /* allocate memory for CategoryType pointer*/
          newCat = malloc(sizeof(CategoryType));
          
          /* check if memory allocation succeeded if fails exit*/
          if(newCat == NULL)
          {
              printf("Memory Allocation error for newCat\n");
    	  exit(0);
          }
          
          /* if the prevCat is NULL point the new node to the 
          start of the list*/
          if(prevCat == NULL)
          {
             menu->headCategory = newCat;
          }
          /* if it isnt at the start get the next node*/
          else
          {
             prevCat->nextCategory = newCat;
          }
           
          
          /* tokenize the Pipe and copy the field
          pointers into the struct*/
               
          token = strtok(line, "|");
          strcpy(newCat->categoryID, token);
               
          token = strtok(NULL, "|");   
          newCat->drinkType = token[0];
          
          token = strtok(NULL, "|");
          strcpy(newCat->categoryName, token);
            
          token = strtok(NULL, "|");
          strcpy(newCat->categoryDescription, token);
       
          /* initialize everything to a safe state*/
          newCat->nextCategory = NULL;
          newCat->headItem = NULL;
          newCat->numItems = 0;
          prevCat = newCat;
       }
       
       /* the current pointer points to headCategory*/
       currentCat = menu->headCategory;
       
       /* for testing purposes traverse through the list
       and print out linked list*/
       /*
       while(currentCat != NULL)
       {
          printf("\n%s, %c, %s, %s", currentCat->categoryID, 
          currentCat->drinkType,
          currentCat->categoryName, 
          currentCat->categoryDescription);
        */  
          /* get the next row
          currentCat = currentCat->nextCategory;
       }*/
       
       /* close the first pointer to the file*/
       /* check if it can close it otherwise error*/
       if(fclose(fp1)!=0)
       {
           fprintf(stderr, "cannot close file\n");
       }
       
       
       /* Open submenu file for reading. */
       fp2 = fopen(submenuFile, "r");
       
       /* check if sub menu file exists*/
       if(fp2 == NULL)
       {
          printf("file %s does not exist \n", submenuFile);
          exit(0);
          
       }
       /*intialize pointer to the start*/
       prevItem = NULL;
       currentCat = menu->headCategory;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp2)) != NULL)
       {
          newItem = malloc(sizeof(ItemType));
        
          token = strtok(line, "|");
          strcpy(newItem->itemID, token);
          
          token = strtok(NULL, "|");
          strcpy(nc_array, token);
           
          /* put the current pointer to the head of the list*/
          currentCat = menu->headCategory;
          
          /* while current is pointing to a node*/
          
          while(currentCat != NULL)
          {
             if(strcmp(currentCat->categoryID, nc_array) == 0)
    	 {
    	    break;
    	 }
    	 
    	 currentCat = currentCat->nextCategory;
          }
    	 
    	 if(currentCat == NULL)
    	 {
    	    printf("NULL POINTER Error!!!\n");
    	 } 
    	 else
    	 {
                token = strtok(NULL, "|");
                strcpy(newItem->itemName, token);     
                
    	    /* store dollars into struct array*/
    	    /* convert all prices to float when storing*/
                for(i = 0; i < NUM_PRICES; i++)
                { 
                   token = strtok(NULL, "|");
                   d = atof(token); 
                   doll = (unsigned) d;  
                   newItem->prices[i].dollars = doll;
                   newItem->prices[i].cents = (d - doll) * 100;
                }
          
                token = strtok(NULL, "|");
                strcpy(newItem->itemDescription, token);
          
                /* from the start of the list*/
                newItem->nextItem = currentCat->headItem;
                currentCat->headItem = newItem;
    	    
    	    /* increment the counter*/
    	    currentCat->numItems ++;
    	 }
          
       }
       currentCat = menu->headCategory;
       /*
       while(currentCat!= NULL)
       {
          currentItem = currentCat->headItem;
          printf("Items for category %s\n", currentCat->categoryID);
       
          while(currentItem != NULL)
          {
             printf("\n%s, %s, %s, %d.%d, %d.%d, %d.%d, %s\n", 
             currentItem->itemID, currentCat->categoryID,
             currentItem->itemName, currentItem->prices[0].dollars,currentItem->prices[0].cents,
             currentItem->prices[1].dollars, currentItem->prices[1].cents,
             currentItem->prices[2].dollars, currentItem->prices[2].cents,
             currentItem->itemDescription);
             currentItem = currentItem->nextItem;
          }
          
          currentCat = currentCat->nextCategory;  
       }*/
       /* try to close the file*/
       /* if it wont close provide an error*/
       if(fclose(fp2)!=0)
       {
           fprintf(stderr, "cannot close file\n");
       }
       
       
       return EXIT_SUCCESS;
    
    
    }
    
    
    /****************************************************************************
    * Deallocates memory used in the program.
    ****************************************************************************/
    void systemFree(GJCType* menu)
    {
    
        CategoryType *currentCat, *catTemp;
        ItemType *currentItem, *itemTemp;
        
        currentCat = menu->headCategory;
        catTemp = NULL;
        
        while(currentCat != NULL)
        {
           catTemp = currentCat;
           free(currentCat);
           
           currentItem = currentCat->headItem;
           itemTemp = NULL;
           
           while(currentItem != NULL)
           {
              itemTemp = currentItem;
    	  free(currentItem);
    	  currentItem = itemTemp->nextItem;  
           }
           
           currentCat = catTemp->nextCategory;
        }
    
    
    }
    
    #include <assert.h>
    #include <string.h>
    
    /* System-wide constants. */
    #define ID_LEN 5
    #define MIN_NAME_LEN 1
    #define MAX_NAME_LEN 25
    #define MIN_DESC_LEN 1
    #define MAX_DESC_LEN 250
    #define NUM_PRICES 3
    #define HOT 'H'
    #define COLD 'C'
    #define ERRORCODE 1
    #define BUFFER_SIZE 800
    #define NC_ARRAY_SIZE 500
    #define TRUE 0
    #define FALSE -1
    
    
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    
    int commandLineArguments(int argc, char *argv[]);
    #endif

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, as a start:
    Code:
    void displaySummary(GJCType* menu, char categoryType)
    {
    
        CategoryTypePtr currentCat;
        ItemType currentItem;
        
        /* checking for a NULL ERROR*/
        assert(menu != NULL);
        
        /*print the summary of the list*/
        currentCat = menu->headCategory;
        while( currentCat != NULL )
        {
            /* print out necessary data for individual category */
            /* You fill in this part! */
            currentCat = currentCat->nextCategory;
        }
      
    }
    I don't quite see where the categoryType variable comes into play. Perhaps you mean to only display the information of those categories where drinkType matches categoryType? If so, then you just need to add an if statement and print conditionally on the basis of the result from said if statement.



    You didn't seem to have much trouble elsewhere in your code with the basic idea of how to do this (disregarding the fact that you have it all commented out):
    Code:
       /* the current pointer points to headCategory*/
       currentCat = menu->headCategory;
       
       /* for testing purposes traverse through the list
       and print out linked list*/
       /*
       while(currentCat != NULL)
       {
          printf("\n%s, %c, %s, %s", currentCat->categoryID, 
          currentCat->drinkType,
          currentCat->categoryName, 
          currentCat->categoryDescription);
        */  
          /* get the next row
          currentCat = currentCat->nextCategory;
       }*/
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    #define TRUE 0
    #define FALSE -1
    TRUE is usually defined as 1, and FALSE as 0; that way you can write statements like
    if(TRUE) or
    Code:
    char var1=TRUE;
    if(var1)
        puts("var1 is true);
    Simmilarly, -1 is usually used for errors.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

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. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM