So, does this code look good then?
Programming_Exercises.h:
Code:
struct node {
string store; //for storing something in the list
node* next;
int one;
int two;
int three;
int random;
};
Programming_Exercises.cpp:
Code:
#include <iostream>
#include <string.h>
#include "Programming_Exercises.h"
using namespace std;
int main() {
node* root; //This will be the unchanging first node
node* conductor; //This will point to each node as it traverses the list
root = new node;
//Assisn values:
root->next = 0; //otherwise it wont work well
root->store = "This program tests a linked list.\n\nYes, it tests a linked list.";
root->one = 1;
root->two = 2;
root->three = 3;
root->random = 4;
conductor = root; //The conductor points at the first node
if (conductor != 0 ) {
while (conductor->next !=0) {
cout<< conductor->store;
cout<< conductor->one <<", " << two << ", " << three << ", " << random <<endl;
conductor->random = 5;
cout<< conductor->random;
conductor = conductor->next;
}
cout<< conductor->store;
}
conductor->next = new node; //creates a node at the end of the list
conductor = conductor->next; //Points to that node
conductor->next = 0; //Prevents it from going further
conductor->store = "Conductor just accessed the list, changed the contents of the string...\n\nTest completed \"
"successfully. Thank you for trying this program";
cout<< conductor->store;
delete[] root;
return 0;
}
I would have used delete on the conductor->next too, since it also uses new, but I didn't think I needed to, since it is a member variable of the node struct, and the object of that node struct (root) was deleted. Is that fine, or do I need to explicitly delete conductor->next too?