caxapexac

Untitled

Jun 4th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define MAX_LENGTH 500
  7.  
  8. /* РЕАЛИЗАЦИЯ СТЕКА НА ОСНОВЕ СПИСКА */
  9.  
  10. // Node of the list <=> element of stack
  11. typedef struct Node {
  12.     char data;  // in this case we save char symb
  13.     struct Node *next;
  14. }Node;
  15.  
  16. // Stack struct, saving top element
  17. typedef struct Stack {
  18.     struct Node* topElem;
  19. }Stack;
  20.  
  21. // Initialization
  22. void initStack(Stack* stack) {
  23.     stack->topElem = (Node*)malloc(1 * sizeof(Node));
  24.     stack->topElem = NULL;
  25. }
  26.  
  27. // Checking for empty
  28. int isEmpty(Stack* stack) {
  29.     return stack->topElem == NULL;
  30. }
  31.  
  32. /*
  33. To push an element into the stack, we create a new node,
  34. and point the stack pointer to the new node.
  35. */
  36. void push(Stack* stack, char data) {
  37.     Node* tmp = (Node*)malloc(1 * sizeof(Node));
  38.     if(!tmp) {
  39.         printf("Can't push!\n");
  40.         return;
  41.     }
  42.     tmp->data = data;
  43.     tmp->next = stack->topElem; // making last top element next for new top element
  44.     stack->topElem = tmp;  // making new top element
  45. }
  46.  
  47. /*
  48. To pop an element from the stack,
  49. we need to get the data of the element,
  50. point the stack pointer to the next element and remove it
  51. */
  52. char pop(Stack* stack) {
  53.     Node* tmp = stack->topElem;
  54.     char del_data = stack->topElem->data;
  55.     stack->topElem = stack->topElem->next;
  56.     free(tmp);
  57.     return del_data;
  58. }
  59.  
  60. // Getting value of top element
  61. char top(struct Stack* stack) {
  62.     return stack->topElem->data;
  63. }
  64.  
  65. /*
  66. To display the stack content,
  67. we traverse the stack elements from the first element to NULL
  68. */
  69. void display(Stack* stack) {
  70.     Node* current;
  71.     current = stack->topElem;
  72.     if(current) {
  73.         printf("Stack: ");
  74.         do {
  75.             printf("%c ", current->data);
  76.             current = current->next;
  77.         } while (current);
  78.         printf("\n");
  79.     }else {
  80.         printf("The Stack is empty!\n");
  81.     }
  82. }
  83.  
  84. // Get quantity of elements in stack
  85. int count(Stack* stack) {
  86.     int quant = 0;
  87.     Node* current;
  88.     current = stack->topElem;
  89.     if(current) {
  90.         do {
  91.             quant++;
  92.             current = current->next;
  93.         } while (current);
  94.     }else {
  95.         printf("The Stack is empty!\n");
  96.     }
  97.     return quant;
  98. }
  99.  
  100. int main() {
  101.     clock_t begin = clock();
  102.  
  103.     Stack* stack = (Stack*)malloc(1 * sizeof(Stack));
  104.     initStack(stack);
  105.  
  106.     clock_t end = clock();
  107.     // CLOCKS_PER_SEC is the number of clocks per second, so dividing by CLOCKS_PER_SEC yields a time in seconds
  108.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  109.  
  110.     printf("Время на инициализацию списка: %fl секунд\n", time_spent);
  111.  
  112.     begin = clock();
  113.  
  114.     for (int i = 0; i < 1000000; i++) {
  115.         push(stack, i);
  116.     }
  117.  
  118.     end = clock();
  119.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  120.     printf("Время на добавление в список 1000000 элементов: %lf секунд\n", time_spent);
  121.  
  122.     begin = clock();
  123.  
  124.     for (int i = 0; i < 1000000; i++) {
  125.         pop(stack);
  126.     }
  127.  
  128.     end = clock();
  129.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  130.     printf("Время на удаление из списка 1000000 элементов: %lf секунд\n", time_spent);
  131.  
  132.     return 0;
  133. }
Add Comment
Please, Sign In to add comment