using functions

my program is



#include<stdio.h>


struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr i;
nodeptr q;
nodeptr p;


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





int main()
{
int i;
int c;
nodeptr start;
nodeptr u;
nodeptr r;
for(i=0; i<6; i++)
{
printf("enter value");
scanf("%d",&c);
u=getnode();
u->info=c;
if(start==NULL)
start=u;
else
{
u->next=start;
start=u;
}
}
r=start;
start=r->next;
freenode(r);

while(start->next!=NULL)
{

printf("\n%d",start->info);
start=start->next;

}
return 0;
}

can yu suggest me how to write the push and pop function
closed account (o3hC5Di1)
Hi,

Would you please be so kind as to wrap your code in [code][/code]-tags and to apply some indentation?
http://en.wikipedia.org/wiki/Indent_style
It will make your code much more readable.

As for your problem: I'm assuming this is a linked-list implementation. You created a struct for a single node, but I can't see a struct for a list. It could look like this:

1
2
3
4
5
struct linked_list
{
    nodeptr start;
    nodeptr end;
}


start and end point to the first and last values in the list, so push and pop can just alter those.


All the best,
NwN
Last edited on
Topic archived. No new replies allowed.