fork download
  1. /* main.c */
  2.  
  3. /*
  4.  * LL of students
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. typedef struct STUDENT
  12. {
  13. char Lastname[64];
  14. char Firstname[64];
  15. int UIN;
  16. char Major[16];
  17. struct STUDENT *Next;
  18. } STUDENT;
  19.  
  20. STUDENT *InputStudents(FILE *input)
  21. {
  22. char ln[64];
  23. char fn[64];
  24. char uin[16];
  25. char major[16];
  26.  
  27. STUDENT *head = NULL;
  28.  
  29. fscanf(input, "%s", ln);
  30. while (strcmp(ln, "#") != 0) // until we see EOF marker
  31. {
  32. fscanf(input, "%s", fn);
  33. fscanf(input, "%s", uin);
  34. fscanf(input, "%s", major);
  35.  
  36. STUDENT *T = (STUDENT *)malloc(sizeof(STUDENT));
  37.  
  38. strcpy(T->Lastname, ln);
  39. strcpy(T->Firstname, fn);
  40. T->UIN = atoi(uin);
  41. strcpy(T->Major, major);
  42. T->Next = NULL;
  43.  
  44. // add to front of list:
  45. if (head == NULL)
  46. head = T;
  47. else
  48. {
  49. T->Next = head;
  50. head = T;
  51. }
  52.  
  53. // next:
  54. fscanf(input, "%s", ln);
  55. }
  56.  
  57. // done:
  58. return head;
  59. }
  60.  
  61. //
  62. // returns pointer to STUDENT if found, NULL if not:
  63. //
  64. STUDENT *SearchByUIN(STUDENT *students, int uin)
  65. {
  66. int sUin;
  67.  
  68. while (students != NULL){
  69. sUin = students->UIN;
  70. if (uin == sUin){
  71. return students;
  72. }
  73. students = students->Next;
  74. }
  75. return NULL;
  76. }
  77.  
  78. //
  79. // deletes the given student, returning a pointer to the
  80. // head of the new linked-list:
  81. //
  82. // NOTE: if the student is not found, no change is made,
  83. // and the original head of the LL is returned.
  84. //
  85. STUDENT *DeleteByLastName(STUDENT *students, char *name)
  86. {
  87. STUDENT *temp;
  88. STUDENT *pNext;
  89.  
  90. int sUin;
  91.  
  92. while (students != NULL){
  93. pNext = students->Next;
  94. if (strcmp(name, students->Next.Lastname) == 0){
  95. students->Next = pNext->Next;
  96. }
  97. }
  98. return NULL;
  99. }
  100.  
  101.  
  102. int main()
  103. {
  104. FILE *input;
  105. STUDENT *students, *S;
  106. int alg;
  107. int uin;
  108. char name[64];
  109.  
  110. input = stdin;
  111.  
  112. students = InputStudents(input);
  113. fscanf(input, "%d", &alg);
  114.  
  115. switch (alg)
  116. {
  117. case 1:
  118. fscanf(input, "%d", &uin);
  119. printf("**%d**\n", uin);
  120. S = SearchByUIN(students, uin);
  121. break;
  122.  
  123. case 3:
  124. fscanf(input, "%s", name);
  125. printf("**%s**\n", name);
  126. students = DeleteByLastName(students, name);
  127. break;
  128.  
  129. default:
  130. printf("**Error: unknown alg code (%d)?!\n\n", alg);
  131. exit(-1);
  132. }
  133.  
  134. while (students != NULL)
  135. {
  136. printf("%d: %s, %s (%s)\n",
  137. students->UIN,
  138. students->Lastname,
  139. students->Firstname,
  140. students->Major);
  141.  
  142. students = students->Next;
  143. }
  144.  
  145. if (alg == 1 || alg == 2)
  146. {
  147. if (S == NULL)
  148. printf("**NOT FOUND**\n");
  149. else
  150. printf("**%d: %s, %s (%s)**\n",
  151. S->UIN,
  152. S->Lastname,
  153. S->Firstname,
  154. S->Major);
  155. }
  156.  
  157. return 0;
  158. }
Compilation error #stdin compilation error #stdout 5s 2304KB
stdin
Bag Jim 707 ME Hummel Joe 234 CS Lore Beth 345 BioE Deitz Sean 123 CS Li Xing 987 CS Hwang James 111 CE Sankar Pooja 555 IT # 3 Deitz #
compilation info
In file included from /usr/include/string.h:635:0,
                 from prog.c:9:
prog.c: In function 'DeleteByLastName':
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:94:37: error: request for member 'Lastname' in something not a structure or union
      if (strcmp(name, students->Next.Lastname) == 0){
                                     ^
prog.c:90:7: warning: unused variable 'sUin' [-Wunused-variable]
   int sUin;
       ^
prog.c:87:12: warning: unused variable 'temp' [-Wunused-variable]
   STUDENT *temp;
            ^
stdout
Standard output is empty