Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     node *next = NULL;
  6.     int data;
  7. };
  8.  
  9. void add(int num, node **head)
  10. {
  11.     node *tmp = new node;
  12.     if (*head == NULL)
  13.     {
  14.         *head = tmp;
  15.         tmp->data = num;
  16.     }
  17.     else
  18.     {
  19.         tmp->data = num;
  20.         tmp->next = *head;
  21.         *head = tmp;
  22.     }
  23. }
  24.  
  25. void show(node *head)
  26. {
  27.     node *tmp = head;
  28.     while (tmp != NULL)
  29.     {
  30.         cout << "data: "<<tmp->data<<endl;
  31.         tmp = tmp->next;
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     setlocale(0, "");
  38.  
  39.     node *head = NULL;
  40.  
  41.     int n;
  42.     cout << "Количество элементов стека: ";
  43.     cin >> n;
  44.  
  45.     int num;
  46.     cout << "Вводите данные: ";
  47.     for (int i = 0; i < n; i++)
  48.     {
  49.         cout << "Информация: ";
  50.         cin >> num;
  51.         add(num, &head);
  52.     }
  53.     system("cls");
  54.    
  55.     show(head);
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement