(10%) Determine
whether the syntax of the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
/* Linked List */
#include <iostream>
struct Node {
int value;
Node* next;
};
void print(Node* p) {
while (p) {
std::cout << p->value;
p = p->next;
if (p) std::cout << ' ';
}
std::cout << std::endl;
}
void insert(Node** aList, int v) {
Node* p = *aList;
if (p == NULL) {
*aList = p = new Node; // malloc(sizeof(Node))
p->value = v;
p->next = NULL;
} else {
while (p->next != NULL)
p = p->next;
p->next = new Node;
p->next->value = v;
p->next->next = NULL;
}
}
int main()
{
Node* head = NULL;
int a[] = { 1, 3, 5, 7, 2, 4, 6 };
for (size_t i=0; i<sizeof(a)/sizeof(a[0]); ++i)
insert(&head, a[i]);
print(head);
return 0;
}