Last active
August 29, 2015 14:07
-
-
Save clandry94/5919822a0e90fa3071cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ---------- ---------- | |
* - Data - - Data - | |
* ---------- ---------- | |
* - Pointer- - - -> - Pointer- | |
---------- ---------- */ | |
#include <iostream> | |
struct node { | |
int data; | |
node *next; | |
}; | |
static node * traverse(node *root, node *traversal) | |
{ | |
if (traversal != NULL) { //ensures something to begin with | |
while ( traversal->next != NULL ) { //goes on until it does find a null | |
std::cout << traversal->data << "\n"; | |
traversal = traversal->next; | |
} | |
std::cout << traversal->data << "\n"; | |
} | |
return traversal; | |
} | |
void insert(node *traversal) | |
{ | |
int x = 1; | |
while(x <= 20) { | |
traversal->next = new node; //create new node at the end of the list | |
traversal = traversal->next; //points to that node | |
traversal->next = NULL; //stops from going any further | |
traversal->data = x; | |
x++; | |
} | |
} | |
int main() | |
{ | |
node *root; // This will be the unchanging first node | |
// Think of like a train engine | |
root = new node; // Now root points to a node struct/class | |
root->next = NULL; // The node root points to has its next pointer | |
// set equal to a null pointer | |
root->data = 0; // By using the -> operator, you can modify the node | |
// a pointer (root in this case) points to. | |
node *traversal; // Will point to the node as it traverses the list. | |
//think of this like the conductor of a train | |
traversal = root; //set the traversal | |
insert(traversal); | |
traverse(root, traversal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment