Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views18 pages

Week 5

The document outlines a lab session for a Data Structures course, focusing on single linked lists in C programming. It includes pre-lab questions, in-lab coding exercises for insertion and deletion operations, and a post-lab task to manage student records using linked lists. The document provides code snippets and expected outputs for each operation.

Uploaded by

12042024yk
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)
5 views18 pages

Week 5

The document outlines a lab session for a Data Structures course, focusing on single linked lists in C programming. It includes pre-lab questions, in-lab coding exercises for insertion and deletion operations, and a post-lab task to manage student records using linked lists. The document provides code snippets and expected outputs for each operation.

Uploaded by

12042024yk
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/ 18

A.Y:2024-25 Regd.

No:24EU05048

LabSession5
Date:
Pre-Lab

1. State the advantage of dynamic memory allocation.

2. Explain the syntax of malloc() function.

3. Define a Single Linked list.

4. Write operation of insertion at begin

5. Write operation of deletion at end

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

In-Lab
1. Write a c program to implement insertion operations using single linked list.

CODE:

#include<stdio.h>
#include <stdlib.h>

//Definethenodestructure
struct node {
intdata;
structnode *next;
};

// Head pointer for the list


structnode*head=NULL;

//Functiontoinsertatthebeginning void
insertAtBegin(int value) {
structnode*newNode=(structnode*)malloc(sizeof(structnode)); if
(newNode == NULL) {
printf("Memoryallocationfailed!\n");
return;
}
newNode->data=value;
newNode->next = head;
head = newNode;
printf("Insertedatbeginning: %d\n",value);
}

//Functiontoinsertattheend void
insertAtEnd(int value) {
structnode*newNode=(structnode*)malloc(sizeof(structnode)); if
(newNode == NULL) {
printf("Memoryallocationfailed!\n");
return;
}

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

newNode->data = value;
newNode->next=NULL;

if(head==NULL){ head
= newNode;
}else{
struct node *temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
}
printf("Insertedatend:%d\n",value);
}

//⬛
Function toinsertafteragivenkeyvalue
voidinsertAtKeyPosition(intkey,intvalue){
structnode*temp=head;
while(temp!=NULL&&temp->data!=key){ temp =
temp->next;
}

if(temp==NULL) {
printf("Key%dnotfound.Insertionfailed.\n",key);
return;
}

structnode*newNode=(structnode*)malloc(sizeof(structnode)); if
(newNode == NULL) {
printf("Memoryallocationfailed!\n");
return;
}

newNode->data = value;
newNode->next=temp->next;
temp->next = newNode;

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

printf("Inserted%d after key %d\n", value,key);


}

//Functiontodisplaythelinkedlist void
displayList() {
structnode*temp=head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d->",temp->data); temp
= temp->next;
}
printf("NULL\n");
}

//Mainfunctiontotestinsertions int
main() {
insertAtBegin(10);
insertAtEnd(20);
insertAtBegin(5);
insertAtEnd(30);
insertAtBegin(2);

displayList();

"Testinsert atkey position


//^
insertAtKeyPosition(10,15);//Insert15afternodewithvalue10
insertAtKeyPosition(99, 100); // Invalid case (key not found)

displayList();

return0;
}

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

OUTPUT:

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

2. Write a c program to implement deletion operations using single linked list.

CODE:
#include <stdio.h>
#include<stdlib.h>

//Definethenodestructure struct
node {
int data;
structnode*next;
};

// Head pointer for the list


structnode*head=NULL;

//Functiontoinsertattheend void
insertAtEnd(int value) {
structnode*newNode=(structnode*)malloc(sizeof(structnode)); if
(newNode == NULL) {
printf("Memoryallocationfailed!\n");
return;
}
newNode->data = value;
newNode->next=NULL;

if(head==NULL){ head
= newNode;
} else{
struct node *temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next= newNode;
}

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

printf("Insertedatend:%d\n", value);
}

//⬛
Functiontodeletefrombeginning void
deleteFromBegin() {
if (head ==NULL){
printf("Listisempty.Nothingtodelete.\n"); return;
}

structnode*temp=head; head
= head->next;
printf("Deletedfrombeginning:%d\n",temp->data); free(temp);
}

