hi guys i need your help
my program run and compiles but the output is not correct and i really could not locate the error
the program should create a list and insert a node (which properly works) but while the user inputs a value to be it should add the value to the sum of NULL
for example:
the output goes like this
1.create list
2.display list
3.insert node
4 exit
1 // user input
Enter data for the new node:
1 // user input
(then return)
1.create list
2.display list
3.insert node
4 exit
2 // user input
1
1.create list
2.display list
3.insert node
4 exit
3 // user input after display
Enter data wich the insertion is to be done
1 // user input
Insert a data for the node
2 // user input
1.create list
2.display list
3.insert node
4 exit
2 // user input
1
2
This should have a sum of 3 which is not shown and i dont know why???
the code is herei have an attachment file (Link.exe)CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node {
int data;
struct node *next;
};
void createlist(struct node **first,int n)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
*first=p;
p->data=n;
p->next=NULL;
}
void display(struct node *first)
{
struct node *p=first;
if(p==NULL)
{
puts("Link list is Empty");
}
else
{
while(p!=NULL)
{
printf("\n%d\t%d\t%d\n",p,p->data,p->next);
p=p->next;
}
}
}
void insertnode(struct node **first, int n)
{
int d, sum;
sum=0;
struct node *q;
struct node *p=*first;
q=(struct node*)malloc(sizeof(struct node));
puts("\nEnter data for the inserted node");
scanf("%d",&d);
sum=n+sum;
printf("sum of the nodes is:%d",sum);
while(p!=NULL)
{
if(p->data==n)
{
q->data=d;
q->next=p->next;
p->next=q;
return;
}
p=p->next;
}
}
void main()
{
struct node *first=NULL;
int ch,n;
do
{
puts("\nEnter choice\n1.create list\n2.display list");
puts("3.INsert a node\n4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(first==NULL)
{
puts("Enter Data for the new node:");
scanf("%d",&n);
createlist(&first,n);
}
break;
case 2: display(first);
break;
case 3: puts("\nEnter data of the node after wich the inserion is to be done:");
scanf("%d",&n);
printf("\n");
insertnode(&first,n);
printf("\n");
break;
}
}while(ch!=4);
}
please help me