C program to construct a singly linked list consisting of following instructions in each node : 1.Student ID(int) 2.Student name(string) 3.Semester(int) The operations supported are 1.Insertion operation at any position 2.deleting a node based on ID
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char name[25];
int id,sem;
struct node *link;
}NODE,*NODEPTR;
NODEPTR insertp(NODEPTR start,int pos, int *count)
{
NODEPTR temp,prev,cur;
int i;
temp=(NODEPTR)malloc(sizeof(NODE));
if(temp==NULL)
printf("Memory allocation not possible\n");
printf("Enter the name, ID and semester of the student\n");
scanf("%s%d%d",temp->name,&temp->id,&temp->sem);
temp->link=NULL;
for(cur=start;cur!=NULL;cur=cur->link)
if(cur->id==temp->id)
{
printf("ID already exists\n");
free(temp);
return start;
}
(*count++);
if(pos==1)
{
temp->link=start;
start=temp;
return start;
}
cur=start;
for(i=1;i<pos;i++)
{
prev=cur;
cur=cur->link;
}
prev->link=temp;
temp->link=cur;
return start;
}
NODEPTR deleteid(NODEPTR start, int did, int *count)
{
NODEPTR prev,cur;
for(cur=start;cur!=NULL;prev=cur,cur=cur->link)
{
if(cur->link==did)
break;
}
if(cur==NULL)
{
printf("Record not found\n");
return start;
}
if(cur==start)
start=start->link;
else
prev->link=cur->link;
free(cur);
(*count)- -;
return start;
}
int search (NODEPTR start, int sid)
{
NODEPTR cur,t;
int mid;
for(cur=start;cur!=NULL;cur=cur->link)
if(cur->id==sid)
break;
if(cur==NULL)
printf("Record not found\n");
else
{
while(1)
{
printf("Enter the ID of the student to be searched\n");
scanf("%d",&nid);
for(t=start;t!=NULL;t=t->link)
{
if(nid=t->id && (nid!= cur->id))
{
printf("ID already exists, enter again\n");
break;
}
}
if(t==NULL)
{
cur->id=nid;
printf("Enter the name and semester\n");
scanf("%s%d",cur->name,&cur->sem);
return;
}
}//end of while
}//end of else
}//end of function
void display(NODEPTR start)
{
NODEPTR cur;
if(start==NULL)
printf("The list is empty\n");
else
{
printf("List content is shown below\n");
for(cur=start;cur!=NULL;cur=cur->link)
printf("%s\t%d\t%d\n",cur->name,cur->id,cur->sem);
}
}
int main()
{
NODEPTR start=NULL;
int ch, id, pos, count=0;
while(1)
{
display(start);
printf("Enter your choice of operation 1.Insert 2.Delete 3.Search OTHER:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("Enter the position (1 to %d) to insert the node\n", count+1);
scanf("%d",&pos);
if(pos < 1 || pos>count+1)
printf("Invalid position\n");
else
start=insertp(start,pos,&count);
break;
case 2 : printf("Enter the ID to be deleted\n");
scanf("%d",&id);
start=deleteid(start,id,&count);
break;
case 3 : printf("Enter the ID to be searched\n");
scanf("%d",&id);
search(start,id);
break;
default : return;
}
}
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char name[25];
int id,sem;
struct node *link;
}NODE,*NODEPTR;
NODEPTR insertp(NODEPTR start,int pos, int *count)
{
NODEPTR temp,prev,cur;
int i;
temp=(NODEPTR)malloc(sizeof(NODE));
if(temp==NULL)
printf("Memory allocation not possible\n");
printf("Enter the name, ID and semester of the student\n");
scanf("%s%d%d",temp->name,&temp->id,&temp->sem);
temp->link=NULL;
for(cur=start;cur!=NULL;cur=cur->link)
if(cur->id==temp->id)
{
printf("ID already exists\n");
free(temp);
return start;
}
(*count++);
if(pos==1)
{
temp->link=start;
start=temp;
return start;
}
cur=start;
for(i=1;i<pos;i++)
{
prev=cur;
cur=cur->link;
}
prev->link=temp;
temp->link=cur;
return start;
}
NODEPTR deleteid(NODEPTR start, int did, int *count)
{
NODEPTR prev,cur;
for(cur=start;cur!=NULL;prev=cur,cur=cur->link)
{
if(cur->link==did)
break;
}
if(cur==NULL)
{
printf("Record not found\n");
return start;
}
if(cur==start)
start=start->link;
else
prev->link=cur->link;
free(cur);
(*count)- -;
return start;
}
int search (NODEPTR start, int sid)
{
NODEPTR cur,t;
int mid;
for(cur=start;cur!=NULL;cur=cur->link)
if(cur->id==sid)
break;
if(cur==NULL)
printf("Record not found\n");
else
{
while(1)
{
printf("Enter the ID of the student to be searched\n");
scanf("%d",&nid);
for(t=start;t!=NULL;t=t->link)
{
if(nid=t->id && (nid!= cur->id))
{
printf("ID already exists, enter again\n");
break;
}
}
if(t==NULL)
{
cur->id=nid;
printf("Enter the name and semester\n");
scanf("%s%d",cur->name,&cur->sem);
return;
}
}//end of while
}//end of else
}//end of function
void display(NODEPTR start)
{
NODEPTR cur;
if(start==NULL)
printf("The list is empty\n");
else
{
printf("List content is shown below\n");
for(cur=start;cur!=NULL;cur=cur->link)
printf("%s\t%d\t%d\n",cur->name,cur->id,cur->sem);
}
}
int main()
{
NODEPTR start=NULL;
int ch, id, pos, count=0;
while(1)
{
display(start);
printf("Enter your choice of operation 1.Insert 2.Delete 3.Search OTHER:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("Enter the position (1 to %d) to insert the node\n", count+1);
scanf("%d",&pos);
if(pos < 1 || pos>count+1)
printf("Invalid position\n");
else
start=insertp(start,pos,&count);
break;
case 2 : printf("Enter the ID to be deleted\n");
scanf("%d",&id);
start=deleteid(start,id,&count);
break;
case 3 : printf("Enter the ID to be searched\n");
scanf("%d",&id);
search(start,id);
break;
default : return;
}
}
}