//⬛
Functiontodeletefromend void
deleteFromEnd() {
if (head ==NULL){
printf("Listisempty.Nothingtodelete.\n"); return;
}

if(head->next == NULL){
//Only onenode
printf("Deletedfromend:%d\n",head->data);
free(head);
head=NULL; return;
}

struct node *temp = head;


structnode*prev=NULL;

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

while(temp->next!=NULL){
prev = temp;
temp=temp->next;
}

printf("Deletedfromend:%d\n",temp->data);
prev->next = NULL;
free(temp);
}
voiddeleteByKey(intkey){
struct node *temp =head, *prev= NULL;

// Iftheheadnode holdsthe key


if(temp!=NULL&&temp->data==key){ head
= temp->next;
printf("Deleted:%d(frombeginning)\n",temp->data);
free(temp);
return;
}

// Search forthekey to bedeleted


while(temp!=NULL&&temp->data!=key){ prev =
temp;
temp=temp->next;
}

//Ifkeywasnotfound if
(temp == NULL) {
printf("Key%dnotfound.Deletionfailed.\n",key); return;
}

prev->next = temp->next;
printf("Deleted:%d\n",temp->data);
free(temp);

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

//Functiontodisplaythelinkedlist
void displayList() {
structnode*temp=head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}

//Mainfunctiontotestalloperations int
main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);
insertAtEnd(50);

displayList();

deleteFromBegin();
displayList();
deleteByKey(30);
displayList();
deleteFromEnd();
displayList();

return0;
}

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

OUTPUT:

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

Post-Lab
1. Write a c program Manage Student Records Using Single Linked List
Operations:
Insert
Display
Delete (by Roll Number)
Search (by Roll Number)
Eachstudentrecordcontains:
Roll number (int)
Name (string)
Marks(float)*/

CODE:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

//Definethestudentstructure struct
student {
int roll;
charname[50];
float marks;
structstudent*next;
};

//Headpointerforthelinkedlist
struct student* head = NULL;

//Functiontoinsertastudentattheend
voidinsertStudent(introll,charname[],floatmarks){
structstudent*newStudent=(structstudent*)malloc(sizeof(struct
student));
newStudent->roll = roll;
strcpy(newStudent->name,name);
newStudent->marks = marks;
newStudent->next = NULL;

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

if (head == NULL) {
head=newStudent;
}else{
struct student* temp = head;
while(temp->next!=NULL)
temp = temp->next;
temp->next=newStudent;
}

printf("Student%saddedsuccessfully.\n",name);
}

//Functiontodisplayallstudents
void displayStudents() {
if(head== NULL){
printf("Nostudentrecordsavailable.\n");
return;
}

struct student* temp = head;


printf("\nStudentRecords:\n");
while (temp != NULL) {
printf("RollNo:%d,Name:%s,Marks:%.2f\n",temp->roll,temp-
>name, temp->marks);
temp=temp->next;
}
}

//Functiontodeleteastudentbyrollnumber void
deleteStudent(int roll) {
structstudent*temp=head,*prev=NULL;

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

if(temp!=NULL&&temp->roll==roll){ head =
temp->next;
free(temp);
printf("Studentwithroll%ddeleted.\n",roll); return;
}

while(temp!=NULL&&temp->roll!=roll){ prev
= temp;
temp=temp->next;
}

if(temp==NULL){
printf("Studentwithroll%dnotfound.\n",roll); return;
}

prev->next=temp->next;
free(temp);
printf("Studentwithroll%ddeleted.\n", roll);
}

//⬛
Functiontosearchforastudentbyrollnumber
voidsearchStudent(introll){
structstudent*temp=head;
while (temp != NULL) {
if (temp->roll == roll) {
printf("\nStudentFound:\n");
printf("Roll No: %d, Name: %s, Marks: %.2f\n", temp-
>roll,temp->name, temp->marks);
return;
}
temp=temp->next;

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

}
printf("Studentwithroll%dnotfound.\n",roll);
}

//Mainfunctionwithmenu
int main() {
intchoice,roll;
charname[50];
float marks;

while(1){
printf("\n======StudentRecordMenu======\n");
printf("1. Insert Student\n");
printf("2. Display Students\n");printf("3.
DeleteStudent by RollNo\n");
printf("4.SearchStudentbyRollNo\n");
printf("5. Exit\n");
printf("Enteryourchoice:");
scanf("%d", &choice);

switch(choice){
case 1:
printf("Enterrollnumber:");
scanf("%d", &roll);
printf("Enter name: ");
scanf("%[^\n]s",name);//readstringwithspaces
printf("Enter marks: ");
scanf("%f", &marks);
insertStudent(roll,name,marks);
break;

case2:
displayStudents();

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

break;

case3:
printf("Enterrollnumbertodelete:");
scanf("%d", &roll);
deleteStudent(roll);
break;

case4:
printf("Enterrollnumbertosearch:");
scanf("%d", &roll);
searchStudent(roll);
break;

case5:
printf("Exitingprogram.Goodbye!\n");
exit(0);

default:
printf("Invalidchoice.Pleasetryagain.\n");
}
}

return0;
}

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

OUTPUT:

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

24IT102:DataStructures PageNo.
A.Y:2024-25 Regd.No:24EU05048

(Faculty use only)

CommentsoftheEvaluator: Evaluator’sObservation

MarksSecured: outof

24IT102:DataStructures PageNo.

You might also like