Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SUCCESS 0
- #define MAX_ARRAY 100
- #define MAX_DYNAMIC 1000
- #define TRUE 1
- #define FULL_STACK -1
- #define INPUT_ERR -2
- #define EMPTY_STACK -3
- typedef struct
- {
- int *high_border;
- int *low_border;
- int *current_ptr;
- int type;
- } static_stack_descriptor_t;
- typedef struct dynamic_stack_descriptor
- {
- int value;
- struct dynamic_stack_descriptor* ex_ptr;
- } dynamic_stack_descriptor_t;
- int static_push(static_stack_descriptor_t *static_stack)
- {
- if (static_stack -> current_ptr == static_stack -> high_border)
- return FULL_STACK;
- int elem;
- printf("Please, input element: ");
- if(scanf("%i", &elem) != 1)
- return INPUT_ERR;
- static_stack -> current_ptr++;
- *(static_stack -> current_ptr) = elem;
- return SUCCESS;
- }
- int static_pop(static_stack_descriptor_t *static_stack)
- {
- if (static_stack -> current_ptr == static_stack -> low_border)
- return EMPTY_STACK;
- printf("Removed element: %i", *(static_stack -> current_ptr));
- (static_stack -> current_ptr)--;
- return SUCCESS;
- }
- int dynamic_push(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
- {
- int elem;
- printf("Please, input element: ");
- if(scanf("%i", &elem) != 1)
- return INPUT_ERR;
- dynamic_stack_descriptor_t *ptr = *dynamic_stack;
- (*dynamic_stack) = malloc(sizeof(dynamic_stack_descriptor_t));
- if (*dynamic_stack == NULL)
- return INPUT_ERR;
- for (int i = 0; i < *free_ptr_len; i++)
- if (*dynamic_stack == free_ptr[i])
- {
- for (int j = i; j < *free_ptr_len - 1; j++)
- free_ptr[j] = free_ptr[j + 1];
- (*free_ptr_len)--;
- break;
- }
- (*dynamic_stack) -> value = elem;
- (*dynamic_stack) -> ex_ptr = ptr;
- return SUCCESS;
- }
- void dynamic_remove_elem(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
- {
- dynamic_stack_descriptor_t *ptr = *dynamic_stack;
- *dynamic_stack = (*dynamic_stack) -> ex_ptr;
- free_ptr[*free_ptr_len] = ptr;
- (*free_ptr_len)++;
- free(ptr);
- return;
- }
- int dynamic_pop(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
- {
- printf("Removed element: %i", (*dynamic_stack) -> value);
- dynamic_remove_elem(dynamic_stack, free_ptr, free_ptr_len);
- return SUCCESS;
- }
- int main()
- {
- //freopen ("in.txt", "r", stdin);
- int choice;
- int array_static_stack[MAX_ARRAY];
- static_stack_descriptor_t static_stack;
- static_stack.low_border = (array_static_stack - 1);
- static_stack.high_border = (&array_static_stack[MAX_ARRAY - 1]);
- static_stack.current_ptr = static_stack.low_border;
- int dynamic_amount = 0;
- dynamic_stack_descriptor_t* dynamic_stack = NULL;
- dynamic_stack_descriptor_t *free_ptr[MAX_DYNAMIC];
- int free_ptr_len;
- while (TRUE)
- {
- printf("\n");
- printf(" _ __ ___ ___ _ __ _ _ \n");
- printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
- printf("| | | | | | __/ | | | |_| |\n");
- printf("|_| |_| |_|\\___|_| |_|\\____|\n");
- printf("\n\nATTENTION: Array stack has a limit in 100 elements!");
- printf("\nATTENTION: Dynamic stack has a ARTIFICIAL limit in 1000 elements!\n\n");
- printf("1: ARRAY STACK: Appending new element\n");
- printf("2: ARRAY STACK: Removing element\n");
- printf("3: LIST STACK: Appending new element\n");
- printf("4: LIST STACK: Removing element\n");
- printf("5: ARRAY STACK: Print the descending series sequences of integers in the reverse order\n");
- printf("6: LIST STACK: Print the descending series sequences of integers in the reverse order\n");
- printf("7: ARRAY STACK: Check memory status\n");
- printf("8: LIST STACK: Check memory status\n");
- printf("\n0: EXIT");
- while (TRUE)
- {
- fflush(stdin);
- printf("\n\nYour choice: ");
- if ((scanf("%i", &choice) == 1) && (choice >= 0))
- break;
- printf("Wrong choice!\n");
- system("pause");
- }
- if (!choice)
- {
- while(dynamic_amount != 0)
- {
- dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
- dynamic_amount--;
- }
- break;
- }
- if (choice == 1)
- {
- switch(static_push(&static_stack))
- {
- case FULL_STACK:
- printf("Stack if full!\n");
- system("pause");
- continue;
- case INPUT_ERR:
- printf("Input error!\n");
- system("pause");
- continue;
- case SUCCESS:
- printf("Successfull input!\n");
- system("pause");
- continue;
- }
- }
- if (choice == 2)
- {
- switch(static_pop(&static_stack))
- {
- case EMPTY_STACK:
- printf("Stack is empty!\n");
- system("pause");
- continue;
- case SUCCESS:
- printf("\n");
- system("pause");
- continue;
- }
- }
- if (choice == 3)
- {
- if (dynamic_amount == MAX_DYNAMIC)
- {
- printf("Stack if full!\n");
- system("pause");
- continue;
- }
- switch(dynamic_push(&dynamic_stack, free_ptr, &free_ptr_len))
- {
- case INPUT_ERR:
- printf("Input error!\n");
- system("pause");
- continue;
- case SUCCESS:
- dynamic_amount++;
- printf("Successfull input!\n");
- system("pause");
- continue;
- }
- }
- if (choice == 4)
- {
- if (dynamic_amount == 0)
- {
- printf("Stack is empty!\n");
- system("pause");
- continue;
- }
- switch(dynamic_pop(&dynamic_stack, free_ptr, &free_ptr_len))
- {
- case SUCCESS:
- dynamic_amount--;
- printf("\n");
- system("pause");
- continue;
- }
- }
- if (choice == 5)
- {
- if (static_stack.current_ptr == static_stack.low_border)
- {
- printf("Empty stack!\n");
- system("pause");
- continue;
- }
- int is_printed = 0;
- int* end = static_stack.current_ptr;
- long int mem = (static_stack.current_ptr - static_stack.low_border) * sizeof(static_stack_descriptor_t);
- int in_seq = 0;
- int is_n = 1;
- clock_t time = clock();
- for (; static_stack.current_ptr != static_stack.low_border; static_stack.current_ptr--)
- {
- in_seq = 0;
- if (static_stack.current_ptr - 1 != static_stack.low_border &&
- *static_stack.current_ptr > *(static_stack.current_ptr - 1))
- in_seq = 1;
- else if (static_stack.current_ptr != end &&
- *static_stack.current_ptr < *(static_stack.current_ptr + 1))
- in_seq = 1;
- else if (static_stack.current_ptr - 1 != static_stack.low_border &&
- static_stack.current_ptr != end &&
- *static_stack.current_ptr > *(static_stack.current_ptr - 1))
- in_seq = 1;
- if (static_stack.current_ptr - 1 != static_stack.low_border &&
- static_stack.current_ptr != end &&
- *static_stack.current_ptr >= *(static_stack.current_ptr + 1) &&
- *static_stack.current_ptr > *(static_stack.current_ptr - 1))
- printf("\n");
- if (in_seq)
- {
- is_printed = 1;
- printf("%i ", *static_stack.current_ptr);
- is_n = 0;
- }
- else if (!is_n)
- {
- is_n = 1;
- printf("\n");
- }
- }
- if (!is_printed)
- printf("\nThere are no descending series in stack!");
- printf("\n");
- time = clock() - time;
- printf("Time spended in ticks: %li\n", (long int) time);
- printf("Memoty usage in bytes: %li\n", mem);
- system("pause");
- }
- if (choice == 6)
- {
- if (dynamic_amount == 0)
- {
- printf("Empty stack!\n");
- system("pause");
- continue;
- }
- int is_printed = 0;
- //dynamic_stack_descriptor_t *end = dynamic_stack;
- int in_seq = 0;
- int is_n = 0;
- long int mem = dynamic_amount * sizeof(dynamic_stack_descriptor_t);
- clock_t time = clock();
- int past = dynamic_stack -> value;
- int current = dynamic_stack -> value;
- dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
- int next = dynamic_stack -> value;
- for (; dynamic_stack != NULL;)
- {
- next = dynamic_stack -> value;
- in_seq = 0;
- if (current > next)
- in_seq = 1;
- else if (current < past)
- in_seq = 1;
- if(current >= past && current > next)
- printf("\n");
- if (in_seq)
- {
- is_printed = 1;
- printf("%i ", current);
- is_n = 0;
- }
- else if (!is_n)
- {
- is_n = 1;
- printf("\n");
- }
- past = current;
- current = next;
- dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
- }
- if(current < past)
- printf("%i", current);
- if (!is_printed)
- printf("\nThere are no descending series in stack!");
- printf("\n");
- time = clock() - time;
- printf("Time spended in ticks: %li\n", (long int) time);
- printf("Memoty usage in bytes: %li\n", mem);
- dynamic_amount = 0;
- system("pause");
- }
- if (choice == 7)
- {
- printf("Current fullness of array stack: %li / %i", (long int)(static_stack.current_ptr - static_stack.low_border), MAX_ARRAY);
- }
- if (choice == 8)
- {
- if (free_ptr_len == 0)
- printf("There are no free adresses!\n");
- else
- {
- printf("List of free pinters:\n");
- for(int i = 0; i < free_ptr_len; i++)
- {
- printf("%p\n", free_ptr[i]);
- }
- }
- printf("\n");
- system("pause");
- }
- }
- //fclose (stdin);
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement