Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include "string"
  3. #include "math.h"
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     node *next = NULL;
  9.     int data;
  10. };
  11.  
  12. void add(int x, node **head)
  13. {
  14.     node *tmp = new node;
  15.     if (*head == NULL)
  16.     {
  17.         *head = tmp;
  18.         tmp->data = x;
  19.     }
  20.     else
  21.     {
  22.         node *top = *head;
  23.         top->next = tmp;
  24.         tmp->data = x;
  25.         *head = top;
  26.        
  27.     }
  28. }
  29.  
  30. void show(node *head)
  31. {
  32.     node *tmp = head;
  33.     while (tmp!=NULL)
  34.     {
  35.         cout << "Data: " << tmp->data << endl;
  36.         tmp = tmp->prev;
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.     setlocale(LC_ALL, "russian");
  43.     node *head = NULL;
  44.     int n;
  45.     cout << "n = ";
  46.     cin >> n;
  47.     int xxx;
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         cout << "Enter data: ";
  51.         cin >> xxx;
  52.         add(xxx, &head);
  53.     }
  54.     system("cls");
  55.     cout << "clear" << endl;
  56.  
  57.     show(head);
  58.  
  59.     system("pause");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement