Advertisement
MaksNew

C_STACK_LABA

Oct 21st, 2021
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <windows.h>
  4. struct node
  5. {
  6.     int data;
  7.     struct node* next;
  8. };
  9. typedef struct node* pnode;
  10.  
  11. void stack_pop(pnode last)
  12. {
  13.     if (last == NULL) {
  14.         puts("Стек пуст!");
  15.         return;
  16.     }
  17.     pnode buf = last;
  18.     last = last->next;
  19.     free(buf);
  20. }
  21.  
  22. void printStack(pnode last)
  23. {
  24.     if (last == NULL)
  25.     {
  26.         puts("Стек пуст!");
  27.         return;
  28.     }
  29.     pnode current = last;
  30.     while(current->next != NULL)
  31.     {
  32.         printf("%d ", current->data);
  33.         current = current->next;
  34.     }
  35. }
  36.  
  37. void stack_push(pnode last, int element)
  38. {
  39.     if(last == NULL)
  40.     {
  41.         last = malloc(0);
  42.         last->data = element;
  43.         last->next = NULL;
  44.         return;
  45.     }
  46.     pnode newNode = malloc(0);
  47.     newNode->data = element;
  48.     newNode->next = last;
  49.     last = newNode;
  50. }
  51.  
  52. int readChoise()
  53. {
  54.     int IsNotCorrect = 1;
  55.     int choise = 0;
  56.     do
  57.     {
  58.         puts("Введите пункт меню:");
  59.         IsNotCorrect = scanf("%d", &choise);// 1 if ok; 0 if bitch
  60.         fflush(stdin);
  61.         if ((IsNotCorrect) && ((choise < 1) || (choise > 4)))
  62.         {
  63.             puts("Введите число от 1 до 4!");
  64.             IsNotCorrect = 0;
  65.         }
  66.     }while(IsNotCorrect == 0);
  67.     return choise;
  68. }
  69.  
  70. int readElement()
  71. {
  72.     int IsNotCorrect = 1;
  73.     int el = 0;
  74.     do
  75.     {
  76.         puts("Введите элемент:");
  77.         IsNotCorrect = scanf("%d", &el);// 1 if ok; 0 if bitch
  78.         fflush(stdin);
  79.         if ((IsNotCorrect) && ((el < -50) || (el > 50)))
  80.         {
  81.             puts("Введите число от -50 до 50!");
  82.             IsNotCorrect = 0;
  83.         }
  84.     }while(IsNotCorrect == 0);
  85.     return el;
  86. }
  87.  
  88. void showMenu()
  89. {
  90.     puts("Выберите один из пунктов меню для продолжения:");
  91.     puts("1. Вставить элемент в стек.");
  92.     puts("2. Удалить элемент со стека.");
  93.     puts("3. Вывести элементы стека.");
  94.     puts("4. Выход.");
  95.     pnode last = NULL;
  96.         switch (readChoise()) {
  97.             case 1:
  98.                 stack_push(last, readElement());
  99.                 break;
  100.             case 2:
  101.                 stack_pop(last);
  102.                 break;
  103.             case 3:
  104.                 printStack(last);
  105.                 break;
  106.             case 4:
  107.                 return;
  108.         }
  109.         showMenu();
  110. }
  111.  
  112. void f(pnode *p)
  113. {
  114.     *p = malloc(sizeof(pnode));
  115.     p->data = 228;
  116. }
  117.  
  118. int main() {
  119.     SetConsoleCP(1251);
  120.     SetConsoleOutputCP(1251);
  121.     setlocale(LC_ALL, "Rus");
  122.  
  123.     pnode p;
  124.     f(&p);
  125.     printf("%d", p->data);
  126.  
  127.     //showMenu();
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement