-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_list_traversal.cpp
More file actions
46 lines (37 loc) · 906 Bytes
/
06_list_traversal.cpp
File metadata and controls
46 lines (37 loc) · 906 Bytes
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
44
45
46
#include <iostream>
#include <cstdlib>
struct Node {
int data;
Node* next;
};
int main(int argc, char* argv[]) {
if (argc < 2) return 1;
int n = std::atoi(argv[1]);
if (argc < n + 2) return 1;
Node* head = nullptr;
Node* tail = nullptr;
for (int i = 0; i < n; i++) {
Node* newNode = new Node;
newNode->data = std::atoi(argv[2 + i]);
newNode->next = nullptr;
if (head == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << "\n";
current = head;
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
return 0;
}