0% found this document useful (0 votes)
14 views9 pages

Linked List

Uploaded by

Dhruv Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Linked List

Uploaded by

Dhruv Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EXPERIMENT – 8

AIM: Implementation of various types of simple linked list like Insertion,


Deletion, display, etc. by taking student’s name, age, roll number as input from
the user.
1. Display:
❖ Display function:
• Write a function named display which is used to display the linked
list elements. It will be accessed by using for loop, iterating from
NULL to the next node of the linked list.
void display(struct Student *head);

2. Insert:
❖ Insert function:
• Write a structure named insert which is used to add a node into the
linked list by taking an input of data needed.
struct Student * insert(struct Student *head, int rollnumber, char name[100], int
age)
3. Delete:
❖ Delete function:
• Write a structure named delete which is used to remove a node
from the linked list.
struct Student * Delete(struct Student *head, int rollnumber)

4. Update:
❖ Update function:
• Write a function named update which is used to update the data of
the node in the linked list.
void update(struct Student *head, int rollnumber)

❖ Main Function:
• Input all the required details from the user.
• Input the operation to be done by the user.
• Call the operation to be performed.
• By this check various linked list operation.

❖ Code(linked list):
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct Student
{
int rollnumber;
char name[100];
int age;
struct Student *next;

};
struct Student * insert(struct Student *head, int rollnumber, char name[100], int
age)
{

struct Student * student = (struct Student *) malloc(sizeof(struct Student));


student->rollnumber = rollnumber;
strcpy(student->name, name);
student->age = age;
student->next = NULL;

if(head==NULL)
{
head = student;
}
else{
student->next = head;
head = student;
}

return head;

}
void search(struct Student *head, int rollnumber)
{
struct Student * temp = head;
while(temp!=NULL){
if(temp->rollnumber==rollnumber){
printf("Roll Number: %d\n", temp->rollnumber);
printf("Name: %s\n", temp->name);
printf("Age: %d\n", temp->age);
return;
}
temp = temp->next;
}
printf("Student with roll number %d is not found !!!\n", rollnumber);
}
void update(struct Student *head, int rollnumber)
{

struct Student * temp = head;


while(temp!=NULL){

if(temp->rollnumber==rollnumber){
printf("Record with roll number %d Found !!!\n", rollnumber);
printf("Enter new name: ");
scanf("%s", temp->name);
printf("Enter new age: ");
scanf("%f",&temp->age);
printf("Updation Successful!!!\n");
return;
}
temp = temp->next;

}
printf("Student with roll number %d is not found !!!\n", rollnumber);

}
struct Student * Delete(struct Student *head, int rollnumber)
{
struct Student * temp1 = head;
struct Student * temp2 = head;
while(temp1!=NULL){

if(temp1->rollnumber==rollnumber){

if(temp1==temp2){
head = head->next;
free(temp1);
}
else{
temp2->next = temp1->next;
free(temp1);
}

return head;

}
temp2 = temp1;
temp1 = temp1->next;

return head;

}
void display(struct Student *head)
{
struct Student * temp = head;
while(temp!=NULL){

printf("Roll Number: %d\n", temp->rollnumber);


printf("Name: %s\n", temp->name);
printf("Age: %d\n", temp->age);
temp = temp->next;

}
}
struct Student * sort(struct Student *head){

struct Student *temp = NULL;


struct Student *ptr;
struct Student *lptr;
while(head!=NULL){

ptr = head;
lptr = head;
while(ptr!=NULL){
if(ptr->rollnumber > lptr->rollnumber){
lptr = ptr;
}
ptr = ptr->next;
}
temp = insert(temp, lptr->rollnumber, lptr->name, lptr->age);
head = Delete(head, lptr->rollnumber);
}
return temp;

}
int main()
{
struct Student *head = NULL;
int choice;
char name[100];
int rollnumber;
int age;
printf("1 to insert student details\n2 to search for student details\n3 to delete
student details\n4 to update student details\n5 to display all student details\n6 to
sort records based on roll number");
do
{
printf("\nEnter Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter roll number: ");
scanf("%d", &rollnumber);
printf("Enter name: ");
scanf("%s", name);
printf("Enter age: ");
scanf("%d", &age);
head = insert(head,rollnumber, name, age);
break;
case 2:
printf("Enter roll number to search: ");
scanf("%d", &rollnumber);
search(head,rollnumber);
break;
case 3:
printf("Enter roll number to delete: ");
scanf("%d", &rollnumber);
head = Delete(head, rollnumber);
break;
case 4:
printf("Enter roll number to update: ");
scanf("%d", &rollnumber);
update(head, rollnumber);
break;
case 5:
display(head);
break;
case 6:
head = sort(head);
break;
}

} while (choice != 0);


}
❖ Expected Output:

❖ Conclusion: Here we learn about the various kind of linked list operations
like insertion, deletion, display, etc.

You might also like