Advertisement
xotohop

stack

Apr 5th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.     int number;
  8.     node *next;
  9. };
  10.  
  11. void push(node **top, int n)
  12. {
  13.     node *ptr = new node;
  14.     ptr->number = n;
  15.     if (top == NULL)
  16.         *top = ptr;
  17.     else
  18.     {
  19.         ptr->next = *top;
  20.         *top = ptr;
  21.     }
  22. }
  23.  
  24. int mult(node *top)
  25. {
  26.     node *ptr = top;
  27.     int res = 1;
  28.     while (ptr)
  29.     {
  30.         res = res * ptr->number;
  31.         ptr = ptr->next;
  32.     }
  33.     return res;
  34. }
  35.  
  36. void print(node *top)
  37. {
  38.     node *ptr = top;
  39.     while (ptr)
  40.     {
  41.         cout << ptr->number << " ";
  42.         ptr = ptr->next;
  43.     }
  44.     cout << endl;
  45. }
  46.  
  47. int main()
  48. {
  49.     node *top = NULL;
  50.     int elem_quan;
  51.     int enter_n;
  52.     cout << "enter number of elems: ";
  53.     cin >> elem_quan;
  54.     for (int i = 0; i < elem_quan; i++)
  55.     {
  56.         cin >> enter_n;
  57.         push(&top, enter_n);
  58.     }
  59.     print(top);
  60.     push(&top, mult(top));
  61.     print(top);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement