Program:
#include <stdio.h>
#include <stdlib.h>
struct student
{
int rollno;
char name[20];
float fee;
struct student *next;
};
struct student *start = NULL;
void insertnode()
{
struct student *newstudent = (struct student *)malloc(sizeof(struct student));
if (newstudent == NULL)
printf("Overflow: Memory Full. You cant insert more items\n");
else
{
printf("\nenter student roll no: ");
scanf("%d", &newstudent->rollno);
printf("enter name: ");
fflush(stdin);
gets(newstudent->name);
printf("enter fee : ");
scanf("%f", &newstudent->fee);
newstudent->next = NULL;
if (start == NULL)
start = newstudent;
else
{
struct student *ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = newstudent;
}
}
}
void display()
{
if (start == NULL)
printf("Linked List is empty. No item to display");
else
{
struct student *ptr = start;
printf("\nInformation of Linked List is : \n");
printf("Rollno\tname\tfee");
while (ptr != NULL)
{
printf("\n%d\t%s\t%f\n", ptr->rollno, ptr->name, ptr->fee);
ptr = ptr->next;
}
}
}
void search(int rn)
{
if (start == NULL)
printf("Linked List is empty.");
else
{
struct student *ptr = start;
while (ptr != NULL && ptr->rollno != rn)
ptr = ptr->next;
if (ptr == NULL)
printf("Rollno not found in the list");
else
{
printf("\nInformation of Linked List is : \n");
printf("Rollno\tname\tfee");
printf("\n%d\t%s\t%f\n", ptr->rollno, ptr->name, ptr->fee);
}
}
}
void main()
{
int ch, rn;
printf("****************************************\n");
printf("****************************************\n");
printf("** Search an item in a Linked List **\n");
printf("** Program Created By Sheetal Garg **\n");
printf("** Assistant professor **\n");
printf("** 9467863365 **\n");
printf("****************************************\n");
printf("****************************************\n");
while (1)
{
printf("\nOptions Available are\n");
printf("1. Insert Node \n");
printf("2. Display Linked List\n");
printf("3. Search Node \n");
printf("4. Exit \n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insertnode();
break;
case 2:
display();
break;
case 3:
printf("Enter Rollno to find");
scanf("%d", &rn);
search(rn);
break;
case 4:
exit(0);
default:
printf("Wrong choice entered");
}
}
}
Output:
****************************************
****************************************
** Search an item in a Linked List **
** Program Created By Sheetal Garg **
** Assistant professor **
** 9467863365 **
****************************************
****************************************
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 1
enter student roll no: 1
enter name: Sheetal
enter fee : 1000
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 1
enter student roll no: 2
enter name: Amit
enter fee : 2000
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 1
enter student roll no: 3
enter name: Nidhi
enter fee : 4000
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 2
Information of Linked List is :
Rollno name fee
1 Sheetal 1000.000000
2 Amit 2000.000000
3 Nidhi 4000.000000
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 3
Enter Rollno to find1
Information of Linked List is :
Rollno name fee
1 Sheetal 1000.000000
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 3
Enter Rollno to find5
Rollno not found in the list
Options Available are
1. Insert Node
2. Display Linked List
3. Search Node
4. Exit
Enter your choice : 4