Advertisement
xTheEc0

8. Dynamic memory management with pointers

May 12th, 2016
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <cstdlib>  // Standard General Utilities Library
  2. #include <cstdio>   // Input/Output operations (C)
  3. #include <iostream> // Input/Output stream objects
  4. #include <iomanip>  // Input/Output manipulator
  5.  
  6. using namespace std;
  7.  
  8. class studentas {
  9. private:
  10.  
  11. public:
  12.     long id;
  13.     double avg;
  14.     studentas *next;
  15. };
  16.  
  17. void AddFirst(studentas *first) {
  18.     cout << "Iveskite ID: "; cin >> first->id;
  19.     cout<< "Iveskite vidurki: "; cin >> first->avg;
  20.     first->next = NULL;
  21. }
  22.  
  23. void Add(studentas *last) {
  24.     while (last->next != NULL)
  25.         last = last->next;
  26.  
  27.     studentas *newlast = new studentas;
  28.  
  29.     cout << "\nIveskite ID: "; cin >> newlast->id;
  30.     cout<< "Iveskite vidurki: "; cin >> newlast->avg;
  31.  
  32.     newlast->next = NULL;
  33.     last->next = newlast;
  34. }
  35.  
  36. void print(studentas *last) {
  37.     while (last->next != NULL)
  38.     {
  39.         cout << "\nID: " << last->id << endl;
  40.         cout <<"Vidurkis: " << last->avg << endl;
  41.  
  42.         last = last->next;
  43.     }
  44.     cout << "\nID: " << last->id << endl;
  45.     cout <<"Vidurkis: " << last->avg << endl;
  46.  
  47. }
  48.  
  49. // Main program
  50. int main(int argc, char *argv[]) {
  51.  
  52.     studentas *first = new studentas;
  53.  
  54.     AddFirst(first);
  55.     for (int i = 0; i < 2; i++)
  56.         Add(first);
  57.  
  58.     print(first);
  59.  
  60.  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement