Your primary problem is here:
while(ch=1)
{
node *head;
head
is local to the while
block. After creating a new list on case 1
, that variable will go out of scope and disappear before the loop comes round to give you a chance to print it. So you're just passing uninitialized memory to printlist()
. You need:
node * head;
while (ch=1) {
There are numerous other problems with your code:
ch=1
means the while loop will never end, you don't seem to ever changech
anyway.- you
malloc()
memory forhead
, but then overwrite the value ofhead
when you callcreatelist()
and lose that memory, without having ever used it. node * current
is never used- don't cast the return from
malloc()