In: Computer Science
Complete the C++ code
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; void print_cells(Cell *cell); int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand(time(NULL)); for (int i = 0; i < MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the c = new Cell; // cell value (val) to rand(). Set the next pointer to the HEAD and c->val = rand(); c->next = HEAD; // then update HEAD. HEAD = c; } print_cells(HEAD); } void print_cells(Cell *cell) { while (cell != NULL) { cout << cell->val << " "; cell = cell->next; } cout << endl; }