DSA assignment
DSA assignment
1
void deleteBook(char* title) {
if (head == NULL) {
printf("No books in the library.\n");
return;
}
Book* temp = head;
while (temp != NULL) {
if (strcmp(temp->title, title) == 0) {
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
head = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
printf("Book deleted successfully!\n");
return;
}
temp = temp->next;
}
printf("Book not found.\n");
}
void displayMenu() {
printf("Library Management System\n");
printf("1. Add Book\n");
printf("2. Delete Book\n");
printf("3. Search Book\n");
printf("4. Exit\n");
printf("Enter your choice: ");
2
}
int main() {
int choice, id;
char title[100], author[100];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter book ID: ");
scanf("%d", &id);
getchar();
printf("Enter book title: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0;
printf("Enter book author: ");
fgets(author, 100, stdin);
author[strcspn(author, "\n")] = 0;
addBook(id, title, author);
break;
case 2:
getchar();
printf("Enter book title to delete: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0;
deleteBook(title);
break;
case 3:
getchar();
printf("Enter book title to search: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0;
searchBook(title);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
3
OUTPUT :
void goBack() {
if (current == NULL) {
printf("No history available.\n");
return;
}
current = current->prev;
printf("Current URL: %s\n", current->url);
}
void displayCurrentURL() {
if (current == NULL) {
printf("No current URL.\n");
} else {
printf("Current URL: %s\n", current->url);
}
5
}
void displayMenu() {
printf("Browser History Management System\n");
printf("1. Visit URL\n");
printf("2. Go Back\n");
printf("3. Display Current URL\n");
printf("4. Exit\n");
printf("Enter your choice: ");
}
int main() {
int choice;
char url[100];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
getchar(); // to consume the newline character left by scanf
printf("Enter URL: ");
fgets(url, 100, stdin);
url[strcspn(url, "\n")] = 0; // remove newline character
visitURL(url);
break;
case 2:
goBack();
break;
case 3:
displayCurrentURL();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
6
3. Undo-Redo Functionality using Stack
• Write a C program to implement undo and redo functionality for a simple text
editor using two stacks. The program should allow the user to type text, undo the
last operation, and redo the undone operation.
Undo-Redo Functionality using Stack
• Use two stacks to store actions for undo and redo functionality.
• Implement functions for typing text, undoing the last operation, and redoing the
undone operation.
• Use terminal input and output to interact with the user.
7
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int main() {
Stack undoStack, redoStack;
char editorContent[MAX] = "";
int choice;
char text[MAX];
initializeStack(&undoStack);
initializeStack(&redoStack);
while (1) {
printf("\nSimple Text Editor\n");
printf("1. Type Text\n");
printf("2. Undo\n");
printf("3. Redo\n");
printf("4. Display Content\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
printf("Enter text: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
typeText(&undoStack, &redoStack, text, editorContent);
break;
8
case 2:
undo(&undoStack, &redoStack, editorContent);
break;
case 3:
redo(&undoStack, &redoStack, editorContent);
break;
case 4:
displayEditorContent(editorContent);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// Stack functions
void initializeStack(Stack* stack) {
stack->top = -1;
}
OUTPUT :
10
4. Customer Service Simulation using Queue
• Develop a C program to simulate a customer service center using a queue.The
program should allow customers to enter the queue, be served in a first-come,
firstserved manner, and display the current queue. Customer Service Simulation
using Queue :
• Use a queue to manage customer requests.
• Implement functions for customers to enter the queue, be served, and display the
current queue.
• Use terminal input and output to interact with the user.
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void serveCustomer() {
if (front == NULL) {
printf("No customers in the queue.\n");
return;
}
Customer* temp = front;
printf("Serving customer %s (ID: %d).\n", temp->name, temp->id);
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
}
void displayMenu() {
printf("Customer Service Simulation\n");
printf("1. Enter Queue\n");
printf("2. Serve Customer\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
12
}
int main() {
int choice;
char name[100];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
getchar(); // to consume the newline character left by scanf
printf("Enter customer name: ");
fgets(name, 100, stdin);
name[strcspn(name, "\n")] = 0; // remove newline character
enqueueCustomer(name);
break;
case 2:
serveCustomer();
break;
case 3:
displayQueue();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
13
5. Task Scheduling System using Priority Queue (Min-Heap)
• Implement a C program that simulates a task scheduling system using a min-
heap. Each task should have a priority and description, and the program should
allow adding tasks, removing the highest priority task, and displaying the current
tasks. Task Scheduling System using Priority Queue (Min-Heap)
• Use a min-heap to manage tasks based on priority.
• Implement functions for adding tasks, removing the highest priority task, and
displaying current tasks.
• Use terminal input and output to interact with the user.
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Task heap[MAX_TASKS];
int heapSize = 0;
14
void swap(Task *a, Task *b) {
Task temp = *a;
*a = *b;
*b = temp;
}
void removeHighestPriorityTask() {
if (heapSize == 0) {
printf("No tasks available.\n");
return;
15
}
printf("Removing task: %s (Priority: %d)\n", heap[0].description, heap[0].priority);
heap[0] = heap[heapSize - 1];
heapSize--;
heapifyDown(0);
}
int main() {
int choice, priority;
char description[100];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter task priority: ");
scanf("%d", &priority);
getchar(); // to consume the newline character left by scanf
printf("Enter task description: ");
fgets(description, 100, stdin);
description[strcspn(description, "\n")] = 0;
addTask(priority, description);
break;
case 2:
removeHighestPriorityTask();
break;
16
case 3:
displayTasks();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
17
6. Social Network Friend Suggestion using Graph • Write a C program to
represent a social network using a graph and suggest friends to a user based on
mutual friends. The program should allow adding users, adding friendships, and
displaying friend suggestions for a given user using BFS.
Social Network Friend Suggestion using Graph
• Use an adjacency list to represent the social network graph.
• Implement functions for adding users, adding friendships, and displaying friend
suggestions using BFS.
• Use terminal input and output to interact with the user.
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Graph* createGraph() {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numUsers = 0;
for (int i = 0; i < MAX_USERS; i++) {
graph->adjLists[i] = NULL;
}
return graph;
}
18
void addFriendship(Graph* graph, int userId1, int userId2) {
if (userId1 >= graph->numUsers || userId2 >= graph->numUsers) {
printf("Invalid user IDs. Friendship not added.\n");
return;
}
Node* newNode1 = (Node*)malloc(sizeof(Node));
newNode1->userId = userId2;
newNode1->next = graph->adjLists[userId1];
graph->adjLists[userId1] = newNode1;
visited[userId] = 1;
queue[rear++] = graph->adjLists[userId];
void displayMenu() {
printf("Social Network Friend Suggestion System\n");
printf("1. Add User\n");
printf("2. Add Friendship\n");
printf("3. Display Friend Suggestions\n");
printf("4. Exit\n");
printf("Enter your choice: ");
}
int main() {
Graph* graph = createGraph();
int choice, userId1, userId2;
char userName[100];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
getchar(); // to consume the newline character left by scanf
printf("Enter user name: ");
fgets(userName, 100, stdin);
userName[strcspn(userName, "\n")] = 0;
addUser(graph, userName);
break;
case 2:
printf("Enter first user ID: ");
scanf("%d", &userId1);
printf("Enter second user ID: ");
scanf("%d", &userId2);
addFriendship(graph, userId1, userId2);
break;
case 3:
printf("Enter user ID for friend suggestions: ");
scanf("%d", &userId1);
displayFriendSuggestions(graph, userId1);
break;
case 4:
20
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
21
7. File System Simulation using Tree:
• Develop a C program to simulate a simple file system using a tree. The program
should allow creating directories and files, navigating between directories, and
displaying the structure of the current directory.
File System Simulation using Tree
• Use a tree structure to represent directories and files.
• Implement functions for creating directories and files, navigating between
directories, and displaying the current directory structure.
• Use terminal input and output to interact with the user
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Node* root;
Node* currentDirectory;
void initializeFileSystem() {
root = createNode("root", DIRECTORY);
currentDirectory = root;
}
22
Node* newNode = createNode(name, DIRECTORY);
newNode->parent = currentDirectory;
if (currentDirectory->child == NULL) {
currentDirectory->child = newNode;
} else {
Node* temp = currentDirectory->child;
while (temp->sibling != NULL) {
temp = temp->sibling;
}
temp->sibling = newNode;
}
printf("Directory %s created.\n", name);
}
void displayCurrentDirectoryStructure() {
printf("Current directory: %s\n", currentDirectory->name);
displayStructure(currentDirectory, 0);
}
void displayMenu() {
printf("File System Simulation\n");
printf("1. Create Directory\n");
printf("2. Create File\n");
printf("3. Change Directory\n");
printf("4. Display Current Directory Structure\n");
printf("5. Exit\n");
printf("Enter your choice: ");
}
int main() {
initializeFileSystem();
int choice;
char name[MAX_NAME];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter directory name: ");
scanf("%s", name);
createDirectory(name);
break;
case 2:
printf("Enter file name: ");
scanf("%s", name);
createFile(name);
break;
case 3:
printf("Enter directory name (.. for parent directory): ");
scanf("%s", name);
changeDirectory(name);
break;
24
case 4:
displayCurrentDirectoryStructure();
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
25
8. Spell Checker using Hash Table
• Implement a C program to create a spell checker using a hash table. The program
should read a dictionary of words into a hash table and allow the user to check if a
given word is spelled correctly.
Spell Checker using Hash Table
• Use a hash table to store dictionary words.
• Implement functions to read words into the hash table and check the spelling of
given words.
• Use terminal input and output to interact with the user.
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
Node* hashTable[TABLE_SIZE];
26
}
char word[WORD_SIZE];
while (fscanf(file, "%s", word) != EOF) {
insert(word);
}
fclose(file);
printf("Dictionary loaded successfully!\n");
}
void displayMenu() {
printf("Spell Checker\n");
printf("1. Load Dictionary\n");
printf("2. Check Word\n");
printf("3. Exit\n");
printf("Enter your choice: ");
}
int main() {
int choice;
char word[WORD_SIZE];
char filename[WORD_SIZE];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter dictionary filename: ");
scanf("%s", filename);
27
loadDictionary(filename);
break;
case 2:
printf("Enter word to check: ");
scanf("%s", word);
if (checkWord(word)) {
printf("%s is spelled correctly.\n", word);
} else {
printf("%s is misspelled.\n", word);
}
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
BookingRequest* dequeueBookingRequest() {
if (bookingQueueFront == NULL) {
return NULL;
}
BookingRequest* temp = bookingQueueFront;
bookingQueueFront = bookingQueueFront->next;
29
if (bookingQueueFront == NULL) {
bookingQueueRear = NULL;
}
return temp;
}
void bookTicket() {
BookingRequest* request = dequeueBookingRequest();
if (request == NULL) {
printf("No booking requests in the queue.\n");
return;
}
free(request);
if (temp == NULL) {
printf("Ticket number %d not found.\n", ticketNumber);
return;
}
if (prev == NULL) {
passengerList = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
void displayReservations() {
Passenger* temp = passengerList;
if (temp == NULL) {
printf("No current reservations.\n");
return;
}
printf("Current Reservations:\n");
while (temp != NULL) {
printf("Ticket Number: %d, Name: %s\n", temp->ticketNumber, temp->name);
temp = temp->next;
}
}
void displayMenu() {
printf("Railway Reservation System\n");
printf("1. Add Booking Request\n");
printf("2. Book Ticket\n");
printf("3. Cancel Ticket\n");
printf("4. Display Current Reservations\n");
printf("5. Exit\n");
printf("Enter your choice: ");
}
int main() {
int choice, ticketNumber;
char name[NAME_SIZE];
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter passenger name: ");
scanf("%s", name);
enqueueBookingRequest(name);
break;
case 2:
bookTicket();
break;
case 3:
printf("Enter ticket number to cancel: ");
scanf("%d", &ticketNumber);
cancelTicket(ticketNumber);
break;
case 4:
displayReservations();
break;
31
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
32
10.ATM Operations using Stack
• Develop a C program to simulate ATM operations using a stack. The program
should allow users to withdraw money, deposit money, and check the balance. It
should also support undoing the last transaction.
ATM Operations using Stack
• Use a stack to manage ATM transactions.
• Implement functions for withdrawing money, depositing money, checking the
balance, and undoing the last transaction.
• Use terminal input and output to interact with the user.
A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Transaction structure
typedef struct Transaction {
char type[10];
int amount;
} Transaction;
// Stack structure
typedef struct Stack {
Transaction data[MAX];
int top;
} Stack;
int main() {
Stack transactionStack;
int balance = 0;
int choice, amount;
initializeStack(&transactionStack);
while (1) {
printf("\nATM Operations\n");
printf("1. Withdraw Money\n");
33
printf("2. Deposit Money\n");
printf("3. Check Balance\n");
printf("4. Undo Last Transaction\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter amount to withdraw: ");
scanf("%d", &amount);
withdraw(&transactionStack, &balance, amount);
break;
case 2:
printf("Enter amount to deposit: ");
scanf("%d", &amount);
deposit(&transactionStack, &balance, amount);
break;
case 3:
checkBalance(balance);
break;
case 4:
undo(&transactionStack, &balance);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT :
36