Advertisement
r1411

Двойной стек

Jun 12th, 2021 (edited)
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define max 6
  5.  
  6. struct stek {
  7.     int top1, top2;
  8.     int s[max];
  9. };
  10.  
  11. void init(stek* st) {
  12.     st->top1 = -1; st->top2 = max;
  13. }
  14.  
  15. void inSt1(stek* st, int x) {
  16.     if (st->top2 - st->top1 == 1)
  17.         cout << "пепеполнение";
  18.     else
  19.         st->s[++st->top1] = x; 
  20. }
  21.  
  22. void inSt2(stek* st, int x) {
  23.     if (st->top2 - st->top1 == 1)
  24.         cout << "пепеполнение";
  25.     else
  26.         st->s[--st->top2] = x;
  27. }
  28.  
  29. int outSt1(stek* st) {
  30.     if (st->top1 == -1) {
  31.         cout << "ты че доставать собрался";
  32.         return 0;
  33.     }
  34.     else {
  35.         return st->s[st->top1--];
  36.     }
  37. }
  38.  
  39. int outSt2(stek* st) {
  40.     if (st->top2 == max) {
  41.         cout << "ты че доставать собрался";
  42.         return 0;
  43.     }
  44.     else {
  45.         return st->s[st->top2++];
  46.     }
  47. }
  48.  
  49. void main() {
  50.     stek* st = new(stek);
  51.     init(st);
  52.  
  53.     inSt1(st, 5);
  54.     inSt1(st, 6);
  55.     inSt1(st, 7);
  56.     inSt2(st, 10);
  57.     inSt2(st, 11);
  58.     inSt2(st, 12);
  59.  
  60.     cout << outSt1(st) << endl;
  61.     cout << outSt2(st) << endl;
  62.     cout << outSt1(st) << endl;
  63.     cout << outSt2(st) << endl;
  64.     cout << outSt1(st) << endl;
  65.     cout << outSt2(st) << endl;
  66.  
  67.     cout << "---------------------" << endl;
  68.  
  69.  
  70.     inSt1(st, 1);
  71.     inSt1(st, 2);
  72.     inSt1(st, 3);
  73.     inSt1(st, 4);
  74.     inSt1(st, 5);
  75.     inSt1(st, 6);
  76.  
  77.     cout << outSt1(st) << endl;
  78.     cout << outSt1(st) << endl;
  79.     cout << outSt1(st) << endl;
  80.     cout << outSt1(st) << endl;
  81.     cout << outSt1(st) << endl;
  82.     cout << outSt1(st) << endl;
  83.  
  84.     cout << "---------------------" << endl;
  85.  
  86.     inSt2(st, 10);
  87.     inSt2(st, 11);
  88.     inSt2(st, 12);
  89.     inSt2(st, 13);
  90.     inSt2(st, 14);
  91.     inSt2(st, 15);
  92.  
  93.     cout << outSt2(st) << endl;
  94.     cout << outSt2(st) << endl;
  95.     cout << outSt2(st) << endl;
  96.     cout << outSt2(st) << endl;
  97.     cout << outSt2(st) << endl;
  98.     cout << outSt2(st) << endl;
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement