linked list help

my code is:
#include<stdio.h>
#include<iostream.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr i;
nodeptr q;
nodeptr p;
nodeptr *plist;

nodeptr getnode(void)
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;
}
void freenode(nodeptr p)
{
free(p);
}





int main()
{
int i;
nodeptr *k;
int a;
int r;
nodeptr end;

p=getnode();
q=getnode();


for(i=0; i<3; i++)
{
q=*k;
cout<<"enter value";
cin>>r;

p->info=r;
q->next=p;
q=q->next;
}

for(q=*k; q->next!=NULL; q=q->next)
cout<<(q->next)->info;

return 0;
}

here when i input three nos
in the output the last no is only shown
can yu suggest me what is wrong in the code
please help


Please use code tags. :)
1
2
3
    nodeptr *k;        // k points to some random place in memory.
    // ...
        q = *k ;  // Undefined behavior.  k points to some random place in memory. 


Chances are good your compiler warns you about using an uninitialized variable. Pay attention to your compiler warnings.

Maybe not though, since your code suggests you're using a pre-standard C++ compiler. In which case, upgrade.
Get out of the habit of using global variables. It may not seem like a bad thing now, and it probably isn't in a small program like this, but trust me, it's not a good thing to use. Better to have getters and setters than have anything in global scope.
Topic archived. No new replies allowed.