#include
#include
struct student
{
int usn;
char name[25];
int sem;
};
typedef struct student s;
struct node
{
student data;
struct node *next;
};
typedef struct node node;
node *getnode()
{
node *newn;
newn=(node *)malloc(sizeof(node));
return(newn);
}
node *linsert(node *head,student s)
{
node *newn;
newn=getnode();
newn->data=s;
newn->next=head;
head=newn;
return(head);
}
node *rinsert(node *head,student s)
{
node *newn,*cur=head;
newn=getnode();
newn->data=s;
newn->next=NULL;
if(head==NULL)
{
head=newn;
return(head);
}
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=newn;
return(head);
}
node *posinsert(node *head,student s,int pos)
{
node *newn,*cur=head,*prev;
int cnt=0;
newn=getnode();
newn->data=s;
newn->next=NULL;
if(pos==0)
{
newn->next=head;
head=newn;
return(head);
}
while(cur!=NULL && cnt
{
prev=cur;
cur=cur->next;
cnt++;
}
if(cnt
{
printf("\n THE POSITION NOT FOUND");
return(head);
}
prev->next=newn;
newn->next=cur;
return(head);
}
node *keydelete(node *head,int key)
{
node *cur=head,*prev;
while(cur!=NULL && cur->data.usn!=key)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n THE SPECEFIED USN NOT FOUND\n");
return(head);
}
if(cur==head)
{
head=head->next;
free(cur);
return(head);
}
prev->next=cur->next;
free(cur);
return(head);
}
node *update(node *head,student s,int key)
{
node *cur=head;
while(cur!=NULL)
{
if(cur->data.usn==key)
break;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n THE SPECEFIED ID IS NOT FOUND\n");
return(NULL);
}
cur->data=s;
return(cur);
}
void display(node *head)
{
node *cur=head;
if(head==NULL)
{
printf("\n NO RECORDS FOUND");
return;
}
printf("\n THE STUDENT RECORD IS");
printf("\n USN\tNAME\t\tSEM\n\n");
while(cur!=NULL)
{
printf("\n %d\t%s\t\t%d\t",cur->data.usn,cur->data.name,cur->data.sem);
cur=cur->next;
}
}
main()
{
node *head=NULL,*t;
int ch,done=0,susn,pos;
student s;
while(!done)
{
printf("\n THE OPERATIONS....\n1,INSERT FRONT 2.INSERT BACK 3.INSERT AT POSITION 4.DELETE ON BASIS OF ID\n\n5.SEARCH AND UPDATE) 6.DISPLAY 7.QUIT \nENTER TYOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE STUDENT INFORMATION(USN,NAME,SEM):\n");
scanf("%d%s%d",&s.usn,s.name,&s.sem);
head=linsert(head,s);
break;
case 2:
printf("\n ENTER THE STUDENT INFORMATION(USN,NAME,SEM):\n");
scanf("%d%s%d",&s.usn,s.name,&s.sem);
head=rinsert(head,s);
break;
case 3:
printf("\nENTER THE STUDENT INFORMATION(USN,NAME,SEM):\n");
scanf("%d%s%d",&s.usn,s.name,&s.sem);
printf("\nENTER THE POSITION:");
scanf("%d",&pos);
head=posinsert(head,s,pos);
break;
case 4:
printf("\n ENTER THE USN WHOSE INFORMATION TO BE SEARCHED AND UPDATED:");
scanf("%d",&susn);
head=keydelete(head,susn);
display(head);
break;
case 5:
printf("\n ENTER THE STUDENT USN TO BE SEARCHED AND UPDATED:");
scanf("%d",&susn);
printf("\n ENTER THE STUDENT INFORMATION (USN,NAME,SEM):\n");
scanf("%d%s%d",&s.usn,s.name,&s.sem);
t=update(head,s,susn);
if(t!=NULL)
{
printf("\n UPDATE SUCCESSFUL...\n");
display(head);
}
break;
case 6:
display(head);
break;
case 7:
done=1;
break;
}
}
}
No comments:
Post a Comment