Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. class lista {
  7. private:
  8.     int key;
  9.     lista* next;
  10. public:
  11.     void push(class lista* head, int x);
  12.     void wypisz(class lista* head);
  13. }*head = NULL, * tmp = NULL;   // tmp=ogon
  14.  
  15. void lista::push(class lista* head, int x)
  16. {
  17.     if (tmp == NULL)
  18.     {
  19.         head->key = x;
  20.         head->next = NULL;
  21.         tmp = head;
  22.     }
  23.     else {
  24.         head = (class lista*)new lista;
  25.         head->key = x;
  26.         head->next = NULL;
  27.         tmp->next = head;
  28.         tmp = head;
  29.     }
  30. }
  31.  
  32. void lista::wypisz(class lista* head) {
  33.  
  34.  
  35.     while (head)
  36.     {
  37.         cout << "Wartosc:  " << head->key << " Adres " << head; // wypisuje wartość i adres
  38.  
  39.  
  40.         cout << "\n\n";
  41.         head = head->next;
  42.     }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. int main() {
  49.  
  50.     int n;
  51.     cout << "Ile liczb \n\n";
  52.     cin >> n;
  53.  
  54.  
  55.     //head = (class lista*)new lista;
  56.  
  57.     lista* head = new lista;
  58.  
  59.     for (int i = 0; i < n; i++) {
  60.         head->push(head, i); // lub   (*head).push(head,x);
  61.  
  62.     }
  63.     cout << "\n\n\n";
  64.  
  65.     head->wypisz(head);
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement