944,123 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2227
  • C RSS
May 21st, 2006
0

sort linked list help please

Expand Post »
Hey guys just wondering how i can sort the linked list by categoryName
for the drink type list. Iv done the linked list in my code but its not sorted?

Could someone help me write an algorithm or sum pseudocode



  1. #ifndef GJC_H
  2. #define GJC_H
  3. /* System-wide header files. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <string.h>
  8.  
  9. /* System-wide constants. */
  10. #define ID_LEN 5
  11. #define MIN_NAME_LEN 1
  12. #define MAX_NAME_LEN 25
  13. #define MIN_DESC_LEN 1
  14. #define MAX_DESC_LEN 250
  15. #define NUM_PRICES 3
  16. #define HOT 'H'
  17. #define COLD 'C'
  18. #define ERRORCODE 1
  19. #define BUFFER_SIZE 800
  20. #define NC_ARRAY_SIZE 500
  21. #define TRUE 1
  22. #define FALSE 0
  23. #define CHAR -----------------------------------------------------------------------
  24.  
  25. typedef struct category* CategoryTypePtr;
  26. typedef struct item* ItemTypePtr;
  27.  
  28. /* Structure definitions. */
  29. typedef struct price
  30. {
  31. unsigned dollars;
  32. unsigned cents;
  33. } PriceType;
  34.  
  35. typedef struct item
  36. {
  37. char itemID[ID_LEN + 1];
  38. char itemName[MAX_NAME_LEN + 1];
  39. PriceType prices[NUM_PRICES];
  40. char itemDescription[MAX_DESC_LEN];
  41. ItemTypePtr nextItem;
  42. } ItemType;
  43.  
  44. typedef struct category
  45. {
  46. char categoryID[ID_LEN + 1];
  47. char categoryName[MAX_NAME_LEN + 1];
  48. char drinkType; /* (H)ot or (C)old. */
  49. char categoryDescription[MAX_DESC_LEN];
  50. CategoryTypePtr nextCategory;
  51. ItemTypePtr headItem;
  52. unsigned numItems;
  53. } drinkType;
  54.  
  55. typedef struct gjc
  56. {
  57. CategoryTypePtr headCategory;
  58. unsigned numCategories;
  59. } GJCType;
  60.  
  61. int commandLineArguments(int argc, char *argv[]);
  62. #endif
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Code:
  69. int loadData(GJCType* menu, char* menuFile, char* submenuFile)
  70. {
  71.  
  72. drinkType *currentCat, *prevCat, *newCat;
  73. ItemType *currentItem, *prevItem, *newItem;
  74.  
  75. FILE *fp1;
  76. FILE *fp2;
  77.  
  78. char *token, *line;
  79. char array[BUFFER_SIZE + 2];
  80. double d;
  81. unsigned doll;
  82. int i;
  83. char nc_array[NC_ARRAY_SIZE + 1];
  84.  
  85. /* Open menu file for reading. */
  86. fp1 = fopen(menuFile, "r");
  87.  
  88. /* check if fp1 menu file exists*/
  89. if(fp1 == NULL)
  90. {
  91. printf("file %s does not exist \n", menuFile);
  92. exit(0);
  93.  
  94. }
  95.  
  96. /* initialize the previous node to NULL*/
  97. prevCat = NULL;
  98.  
  99. while((line = fgets(array, BUFFER_SIZE + 2, fp1)) != NULL)
  100. {
  101. /* allocate memory for CategoryType pointer*/
  102. newCat = malloc(sizeof(drinkType));
  103.  
  104. /* check if memory allocation succeeded if fails exit*/
  105. if(newCat == NULL)
  106. {
  107. printf("Memory Allocation error for newCat\n");
  108. exit(0);
  109. }
  110.  
  111. /* if the prevCat is NULL point the new node to the
  112.   start of the list*/
  113. if(prevCat == NULL)
  114. {
  115. menu->headCategory = newCat;
  116. }
  117. /* if it isnt at the start get the next -1node*/
  118. else
  119. {
  120. prevCat->nextCategory = newCat;
  121. }
  122.  
  123.  
  124. /* tokenize the Pipe and copy the field
  125.   pointers into the struct*/
  126.  
  127. token = strtok(line, "|");
  128. strcpy(newCat->categoryID, token);
  129.  
  130. token = strtok(NULL, "|");
  131. newCat->drinkType = token[0];
  132.  
  133. token = strtok(NULL, "|");
  134. strcpy(newCat->categoryName, token);
  135.  
  136. token = strtok(NULL, "|");
  137. strcpy(newCat->categoryDescription, token);
  138.  
  139. /* initialize everything to a safe state*/
  140. newCat->nextCategory = NULL;
  141. newCat->headItem = NULL;
  142. newCat->numItems = 0;
  143. prevCat = newCat;
  144. }
  145.  
  146. /* the current pointer points to headCategory*/
  147. currentCat = menu->headCategory;
  148.  
  149. /* for testing purposes traverse through the list
  150.   and print out linked list*/
  151. /*
  152.   while(currentCat != NULL)
  153.   {
  154.   printf("\n%s, %c, %s, %s", currentCat->categoryID,
  155.   currentCat->drinkType,
  156.   currentCat->categoryName,
  157.   currentCat->categoryDescription);
  158.   */
  159. /* get the next row
  160.   currentCat = currentCat->nextCategory;
  161.   }*/
  162.  
  163. /* close the first pointer to the file*/
  164. /* check if it can close it otherwise error*/
  165. if(fclose(fp1)!=0)
  166. {
  167. fprintf(stderr, "cannot close file\n");
  168. }
  169.  
  170.  
  171. /* Open submenu file for reading. */
  172. fp2 = fopen(submenuFile, "r");
  173.  
  174. /* check if sub menu file exists*/
  175. if(fp2 == NULL)
  176. {
  177. printf("file %s does not exist \n", submenuFile);
  178. exit(0);
  179.  
  180. }
  181. /*intialize pointer to the start*/
  182. prevItem = NULL;
  183. currentCat = menu->headCategory;
  184.  
  185. while((line = fgets(array, BUFFER_SIZE + 2, fp2)) != NULL)
  186. {
  187. newItem = malloc(sizeof(ItemType));
  188.  
  189. token = strtok(line, "|");
  190. strcpy(newItem->itemID, token);
  191.  
  192. token = strtok(NULL, "|");
  193. strcpy(nc_array, token);
  194.  
  195. /* put the current pointer to the head of the list*/
  196. currentCat = menu->headCategory;
  197.  
  198. /* while current is pointing to a node*/
  199.  
  200. while(currentCat != NULL)
  201. {
  202. if(strcmp(currentCat->categoryID, nc_array) == 0)
  203. {
  204. break;
  205. }
  206.  
  207. currentCat = currentCat->nextCategory;
  208. }
  209.  
  210. if(currentCat == NULL)
  211. {
  212. printf("NULL POINTER Error!!!\n");
  213. }
  214. else
  215. {
  216. token = strtok(NULL, "|");
  217. strcpy(newItem->itemName, token);
  218.  
  219. /* store dollars into struct array*/
  220. /* convert all prices to float when storing*/
  221. for(i = 0; i < NUM_PRICES; i++)
  222. {
  223. token = strtok(NULL, "|");
  224. d = atof(token);
  225. doll = (unsigned) d;
  226. newItem->prices[i].dollars = doll;
  227. newItem->prices[i].cents = (d - doll) * 100;
  228. }
  229.  
  230. token = strtok(NULL, "|");
  231. strcpy(newItem->itemDescription, token);
  232.  
  233. /* from the start of the list*/
  234. newItem->nextItem = currentCat->headItem;
  235. currentCat->headItem = newItem;
  236.  
  237. /* increment the counter*/
  238. currentCat->numItems ++;
  239. }
  240.  
  241. }
  242. currentCat = menu->headCategory;
  243. /*
  244.   while(currentCat!= NULL)
  245.   {
  246.   currentItem = currentCat->headItem;
  247.   printf("Items for category %s\n", currentCat->categoryID);
  248.  
  249.   while(currentItem != NULL)
  250.   {
  251.   printf("\n%s, %s, %s, %d.%d, %d.%d, %d.%d, %s\n",
  252.   currentItem->itemID, currentCat->categoryID,
  253.   currentItem->itemName, currentItem->prices[0].dollars,currentItem->prices[0].cents,
  254.   currentItem->prices[1].dollars, currentItem->prices[1].cents,
  255.   currentItem->prices[2].dollars, currentItem->prices[2].cents,
  256.   currentItem->itemDescription);
  257.   currentItem = currentItem->nextItem;
  258.   }
  259.  
  260.   currentCat = currentCat->nextCategory;
  261.   }*/
  262. /* try to close the file*/
  263. /* if it wont close provide an error*/
  264. if(fclose(fp2)!=0)
  265. {
  266. fprintf(stderr, "cannot close file\n");
  267. }
  268.  
  269.  
  270. return EXIT_SUCCESS;
  271.  
  272.  
  273. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
musicmancanora4 is offline Offline
34 posts
since Mar 2006
May 21st, 2006
0

Re: sort linked list help please

Is this your code? I doubt it is. And stop posting in multiple forums. You have already been guided.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: insertSortedList
Next Thread in C Forum Timeline: Urgent help needed Win_socket questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC