CSE 2104 NAZIFA TASNIM HIA
Lecturer, Department of CSE at University of Liberal Arts Bangladesh (ULAB)
Object Oriented Programming Lab
Thursday, April 17.
NAME: MD SYFUL ALAM RAFI
Title: Linked List Deletion.
Problem:
Console-Based CRUD Application:
You are tasked with developing a simple console-based application that performs basic CRUD
operations (Create, Read, Update, Delete) on a list of records.
CRUD:
● Create – Add a new record to the system.
● Read – Display the records stored in the system.
● Update – Modify an existing record based on a unique identifier.
● Delete – Remove a specific record from the system.
System Description:
The application will store a list of records, each having the following three attributes:
1. A unique identifier (e.g., Record ID – integer)
2. A name or title (e.g., Name – string, max 50 characters)
3. A numeric or float value representing some detail (e.g., Rating, GPA, Score, etc.)
required to:
Implement this system using a suitable data structure.
Use dynamic memory allocation to manage the records efficiently at runtime.
Ensure that all CRUD operations are implemented:
Add a new record
Delete a record by ID
Update an existing record by ID
Display all records
Search for a record by ID
Objective: To understand the time difficulties of doubly linked lists (DLL) and develop a deletion
function for them. Create a deletion function for a singly circular linked list (SCLL) as an extra
assignment.
Tools Used:
1. Language: C
2. Compiler: GCC
3. IDE: CodeBlocks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 50
typedef struct Record {
int id;
char name[NAME_LENGTH];
float value;
struct Record* next;
} Record;
Record* head = NULL;
void addRecord() {
Record* newRecord = (Record*)malloc(sizeof(Record));
printf("Enter ID: ");
scanf("%d", &newRecord->id);
printf("Enter Name: ");
getchar(); // flush newline
fgets(newRecord->name, NAME_LENGTH, stdin);
newRecord->name[strcspn(newRecord->name, "\n")] = '\0';
printf("Enter Value (Rating/GPA/Score): ");
scanf("%f", &newRecord->value);
newRecord->next = head;
head = newRecord;
printf("Record added successfully.\n");
}
void displayRecords() {
if (head == NULL) {
printf("No records found.\n");
return;
}
Record* temp = head;
printf("\n--- All Records ---\n");
while (temp != NULL) {
printf("ID: %d\nName: %s\nValue: %.2f\n\n", temp->id, temp->name, temp->value);
temp = temp->next;
}
}
void searchRecord() {
int id;
printf("Enter ID to search: ");
scanf("%d", &id);
Record* temp = head;
while (temp != NULL) {
if (temp->id == id) {
printf("Record Found:\nID: %d\nName: %s\nValue: %.2f\n", temp->id, temp->name,
temp->value);
return;
}
temp = temp->next;
}
printf("Record with ID %d not found.\n", id);
}
void updateRecord() {
int id;
printf("Enter ID to update: ");
scanf("%d", &id);
Record* temp = head;
while (temp != NULL) {
if (temp->id == id) {
printf("Enter new name: ");
getchar(); // flush newline
fgets(temp->name, NAME_LENGTH, stdin);
temp->name[strcspn(temp->name, "\n")] = '\0';
printf("Enter new value: ");
scanf("%f", &temp->value);
printf("Record updated successfully.\n");
return;
}
temp = temp->next;
}
printf("Record with ID %d not found.\n", id);
}
void deleteRecord() {
int id;
printf("Enter ID to delete: ");
scanf("%d", &id);
Record *temp = head, *prev = NULL;
while (temp != NULL && temp->id != id) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Record with ID %d not found.\n", id);
return;
}
if (prev == NULL) {
head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
printf("Record deleted successfully.\n");
}
int main() {
int choice;
do {
printf("\n--- CRUD MENU ---\n");
printf("1. Add Record\n");
printf("2. Display All Records\n");
printf("3. Search Record by ID\n");
printf("4. Update Record by ID\n");
printf("5. Delete Record by ID\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addRecord(); break;
case 2: displayRecords(); break;
case 3: searchRecord(); break;
case 4: updateRecord(); break;
case 5: deleteRecord(); break;
case 6: printf("Exiting...\n"); break;
default: printf("Invalid option. Try again.\n");
}
} while (choice != 6);
return 0;
}
Photos executing the program:
Discussion: This program supports all fundamental CRUD tasks and makes use of a single linked list. Malloc
and free are used to dynamically manage memory. The functions of creating, reading, updating, deleting, and
searching are all neatly divided. The system makes good use of dynamic data structures in C and is both
expandable and efficient.