Saturday, 23 December 2017

Write a C Program to perform following operations on Singly Linked List ADT :
i. Create
ii. Insert
iii. Delete
iv. Display
v. Exit

Implementation of above problem in C language:
#include<stdio.h>
#include<string.h>
#include<alloc.h>
#include<conio.h>
struct student
{
int sno;
char sname[25];
int m1,m2,m3;
int tot;
float per;
struct student *next;
};
void main()
{
char cont='y',choice;
char sear[20];
int ch,i,nsno;
struct student *pp,*fp,*p,*p1,*np;
clrscr();
do
{
printf("\n 1.Creation");
printf("\n 2.Traversing the Linked List");
printf("\n 3.Locating the particular element");
printf("\n 4.Inssertion");
printf("\n 5.Deletion");
printf("\n 6.Quit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: fp=p=p1=(struct student *)malloc(sizeof(struct student));
while(cont=='y')
{
printf("enter student no");
scanf("%d", &p->sno);
printf("enter student name");
scanf("%s", &p->sname);
printf("enter student mark1");
scanf("%d", &p->m1);
printf("enter student mark2");
scanf("%d", &p->m2);
printf("enter student mark3");
scanf("%d", &p->m3);
(*p).tot=(*p).m1+(*p).m2+(*p).m3;
(*p).per=(float)(*p).tot/3.0;
printf("do you want to continue");
fflush(stdin);
cont=getchar();
if(cont=='y')
p->next=(struct student *)malloc(sizeof(struct student));
else
p->next=(struct student *)0;
p=p->next;
}
break;
case 2:p1=fp;
printf("traversing the linked list");
while(p1!=(struct student *)0)
{
printf("student no:%d\n",p1->sno);
printf("student name:%s\n",p1->sname);
printf("marks1:%d\n",p1->m1);
printf("marks2:%d\n",p1->m2);
printf("marks3:%d\n",p1->m3);
printf("total:%d\n",p1->tot);
printf("average:%f\n",p1->per);
getch();
p1=p1->next;
}
break;
case 3:p1=fp;
printf("enter name of the student that you want to find out:");
scanf("%s",sear);
while(p1!=(struct student *)0)
{
i=strcmp(p1->sname,sear);
if(i==0)
{
printf("student no:%d\n",p1->sno);
printf("marks1:%d\n",p1->m1);
printf("marks2:%d\n",p1->m2);
printf("marks3:%d\n",p1->m3);
printf("total:%d\n",p1->tot);
printf("average:%f\n",p1->per);
break;
}
p1=p1->next;
}
if(p1==0&&i!=0)
printf("\n name not found");
break;
case 4:printf("a)at the beginning...\n");
printf("b)at the middle...\n");
printf("c)at the end...\n");
printf("enter your choice");
scanf("%*c%c",&choice);
switch(choice)
{
case'a':p=(struct student *)malloc(sizeof(struct student));
printf("enter student number");
scanf("%d",&p->sno);
printf("enter student name :");
scanf("%s",&p->sname);
printf("enter student mark1:");
scanf("%d",&p->m1);
printf("enter student mark2:");
scanf("%d",&p->m2);
printf("enter student mark3:");
scanf("%d*c",&p->m3);
(*p).tot=(*p).m1+(*p).m2+(*p).m3;
(*p).per=(float)(*p).tot/3.0;
p->next=fp;
fp=p;
break;
case'b':p1=fp;
printf("after which student do you want to insert");
scanf("%d", &nsno);
while(p1->sno!=nsno)
p1=p1->next;
np=p1->next;
p=(struct student *)malloc(sizeof(struct student));
p1->next=p;
printf("enter student number");
scanf("%d",&p->sno);
printf("enter student name :");
scanf("%s",&p->sname);
printf("enter student mark1:");
scanf("%d",&p->m1);
printf("enter student mark2:");
scanf("%d",&p->m2);
printf("enter student mark3:");
scanf("%d*c",&p->m3);
(*p).tot=(*p).m1+(*p).m2+(*p).m3;
(*p).per=(float)(*p).tot/3.0;
p->next=np;
break;
case'c':p1=fp;
while(p1->next!=(struct student *)0)
p1=p1->next;
p=(struct student *)malloc(sizeof(struct student));
p1->next=p;
printf("enter student number");
scanf("%d",&p->sno);
printf("enter student name:");
scanf("%s",&p->sname);
printf("enter student mark1:");
scanf("%d",&p->m1);
printf("enter student mark2:");
scanf("%d",&p->m2);
printf("enter student mark3:");
scanf("%d*c",&p->m3);
(*p).tot=(*p).m1+(*p).m2+(*p).m3;
(*p).per=(float)(*p).tot/3.0;
p->next=(struct student *)0;
break;
}
break;
case 5: printf("which student record u want to delete");
scanf("%d", &nsno);
if(fp==NULL)
{
printf("the singly linked list is empty");
break;
}
p1=fp;
if(fp->sno==nsno)
{
fp=fp->next;
p1=fp;
break;
}
else
{
p1=fp;
while(p1->sno!=nsno)
{
pp=p1;
p1=p1->next;
}
pp->next=p1->next;
printf("record deleted");
break;
}
p1=fp;
while(p1->next!=(struct student *)0)
p1=p1->next;
if(p1->next==((struct student *)0)&&(p1->sno==nsno))
{
p1=p1->next-1;
p1->next=((struct student *)0);
printf("no:%d",p1->sno);
break;
}
else
{
printf("the given sno is not found \n");
break;
}
}
} while(ch!=6);
}


Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput