Advertisement
endreweast

Лабараторна 11, варіант 6(стеки)

Oct 6th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. struct Group peek();
  7. void push(struct Group value);
  8. void show_item(struct Group item);
  9. void show_items();
  10. void add_item();
  11. void pop();
  12.  
  13.  
  14. struct Group
  15. {
  16. float students;
  17. float there;
  18. float missing;
  19. };
  20.  
  21.  
  22. struct Stack
  23. {
  24. struct Group data;
  25. struct Stack *next;
  26. } *head;
  27. int stack_size = 10;
  28. int stack_length = 0;
  29.  
  30.  
  31.  
  32. int menu()
  33. {
  34. int answer;
  35. printf("Menu:\n");
  36. printf("1. Push\n");
  37. printf("2. Pop\n");
  38. printf("3. Peek\n");
  39. printf("4. Show items\n");
  40. printf("0. Exit\n");
  41. printf(">\n");
  42. scanf("%d", &answer);
  43. return answer;
  44. }
  45.  
  46. int main()
  47.  
  48. {
  49.         int answer = menu();
  50.     while (answer)
  51. {
  52.     switch (answer)
  53. {
  54.   case 1:
  55. {
  56.     add_item();
  57.     printf("Push done!\n");
  58.     break;
  59. }
  60.   case 2:
  61.     pop();
  62.     printf("Pop done!\n");
  63.     break;
  64.   case 3:
  65.     show_item(peek());
  66.     break;
  67.   case 4:
  68.     show_items();
  69.     break;
  70.     default:
  71.     printf("Error number!!!\n");
  72.     break;
  73. }
  74.     answer = menu();
  75. }
  76.     system("pause");
  77.     return 0;
  78. }
  79.  
  80.  
  81. void add_item()
  82. {
  83. struct Group new_item;
  84. printf("Enter students, there, missing:\n");
  85. scanf("%f%f%f", &new_item.students, &new_item.there, &new_item.missing);
  86. push(new_item);
  87. }
  88.  
  89. void show_item(struct Group item)
  90. {
  91. printf("Students: %s\n", item.students);
  92. printf("There: %f\n", item.there);
  93. printf("Missing: %f\n", item.missing);
  94. }
  95.  
  96. void show_items()
  97. {
  98.         struct Stack *current = head;
  99.         if (current == NULL)
  100.         {
  101.                 printf("Stack is empty!\n");
  102.         }
  103.         else
  104.         {
  105.                 int counter = 1;
  106.                 while (current != NULL)
  107.                 {
  108.                         if (current->data.missing > 6)
  109.                         {
  110.                                 printf("#%d:\n", counter);
  111.                                 show_item(current->data);
  112.                                 counter++;
  113.                         }
  114.                         current = current->next;
  115.                 }
  116.                 if (counter == 1)
  117.                 {
  118.                         printf("Not found!\n");
  119.                 }
  120.         }
  121. }
  122. void push(struct Group value)
  123. {
  124.   if (stack_length == stack_size)
  125. {
  126.   printf("Error: Stack overflow\n");
  127.    return;
  128. }
  129.   if (head == NULL)
  130. {
  131.   head = malloc(sizeof(struct Stack));
  132.   head->data = value;
  133.   head->next = NULL;
  134.   stack_length = 1;
  135. }
  136.   else
  137. {
  138. struct Stack *new_element = malloc(sizeof(struct Stack));
  139.  new_element->data = value;
  140.  new_element->next = head;
  141.  head = new_element;
  142.  stack_length++;
  143. }
  144. }
  145.  
  146. void pop()
  147. {
  148.   if (head == NULL)
  149. {
  150.   return;
  151. }
  152.  
  153.   if (head->next == NULL)
  154. {
  155.  free(head);
  156.  head = NULL;
  157.  stack_length = 0;
  158. }
  159.   else
  160. {
  161. struct Stack *buffer = head;
  162.  head = head->next;
  163.  free(buffer);
  164.  stack_length--;
  165. }
  166. }
  167.  
  168. struct Group peek()
  169. {
  170.  if (head != NULL)
  171. {
  172.  return head->data;
  173. }
  174.  
  175.  
  176. else
  177. {
  178. struct Group empty;
  179. empty.students = 0;
  180. empty.there = 0;
  181. empty.missing = 0;
  182. return empty;
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement