Am I forgetting any cleanup/safety checks that I should be using? (Linked lists)
I was just wondering if in this code (which works) I am forgetting anything to clean up / protect myself from accessing garbage memory, etc. I wasn't sure in my functions if I should delete or if they're automatically destroyed at the end of the function.
Thank you for your time, in advance. :)
Code:
#include <iostream>
#include <ctime>
// Node prototype
struct node
{
int x;
node *next;
};
// Function prototypes
node* NewNode(node *end);
int TravList(node *root);
// Code
int main()
{
srand(time(NULL));
node *root, *link;
root = new node;
root->next = 0;
root->x = 5;
link = NewNode(root);
link = NewNode(link);
TravList(root);
std::cin.get();
return(0);
}
node* NewNode(node *root)
{
node *link;
link = root;
link->next = new node;
link = link->next;
link->next = 0;
link->x = (rand()%100 + 1);
root->next = link;
return(link);
}
int TravList(node *root)
{
node *link;
link = root;
while (link->next != 0)
{
std::cout << link->x << "\n";
link = link->next;
}
std::cout << link->x;
return(0);
}