Code:
//: S04:LinkedList.cpp
#include <iostream>
#include <cassert>
using namespace std;
struct Node {
int val;
Node* next;
};
void create(Node* head, int howMany) {
assert(head);
Node* p1 = head;
for (int i = 1; i < howMany; ++i) {
Node* p2 = new Node;
p2->val = i;
p1->next = p2;
p1 = p2;
}
p1->next = 0;
}
void display(Node* node) {
assert(node);
while (node) {
cout << node->val << " (at " << (long)node << ")\n";
node = node->next;
}
}
void cleanup(Node* node) {
if (node) {
cleanup(node->next);
cout << "deleting Node at " << (long)node << endl;
delete node;
}
}
int main() {
Node* head = new Node;
head->val = 0;
create(head, 10);
display(head);
cout << endl << "sizeof(head)== " << sizeof(head) << endl;
cleanup(head);
getchar();
}
//: S04:LinkedList.cpp
#include <iostream>
#include <cassert>
using namespace std;
struct Node {
int val;
Node* next;
};
void create(Node* head, int howMany) {
assert(head);
Node* p1 = head;
for (int i = 1; i < howMany; ++i) {
Node* p2 = new Node;
p2->val = i;
p1->next = p2;
p1 = p2;
}
p1->next = 0;
}
void display(Node* node) {
assert(node);
while (node) {
cout << node->val << " (at " << (long)node << ")\n";
node = node->next;
}
}
void cleanup(Node* node) {
if (node) {
cleanup(node->next);
cout << "deleting Node at " << (long)node << endl;
delete node;
}
}
int main() {
Node* head = new Node;
head->val = 0;
create(head, 10);
display(head);
cout << endl << "sizeof(head)== " << sizeof(head) << endl;
cleanup(head);
getchar();
}
0 (at 4007016)
1 (at 4007080)
2 (at 4007096)
3 (at 4007112)
4 (at 4007128)
5 (at 4007144)
6 (at 4007160)
7 (at 4007176)
8 (at 4007192)
9 (at 4007208)
sizeof(head)== 4
deleting Node at 4007208
deleting Node at 4007192
deleting Node at 4007176
deleting Node at 4007160
deleting Node at 4007144
deleting Node at 4007128
deleting Node at 4007112
deleting Node at 4007096
deleting Node at 4007080
deleting Node at 4007016
Ovde su 3 nejasnoce.
1. Kako je velicina node-a 4 bajta, kada pored integera(val) mora negde da se smesti i adresa(next) ?
2. Ako je vec velicina 4, odakle onda razlika memorijskih adresa node-ova 16 ?
3. Ako je vec ta razlika 16, odakle onda razlika izmedju head-a i drugog node-a 64 ?
TX