Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int value = 0;
- Node* previous = nullptr;
- Node* next = nullptr;
- };
- struct List {
- List(){
- head = new Node();
- tail = head;
- }
- ~List(){
- std::cout << "Destructor: " << std::endl;
- Node* current = head;
- while (current != nullptr){
- Node* next = current->next;
- std::cout << "Deleting: " << current->value << std::endl;
- delete current;
- current = next;
- }
- }
- void Append(int value){
- Node* previous = tail;
- tail = new Node();
- tail->value = value;
- tail->previous = previous;
- previous->next = tail;
- }
- void Print(){
- std::cout << "Printing the List:" << std::endl;
- Node* current = head;
- for (Node* current = head; current != nullptr;current = current->next){
- std::cout << current->value << std::endl;
- }
- }
- Node* tail;
- Node* head;
- };
- void main(){
- int a = 5; //variable named a has a value of 5.
- int *pA = &a; //pointer named pA is now referencing the memory address of a (we reference "a" with & to get the address).
- std::cout << *pA << ", " << a << std::endl; //output 5, 5
- a = 6;
- std::cout << *pA << ", " << a << std::endl; //output 6, 6
- *pA = 7;
- std::cout << *pA << ", " << a << std::endl; //output 7, 7
- int b = 20;
- pA = &b;
- std::cout << *pA << ", " << a << ", " << b << std::endl; //output 20, 7, 20
- *pA = 25;
- std::cout << *pA << ", " << a << ", " << b << std::endl; //output 25, 7, 25
- pA = &a;
- std::cout << *pA << ", " << a << ", " << b << std::endl; //output 7, 7, 25
- *pA = 8;
- std::cout << *pA << ", " << a << ", " << b << std::endl; //output 8, 8, 25
- b = 30;
- pA = &b;
- std::cout << *pA << ", " << a << ", " << b << std::endl; //output 30, 8, 30
- char text[] { 'H', 'e', 'l', 'l', 'o', '\0' };
- char* letter = text;
- for (char* letter = &text[0]; *letter != '\0';++letter){
- std::cout << "[" << *letter << "]";
- }
- std::cout << std::endl;
- {
- List sampleList;
- sampleList.Append(5);
- sampleList.Append(6);
- sampleList.Append(7);
- sampleList.Append(8);
- sampleList.Print();
- }
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement