SHARE
TWEET

7

a guest Dec 3rd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. typedef struct _node* Position;
  7. typedef struct _node
  8. {
  9.     int value;
  10.     Position next;
  11. }Node;
  12.  
  13. int PushStack(Position, int);
  14. int PopStack(Position);
  15. int AddFirst(Position, Position);
  16. Position RandomValue(int);
  17. void Print(Position);
  18. int ArrayLength(Position);
  19. void DeleteLast(Position);
  20.  
  21. int main(int argc, char** argv)
  22. {
  23.     Node stack;
  24.     stack.next = NULL;
  25.     int maxEl = 0;
  26.     int choice = 0;
  27.     srand((unsigned)time(NULL));
  28.  
  29.  
  30.     printf("Circular stack maximum elements: ");
  31.     scanf_s(" %d", &maxEl);
  32.     printf("\r\n");
  33.  
  34.     while (choice != 4) {
  35.         printf("1: push stack\r\n2: pop stack\r\n3: print list\r\n4: exit \r\n");
  36.         printf("Your choice: ");
  37.         scanf_s(" %d", &choice);
  38.         printf("\r\n");
  39.  
  40.         switch (choice)
  41.         {
  42.         case 1:
  43.             PushStack(&stack, maxEl);
  44.             break;
  45.         case 2:
  46.             PopStack(&stack);
  47.             break;
  48.         case 3:
  49.             Print(stack.next);
  50.             break;
  51.         case 4:
  52.             printf("\r\nExiting..."); choice = 4;
  53.             break;
  54.         default:
  55.             printf("\r\nWrong choice, try again. \r\n");
  56.         }
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
  62. int PushStack(Position head, int max)
  63. {
  64.     int target = 0;
  65.     target = rand() % (100 - 10 + 1) + 10;
  66.     if (ArrayLength(head) < max)
  67.         AddFirst(head, RandomValue(target));
  68.     else
  69.     {
  70.         AddFirst(head, RandomValue(target));
  71.         DeleteLast(head);
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
  77. int PopStack(Position head)
  78. {
  79.     head->next = head->next->next;
  80.     return 0;
  81. }
  82.  
  83. int AddFirst(Position head, Position newEl)
  84. {
  85.     newEl->next = head->next;
  86.     head->next = newEl;
  87.  
  88.     return 0;
  89. }
  90.  
  91. Position RandomValue(int number)
  92. {
  93.     Position newNode;
  94.     newNode = (Position)malloc(sizeof(Node));
  95.     if (newNode == NULL)
  96.         return NULL;
  97.     newNode->value = number;
  98.     newNode->next = NULL;
  99.  
  100.     return newNode;
  101. }
  102.  
  103. void Print(Position p)
  104. {
  105.     while (p != NULL)
  106.     {
  107.         printf("%d \t", p->value);
  108.         p = p->next;
  109.     }
  110.     printf("\r\n\r\n");
  111. }
  112.  
  113. int ArrayLength(Position head)
  114. {
  115.     int counter = 0;
  116.     while (head->next != NULL)
  117.     {
  118.         counter++;
  119.         head = head->next;
  120.     }
  121.     return counter;
  122. }
  123.  
  124. void DeleteLast(Position head)
  125. {
  126.     while (head->next->next != NULL)
  127.         head = head->next;
  128.     head->next = NULL;
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top