Hello,
I'm trying to get the hang of pointers and decided to create a dynamic and generic struct of lists. now everything worked just fine with ints, but when I decided to give it a shot with chars it f***ed up a little bit.
This is my main function:
void main()
{
char *nPtr;
Leanear_Node_Ptr list, list_h;
initLeanearList(&list); initLeanearList(&list_h);
puts("enter list chars (last one '-')");
nPtr = (char*)malloc(sizeof(int));
scanf("%c", nPtr);
while(*nPtr!='-')
{
if(list)
{
insertAfterLeanearList(list,nPtr);
list = list->next;
}
else
{
pushLeanearList(&list,nPtr);
list_h = list;
}
nPtr = (char*)malloc(sizeof(int));
scanf("%c", nPtr);
}
while(list)
printf("%c ", *(char*)popLeanearList(&list));
}
this is my class:
#include "LeanearList.h"
void initLeanearList(Leanear_Node_Ptr *list)
{
*list = NULL;
}
void pushLeanearList(Leanear_Node_Ptr *list, void *value)
{
Leanear_Node_Ptr tmp = (Leanear_Node_Ptr)malloc(sizeof(Leanear_Node));
tmp->next = *list;
tmp->data = value;
*list = tmp;
}
void insertAfterLeanearList(Leanear_Node_Ptr node, void *value)
{
Leanear_Node_Ptr tmp = (Leanear_Node_Ptr)malloc(sizeof(Leanear_Node));
tmp->next = node->next;
tmp->data = value;
node->next = tmp;
}
void* popLeanearList(Leanear_Node_Ptr *list)
{
Leanear_Node_Ptr tmp = *list;
void* value = tmp->data;
*list = (*list)->next;
free(tmp);
return value;
}
void* removeAfterLeanearList(Leanear_Node_Ptr node)
{
Leanear_Node_Ptr tmp = node->next;
void* value = tmp->data;
node->next = tmp->next;
free(tmp);
return value;
}
void freeLeanearList(Leanear_Node_Ptr *list)
{
while(*list)
popLeanearList(list);
}
I didn't put the entire code because most of it is not needed at this point, I'm missing something with my casting lines at the main function, will be more than glad if anyone could tell me what the problem is.
Thank you very much.