I have written a code for linklist; the different operations on it; but at the time of insertion; it doesn't create a new strct object; it's overwritting the value.....
Just see if somebody can point out, what's wrong with it.
As, the complete code is quite big; m writting only the insertion part.
Here; first is my root node.
And i have made a functon, which accepts the value of first.
[CODE=c]//General insertion.
#include<stdio. h>
#include<stdlib .h>
struct linklist
{
int value;
struct linklist*next;
}* first;
void insertion(struc t linklist*first)
{
struct linklist* add=NULL;
add= (struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter the no. you wish to add in link list\n");
scanf("\n %d",&add->value);
/*while(first!=N ULL)
{
first=first->next;
}
first=add;
add->next=NULL;*/
first->next=add;
add->next=NULL;
add=add->next;
}
//Display of list
void display(struct linklist* first)
{
printf("\nFollo wing are the elements you have added in the list till now\n");
while(first!=NU LL)
{
printf("\n%d\n" ,first->value);
first=first->next;
}
}[/CODE]
I know here it works only for two values........
But then if i write a code for more than one value; it doesnt diplay even a single value.
The commented code is what i thought should work for inserting different value; but if i go for it; display function doesn't even display 2 values......
So, for the time being i have commented it out.
I had also tried with if statements; but invain.
What i need here is to update the value of first->next; so that there will be link in the objects; and every time user calls insertion function; a new add object should get created.
And here its overwrittin .......
Tell me what to do.
Just see if somebody can point out, what's wrong with it.
As, the complete code is quite big; m writting only the insertion part.
Here; first is my root node.
And i have made a functon, which accepts the value of first.
[CODE=c]//General insertion.
#include<stdio. h>
#include<stdlib .h>
struct linklist
{
int value;
struct linklist*next;
}* first;
void insertion(struc t linklist*first)
{
struct linklist* add=NULL;
add= (struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter the no. you wish to add in link list\n");
scanf("\n %d",&add->value);
/*while(first!=N ULL)
{
first=first->next;
}
first=add;
add->next=NULL;*/
first->next=add;
add->next=NULL;
add=add->next;
}
//Display of list
void display(struct linklist* first)
{
printf("\nFollo wing are the elements you have added in the list till now\n");
while(first!=NU LL)
{
printf("\n%d\n" ,first->value);
first=first->next;
}
}[/CODE]
I know here it works only for two values........
But then if i write a code for more than one value; it doesnt diplay even a single value.
The commented code is what i thought should work for inserting different value; but if i go for it; display function doesn't even display 2 values......
So, for the time being i have commented it out.
I had also tried with if statements; but invain.
What i need here is to update the value of first->next; so that there will be link in the objects; and every time user calls insertion function; a new add object should get created.
And here its overwrittin .......
Tell me what to do.
Comment