Here is the code i hav written for inserting a no., after given no. in a link list;
i guess the logic is ofcourse right.
there is no error in it,
but at the time of display its not displaying the updated value.
[CODE=cpp]#include<stdio. h>
#include<stdlib .h>
struct linklist
{
int value;
struct linklist*next;
}* first;
//Inserting a number after given number
void insertafter(str uct linklist*first)
{
int p_no;
struct linklist*hold;
struct linklist*temp;
hold=(struct linklist*)mallo c(sizeof(struct linklist));
temp=(struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter a particular no. after which you want to add a no.\n");
scanf("\n%d",&p _no);
if(first->value==p_no)
{
printf("\nEnter the desired number:\n");
scanf("\n%d",&h old->value);
hold->next=first->next;
first->next=hold;
first=first->next;
}
else
{
temp=first;
while(temp->next!=NULL &&temp->value!=p_no)
{
temp=temp->next;
}
if(temp->value!=p_no)
{
printf("\nThe number after which you want to add another no. does not exist in list.\n ");
}
else
{
printf("\nEnter the desired number:\n");
scanf("\n%d",&h old->value);
hold->next=temp->next;
temp=hold;
}
}
}
//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 guess the logic is ofcourse right.
there is no error in it,
but at the time of display its not displaying the updated value.
[CODE=cpp]#include<stdio. h>
#include<stdlib .h>
struct linklist
{
int value;
struct linklist*next;
}* first;
//Inserting a number after given number
void insertafter(str uct linklist*first)
{
int p_no;
struct linklist*hold;
struct linklist*temp;
hold=(struct linklist*)mallo c(sizeof(struct linklist));
temp=(struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter a particular no. after which you want to add a no.\n");
scanf("\n%d",&p _no);
if(first->value==p_no)
{
printf("\nEnter the desired number:\n");
scanf("\n%d",&h old->value);
hold->next=first->next;
first->next=hold;
first=first->next;
}
else
{
temp=first;
while(temp->next!=NULL &&temp->value!=p_no)
{
temp=temp->next;
}
if(temp->value!=p_no)
{
printf("\nThe number after which you want to add another no. does not exist in list.\n ");
}
else
{
printf("\nEnter the desired number:\n");
scanf("\n%d",&h old->value);
hold->next=temp->next;
temp=hold;
}
}
}
//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]
Comment