-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-pointer-ex.cpp
43 lines (31 loc) · 1014 Bytes
/
05-pointer-ex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
struct node {
int val;
node *next;
};
int main() {
node *n;
n = NULL;
n = new node;
n->val = 1;
node *current = NULL;
current = new node;
current->val = 5;
current->next = NULL;
n->next = current;
cout << "First node value : " << n->val << endl;
cout << "Second node value: " << n->next->val << endl;
cout << "Second node (using current): " << current->val << endl;
// creat a third node (note that we're re-using current)
current = new node;
current->val = 3;
current->next = NULL;
n->next->next = current;
// n->next gives second node and we set the next of that node to current
// next next (need two of "next" to get to 3)
// { 1 }-->{ 5 }-->{ 3 }
cout << "Third node value: " << n->next->next->val << endl;
cout << "Third node (using current): " << current->val << endl;
return 0;
}