Advertisement
Guest User

C++ Pointer Sample

a guest
Jul 10th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4.     int value = 0;
  5.     Node* previous = nullptr;
  6.     Node* next = nullptr;
  7. };
  8.  
  9. struct List {
  10.     List(){
  11.         head = new Node();
  12.         tail = head;
  13.     }
  14.     ~List(){
  15.         std::cout << "Destructor: " << std::endl;
  16.         Node* current = head;
  17.         while (current != nullptr){
  18.             Node* next = current->next;
  19.             std::cout << "Deleting: " << current->value << std::endl;
  20.             delete current;
  21.             current = next;
  22.         }
  23.     }
  24.     void Append(int value){
  25.         Node* previous = tail;
  26.         tail = new Node();
  27.         tail->value = value;
  28.         tail->previous = previous;
  29.         previous->next = tail;
  30.     }
  31.  
  32.     void Print(){
  33.         std::cout << "Printing the List:" << std::endl;
  34.         Node* current = head;
  35.         for (Node* current = head; current != nullptr;current = current->next){
  36.             std::cout << current->value << std::endl;
  37.         }
  38.     }
  39.     Node* tail;
  40.     Node* head;
  41. };
  42.  
  43. void main(){
  44.     int a = 5; //variable named a has a value of 5.
  45.     int *pA = &a; //pointer named pA is now referencing the memory address of a (we reference "a" with & to get the address).
  46.  
  47.     std::cout << *pA << ", " << a << std::endl; //output 5, 5
  48.     a = 6;
  49.     std::cout << *pA << ", " << a << std::endl; //output 6, 6
  50.     *pA = 7;
  51.     std::cout << *pA << ", " << a << std::endl; //output 7, 7
  52.  
  53.     int b = 20;
  54.     pA = &b;
  55.     std::cout << *pA << ", " << a << ", " << b << std::endl; //output 20, 7, 20
  56.     *pA = 25;
  57.     std::cout << *pA << ", " << a << ", " << b << std::endl; //output 25, 7, 25
  58.     pA = &a;
  59.     std::cout << *pA << ", " << a << ", " << b << std::endl; //output 7, 7, 25
  60.     *pA = 8;
  61.     std::cout << *pA << ", " << a << ", " << b << std::endl; //output 8, 8, 25
  62.     b = 30;
  63.     pA = &b;
  64.     std::cout << *pA << ", " << a << ", " << b << std::endl; //output 30, 8, 30
  65.  
  66.     char text[] { 'H', 'e', 'l', 'l', 'o', '\0' };
  67.     char* letter = text;
  68.  
  69.     for (char* letter = &text[0]; *letter != '\0';++letter){
  70.         std::cout << "[" << *letter << "]";
  71.     }
  72.     std::cout << std::endl;
  73.     {
  74.         List sampleList;
  75.         sampleList.Append(5);
  76.         sampleList.Append(6);
  77.         sampleList.Append(7);
  78.         sampleList.Append(8);
  79.         sampleList.Print();
  80.     }
  81.     system("PAUSE");
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement