I'm writing a simple generic singly linked list implementation of a stack:
Code:
typedef struct LL_stack
{
struct LL_stack* next;
void* data;
} node;
My push fcn prototype:
Code:
void push(node** head, void* data);
I have a wrapper that user can pass in ptr to an array of specified type
Code:
void push_int_data(node** head, int* data)
{
push(head, data);
}
void push_double_data(node** head, double* data)
{
push(head, data);
}
void push_str_data(node** head, char* data)
{
push(head, data);
}
When I go use it, it's ugly for numeric data since I have to store all the literals in an array, but it's simple with C-strings, since a string literal is type: char*, so any hint how to design a better way for generic linked list (so I want it to hold int linked list, double/float linked list, string (C-strings) linked list)
Code:
int main()
{
node* int_head = 0;
node* float_head = 0;
node* str_head = 0;
int i_data[] = {1,2,3,4,5,6,7,8,9,10,11,12};//vs int* arr, you need to explicitly create size, but advantage is it can grow et shrink w/o effort b/c this is dynamic allocation
double d_data[] = {1.2,2.3,345.22,3.14,66.7,899.12,34.211,56.78,
99.8,100.67,12.111,11.87,90.87};
//int SLL
push_int_data(&int_head, &i_data[2]);//this is ugly way, how else if I'm using void* data in struct??? and I can't pass literals as args to ptr params
push_int_data(&int_head, &i_data[1]);
// //Floating number SLL
push_double_data(&float_head, &d_data[2]);
//C-string SLL
push_str_data(&str_head, "Hello");//this is intuitive to just type string
return 0;
}