Advertisement
palenda21

Lab13A

May 20th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct comp
  6.  
  7. {
  8.  
  9.     int data;
  10.  
  11.     comp* next;
  12.  
  13. };
  14.  
  15. comp* addstack(comp* top, int inf)
  16.  
  17. {
  18.  
  19.     comp* spt = new comp;
  20.  
  21.     spt->data = inf;
  22.  
  23.     spt->next = top;
  24.  
  25.     return spt;
  26.  
  27. }
  28.  
  29. int chek(comp* top) {
  30.     comp* tmp = top;
  31.     while (tmp) {
  32.         if (tmp->data < 0) return 1;
  33.         tmp = tmp->next;
  34.     }
  35.     return 0;
  36. }
  37.  
  38. void s_print(comp* top) {
  39.  
  40.     comp* tmp = top;
  41.  
  42.     while (tmp) {
  43.  
  44.         cout << tmp->data << ' ';
  45.  
  46.         tmp = tmp->next;
  47.  
  48.     }
  49.  
  50. }
  51.  
  52. void delstack(comp* top) {
  53.  
  54.     comp* tmp; int data;
  55.  
  56.     while (top) {
  57.  
  58.         tmp = top;
  59.  
  60.         data = top->data;
  61.  
  62.         cout << data << ' ';
  63.  
  64.         top = top->next;
  65.  
  66.         delete tmp;
  67.  
  68.     }
  69.  
  70. }
  71.  
  72. int main() {
  73.  
  74.     setlocale(LC_ALL, "RU");
  75.  
  76.     comp* top = NULL;
  77.  
  78.     int n, value;
  79.  
  80.     cout << "Введите количество элементов стека: ";
  81.  
  82.     cin >> n;
  83.  
  84.     cout << "Введите элементы стека: ";
  85.  
  86.     for (int i = 0; i < n; i++) {
  87.  
  88.         cin >> value;
  89.  
  90.         top = addstack(top, value);
  91.  
  92.     }
  93.  
  94.     s_print(top);
  95.  
  96.     chek(top);
  97.     int e = chek(top);
  98.     if (e == 1) cout << "\nесть отрицательные числа";
  99.     else cout << "\nнет отрицательных чисел";
  100.     cout << endl;
  101.  
  102.     delstack(top);
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement