Advertisement
Guest User

Untitled

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