Last active
August 29, 2015 14:08
-
-
Save LizardLeliel/1c6794ba4c10bcd16bae to your computer and use it in GitHub Desktop.
Me playing arround with linked lists and other stuff
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
// Note: I was a bit tired wrapping this up, so I could use a bit more code comments | |
// And I didn't clean up a few things to have "proper coding style", normally mixing | |
// variables beginning with capitals and what not | |
// Note2 (Wrotten after it was uploaded): it looks like I was dynamically adding nodes to the | |
// linked list wrong, they should be added at the head, not the tail. | |
// Also, I'll work on being consistent on my code style in next ones, I'll just leave things here | |
// as is | |
// Define Node struct | |
struct node { | |
int data = 0; | |
node* next = NULL; | |
node(){}; | |
node(int info, node* linked=NULL): data(info), next(linked) {}; | |
}; | |
// Function prototype | |
void deleteLinkedLists(node* deleteThis); | |
int main() { | |
// Declare LinkedList head & LinkedList | |
node* const LinkedListHead = new node(423, new node(191, new node(9003, new node(44)))); | |
node* LinkedList = LinkedListHead; | |
// Print each node's data | |
while (LinkedList != NULL) { | |
cout << LinkedList->data << ' '; | |
LinkedList = LinkedList->next; | |
} cout << endl; | |
// This is fun | |
cout << LinkedListHead->next->next->data << endl; | |
// Create a const array of five linkedlist pointers | |
node** const graph = new node* [5]; | |
// Initialize each linked list | |
for (int i=0; i != 5; ++i) { | |
graph[i] = new node(7); } | |
// Create some data for the linked list | |
graph[0]->next = new node(5, graph[3]); | |
graph[3]->data = 9999; | |
// Same as cout << graph[3]->data << endl; | |
cout << graph[0]->next->next->data << endl; | |
// Delete earlier linked list | |
LinkedList = LinkedListHead; | |
deleteLinkedLists(LinkedList); | |
// Delete graph | |
for (int i=0; i < 5; ++i) { | |
deleteLinkedLists(graph[i]); | |
} | |
// Pause | |
cout << "Press enter"; cin.ignore(); | |
} | |
// Utility function for deleting linked lists | |
void deleteLinkedLists(node* deleteThis) { | |
node* tracker; | |
do { | |
tracker = deleteThis->next; | |
delete deleteThis; | |
deleteThis = NULL; | |
deleteThis = tracker; | |
} while (deleteThis != NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment