Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Last active August 29, 2015 14:09
Show Gist options
  • Save LizardLeliel/24b2705522d201d21c01 to your computer and use it in GitHub Desktop.
Save LizardLeliel/24b2705522d201d21c01 to your computer and use it in GitHub Desktop.
A struct which can be used for linked lists which holds any data
#include <iostream>
#include <cstdlib>
struct dynamicNode {
void* data;
int type;
dynamicNode* next;
};
// I've uploaded this so I may self-reference it later
using namespace std;
int main() {
dynamicNode* first = (dynamicNode*)malloc(sizeof(dynamicNode));
first->next = (dynamicNode*)malloc(sizeof(dynamicNode));
int m = 4;
float q = 4.5;
first->data = (void*)(&m);
first->next->data = (void*)(&q);
cout << (*(int*)first->data + *(float*)first->next->data) << endl;
int* a = (int*)malloc(sizeof(int));
float* b = (float*)malloc(sizeof(float));
first->next->next = (dynamicNode*)malloc(sizeof(dynamicNode));
first->next->next->next = (dynamicNode*)malloc(sizeof(dynamicNode));
first->next->next->data = (void*)a;
first->next->next->next->data = (void*)b;
*a = 5;
*b = 22.4;
cout << (*(int*)first->next->next->data
+ *(float*)first->next->next->next->data) << endl;
// Assigning type
first->type = 0;
first->next->type = 1;
first->next->next->type = 0;
first->next->next->next->type = 1;
first->next->next->next->next = 0;
dynamicNode* tracker = first;
double o = 0;
while (tracker) {
if (tracker->type == 0) {
o += *(int*)tracker->data;
}
else if (tracker->type == 1) {
o += *(float*)tracker->data;
}
tracker = tracker->next;
}
cout << o << endl;
cin.ignore();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment