Linked List
There is my coding in C for insertion of a new node at desired location(using call by reference method). During compilation the program succeed, but its not showing any output. If you have any idea please correct me.
#include<stdio.h>
#include<conio.h>
struct student
{
int data;
struct student *next;
};
void insert_pos(struct student **);
void main()
{
struct student *head;
head=create();
traverse(head);
insert_pos(&head);
traverse(head);
getch();
}
void insert_pos(struct student **head)
{
struct student *ptr, *loc=NULL,*n;
ptr=*head;
n=(struct student *)malloc(sizeof(struct student *));
printf("Enter value");
scanf("%d",&n->data);
n->next=NULL
printf("Enter value where u want to insert");
scanf("%d",&item);
while(ptr!=NULL)
{
if(item==ptr->data)
{
loc=ptr;
break;
}
ptr=ptr->next;
}
if(loc==NULL)
ptr=n;
else
{
n->next=ptr->next;
loc->next=n;
}
}
struct student *create()
{
struct student *head=NULL,*ptr;
char choice='y';
while(choice='='||choice=='Y')
{
if(head==NULL)
{
head=(struct student *)malloc(sizeof(struct student *));
ptr=head;
}
else
{
ptr->next=(struct student *)malloc(sizeof(struct student *));
ptr=ptr->next;
}
ptr->next=NULL;
printf("Enter ur data");
scanf("%d",&ptr->data);
printf("Do u want 2 create another node");
scanf("%c",&choice);
}
return(head);
}
void traverse(struct student *ptr)
{
while(ptr!NULL)
{
printf("%d",ptr->data);
ptr=ptr->next;
}
}