when we go to the append function definition,in the if statement a new node is being created if address of the structure is NULL
then after the creation the address is being assigned to the temp.
there is nothing weird in that
the weird part comes in else statement if you find a node
already created you goto the last node and then you append it by adding a new node
my doubt is that you are not returning the address of the new node to the main function i.e you are not assigning to *p then how the address of the *p is getting updated?
please help........
thanks a lot
Expand|Select|Wrap|Line Numbers
- #include "alloc.h"
- struct node
- {
- int x;
- int y;
- struct node *link;
- };
- main()
- {
- struct node *p;
- p=NULL;
- append(&p,5,74);
- display(p);
- printf("THE NO OF ELEMENTS IN THE LINKED LIST IS:%d",count(p));
- getch();
- }
- append(struct node **q,int num1,int num2)
- {
- struct node *temp;
- temp=*q;
- if(*q==NULL)
- {
- *q=malloc(sizeof(struct node));
- temp=*q;
- }
- else
- {
- while(temp->link!=NULL)
- temp=temp->link;
- temp->link=malloc(sizeof(struct node));
- temp=temp->link;
- }
- temp->x=num1;
- temp->y=num2;
- temp->link=NULL;
- }