Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Group peek();
- void push(struct Group value);
- void show_item(struct Group item);
- void show_items();
- void add_item();
- void pop();
- struct Group
- {
- float students;
- float there;
- float missing;
- };
- struct Stack
- {
- struct Group data;
- struct Stack *next;
- } *head;
- int stack_size = 10;
- int stack_length = 0;
- int menu()
- {
- int answer;
- printf("Menu:\n");
- printf("1. Push\n");
- printf("2. Pop\n");
- printf("3. Peek\n");
- printf("4. Show items\n");
- printf("0. Exit\n");
- printf(">\n");
- scanf("%d", &answer);
- return answer;
- }
- int main()
- {
- int answer = menu();
- while (answer)
- {
- switch (answer)
- {
- case 1:
- {
- add_item();
- printf("Push done!\n");
- break;
- }
- case 2:
- pop();
- printf("Pop done!\n");
- break;
- case 3:
- show_item(peek());
- break;
- case 4:
- show_items();
- break;
- default:
- printf("Error number!!!\n");
- break;
- }
- answer = menu();
- }
- system("pause");
- return 0;
- }
- void add_item()
- {
- struct Group new_item;
- printf("Enter students, there, missing:\n");
- scanf("%f%f%f", &new_item.students, &new_item.there, &new_item.missing);
- push(new_item);
- }
- void show_item(struct Group item)
- {
- printf("Students: %s\n", item.students);
- printf("There: %f\n", item.there);
- printf("Missing: %f\n", item.missing);
- }
- void show_items()
- {
- struct Stack *current = head;
- if (current == NULL)
- {
- printf("Stack is empty!\n");
- }
- else
- {
- int counter = 1;
- while (current != NULL)
- {
- if (current->data.missing > 6)
- {
- printf("#%d:\n", counter);
- show_item(current->data);
- counter++;
- }
- current = current->next;
- }
- if (counter == 1)
- {
- printf("Not found!\n");
- }
- }
- }
- void push(struct Group value)
- {
- if (stack_length == stack_size)
- {
- printf("Error: Stack overflow\n");
- return;
- }
- if (head == NULL)
- {
- head = malloc(sizeof(struct Stack));
- head->data = value;
- head->next = NULL;
- stack_length = 1;
- }
- else
- {
- struct Stack *new_element = malloc(sizeof(struct Stack));
- new_element->data = value;
- new_element->next = head;
- head = new_element;
- stack_length++;
- }
- }
- void pop()
- {
- if (head == NULL)
- {
- return;
- }
- if (head->next == NULL)
- {
- free(head);
- head = NULL;
- stack_length = 0;
- }
- else
- {
- struct Stack *buffer = head;
- head = head->next;
- free(buffer);
- stack_length--;
- }
- }
- struct Group peek()
- {
- if (head != NULL)
- {
- return head->data;
- }
- else
- {
- struct Group empty;
- empty.students = 0;
- empty.there = 0;
- empty.missing = 0;
- return empty;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement