cavaman

6-7.2

Jun 10th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <forward_list>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. bool clear(stack<int>& cstk);
  9. void print(stack<int> pstk);
  10. void isEmpty(stack<int>& estk);
  11. bool paste(stack<int>& instk);
  12. void extract(stack<int>& fromstk, int& el);
  13.  
  14. int main()
  15. {
  16.     setlocale(LC_ALL, "rus");
  17.     forward_list<int> stklist;
  18.     stack<int> stk;
  19.     int pos;
  20.     const int size = 5;
  21.     cout << "Ввод стека:\n";
  22.     for (int i = 0; i < size; i++)
  23.     {
  24.         int el;
  25.         cin >> el;
  26.         stk.push(el);
  27.     }
  28.     stack<int> stk2 = stk;
  29.     print(stk);
  30.  
  31.     vector<int> vec;
  32.     for (int i = 0; i < size; i++)
  33.     {
  34.         if (i == 0)
  35.             pos = i;
  36.         vec.push_back(stk.top());
  37.         stk.pop();
  38.     }
  39.     cout << "\nа) ";
  40.     for (int i : vec)
  41.         cout << i << " ";
  42.     cout << "\nИндекс компоненты массива, занятой последним элементом стека = " << pos;
  43.  
  44.     for (int i = stk2.size(); i > 0; i--)
  45.     {
  46.         int temp = stk2.top();
  47.         stk2.pop();
  48.         stklist.push_front(temp);
  49.     }
  50.     cout << "\nб) ";
  51.     for (int i : stklist)
  52.         cout << i << " ";
  53. }
  54.  
  55. bool clear(stack<int>& cstk)
  56. {
  57.     if (cstk.empty())
  58.         return true;
  59.     while (!cstk.empty())
  60.     {
  61.         cstk.top();
  62.         cstk.pop();
  63.     }
  64.     return true;
  65. }
  66.  
  67. void print(stack<int> pstk)
  68. {
  69.     while (!pstk.empty())
  70.     {
  71.         int el = pstk.top();
  72.         pstk.pop();
  73.         cout << el << " ";
  74.     }
  75. }
  76.  
  77. void isEmpty(stack<int>& estk)
  78. {
  79.     if (estk.empty())
  80.         cout << "\nСтэк пустой";
  81.     else cout << "\nСтэк не пустой";
  82. }
  83.  
  84. bool paste(stack<int>& instk)
  85. {
  86.     cout << "\nЭлемент для вставки: ";
  87.     int el;
  88.     cin >> el;
  89.     instk.push(el);
  90.     return true;
  91. }
  92.  
  93. void extract(stack<int>& fromstk, int& el)
  94. {
  95.     el = fromstk.top();
  96.     fromstk.pop();
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment