0% found this document useful (0 votes)
16 views5 pages

Experiment 8

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)
16 views5 pages

Experiment 8

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/ 5

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
linkedist 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

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Student

char name[50];

int age; int

rollNumber; struct

Student* next;

};

struct Student* createStudent(char name[], int age, int rollNumber)

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

if (newStudent == NULL) { printf("Memory allocation failed.\n");

exit(1);

strcpy(newStudent->name, name);

newStudent->age = age;

newStudent->rollNumber = rollNumber;

newStudent->next = NULL;

return newStudent;

void insertStudent(struct Student** head, char name[], int age, int rollNumber)

{
struct Student* newStudent = createStudent(name, age,

rollNumber); newStudent->next = *head; *head =

newStudent; printf("Student inserted: %s (Roll Number:

%d)\n", name, rollNumber);

void deleteStudent(struct Student** head, int rollNumber)

struct Student* current = *head;

struct Student* prev = NULL;

while (current != NULL)

if (current->rollNumber == rollNumber)

if (prev != NULL)

prev->next = current->next;

else

*head = current->next;

free(current);

printf("Student with Roll Number %d deleted.\n",

rollNumber);

return;

prev = current;

current = current->next;
}

printf("Student with Roll Number %d not found.\n", rollNumber);

void displayStudents(struct Student* head)

if (head == NULL)

printf("The list is empty.\n");

return;

printf("List of Students:\n");

while (head != NULL) {

printf("Name: %s, Age: %d, Roll Number: %d\n", head->name, head->age,


head->rollNumber); head = head->next;

int main() { struct Student*

head = NULL;

insertStudent(&head, "Alice", 20, 101);

insertStudent(&head, "Bob", 21, 102); insertStudent(&head,

"Charlie", 22, 103);

displayStudents(head);
deleteStudent(&head, 102);

displayStudents(head);

return 0;

OUTPUT

You might also like