Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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);
	}
	//list = longestString(&list_h);
	while(list)
		printf("%c ", *(char*)popLeanearList(&list));
}

this is my class:
C++
#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.
Posted

Nevermind, I simply forgot to restore the list's header. This can be locked, thanks.
 
Share this answer
 
PS: Please ignore the typo in 'leanear', should have been 'linear' instead.
 
Share this answer
 
Comments
Nelek 24-Nov-12 10:43am    
Please don't post solutions to add information, to ask something or to comment another user.
- To add information to your message, you can use the widget "Improve question" / "Improve solution" at the bottom of your text.
- To ask/answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900