0% found this document useful (0 votes)
10 views20 pages

Dsa Da4

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

Dsa Da4

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

Name: Sankalp Singh Solanki

Reg No: 23MIS0002

Data Structures and Algorithms

Assessment:4
Q1) Details such as Register number, name, quiz-3 marks of those students who wrote for
SET-A question paper and of those who wrote for SET-B question paper are maintained in
sorted order in two separate lists L1 and L2. Write a program to merge the two lists in sorted
order to facilitate mark entry in faculty login. Merging list 2 at the end of list 1. Merge list 1
and list 2 to get sorted output.

SOURCE CODE: -

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

struct student {
char register_number[20];
char name[50];
float quiz_3_marks;
struct student *next;
};

struct student* create_node(char register_number[], char name[], float


quiz_3_marks) {
struct student *new_node = (struct student *)malloc(sizeof(struct
student));
if (new_node != NULL) {
strcpy(new_node->register_number, register_number);
strcpy(new_node->name, name);
new_node->quiz_3_marks = quiz_3_marks;
new_node->next = NULL;
}
return new_node;
}
void insert_node(struct student **head, char register_number[], char name[],
float quiz_3_marks) {
struct student *new_node = create_node(register_number, name,
quiz_3_marks);
if (new_node == NULL) {
printf("Memory allocation failed\n");
return;
}
if (*head == NULL) {
*head = new_node;
} else {
struct student *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
}

struct student* merge_lists(struct student *L1, struct student *L2) {


struct student dummy;
struct student *tail = &dummy;
dummy.next = NULL;

while (1) {
if (L1 == NULL) {
tail->next = L2;
break;
} else if (L2 == NULL) {
tail->next = L1;
break;
}

if (strcmp(L1->register_number, L2->register_number) < 0) {


tail->next = L1;
L1 = L1->next;
} else {
tail->next = L2;
L2 = L2->next;
}
tail = tail->next;
}

return dummy.next;
}

void display_list(struct student *head) {


struct student *temp = head;
while (temp != NULL) {
printf("Register Number: %s, Name: %s, Quiz 3 Marks: %.2f\n", temp-
>register_number, temp->name, temp->quiz_3_marks);
temp = temp->next;
}
}

void free_list(struct student *head) {


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

int main() {
struct student *L1 = NULL;
struct student *L2 = NULL;

int num_students_L1, num_students_L2;

printf("Enter the number of students for List 1: ");


scanf("%d", &num_students_L1);

printf("Enter student details for List 1:\n");


char register_number[20], name[50];
float quiz_3_marks;
for (int i = 0; i < num_students_L1; ++i) {
printf("Enter register number for student %d: ", i + 1);
scanf("%s", register_number);
printf("Enter name for student %d: ", i + 1);
scanf("%s", name);
printf("Enter quiz 3 marks for student %d: ", i + 1);
scanf("%f", &quiz_3_marks);
insert_node(&L1, register_number, name, quiz_3_marks);
}

printf("\nEnter the number of students for List 2: ");


scanf("%d", &num_students_L2);

printf("\nEnter student details for List 2:\n");


for (int i = 0; i < num_students_L2; ++i) {
printf("Enter register number for student %d: ", i + 1);
scanf("%s", register_number);
printf("Enter name for student %d: ", i + 1);
scanf("%s", name);
printf("Enter quiz 3 marks for student %d: ", i + 1);
scanf("%f", &quiz_3_marks);
insert_node(&L2, register_number, name, quiz_3_marks);
}

struct student *merged_list = merge_lists(L1, L2);

printf("\nMerged List:\n");
display_list(merged_list);

free_list(merged_list);

return 0;
}

OUTPUT: -
Q2) Assume FLAMES game that tests for relationship have to be implemented using a
dynamic structure. The letters in the FLAMES stand for Friends, Love, Affection, Marriage,
Enmity and Sister. Initially store the individual letters of the word ‘flames’ in the nodes of the
dynamic structure. Given the count of the number of uncommon letters in the two names
‘n’, write a program to delete every nth node in it, till it is left with a single node. If the end
of the dynamic structure is reached while counting, resume the counting from the
beginning. Display the letter that remains and the corresponding relationship.

E.g., If Ajay and Jack are the two names, there are 4 uncommon letters in these. So delete
4th node in the first iteration and for the next iteration start counting from the node
following the deleted node.

Low Level: Delete only the first nth node only.

Middle Level: Implement the above problem to delete every nth node till the list is left with
a single node.

SOURCE CODE:

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

struct Node {
char data;
struct Node* next;
};

struct Node* createNode(char data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* insertLetters(struct Node* head) {
char letters[] = {'f', 'l', 'a', 'm', 'e', 's'};
struct Node* current = head;

for (int i = 0; i < 6; i++) {


struct Node* newNode = createNode(letters[i]);
if (head == NULL) {
head = newNode;
current = head;
} else {
current->next = newNode;
current = current->next;
}
}
current->next = head;
return head;
}

struct Node* deleteNthNode(struct Node* head, int n) {


if (head == NULL)
return NULL;

struct Node* prev = NULL;


struct Node* current = head;

while (current->next != current) {


for (int i = 1; i < n; i++) {
prev = current;
current = current->next;
}
prev->next = current->next;
struct Node* temp = current;
current = current->next;
free(temp);
}
return current;
}

void getRelationship(char letter) {


switch (letter) {
case 'f':
printf("Friends\n");
break;
case 'l':
printf("Love\n");
break;
case 'a':
printf("Affection\n");
break;
case 'm':
printf("Marriage\n");
break;
case 'e':
printf("Enmity\n");
break;
case 's':
printf("Sister\n");
break;
default:
printf("Invalid\n");
}
}

int main() {
struct Node* head = NULL;
head = insertLetters(head);

int n;
printf("Enter the number of uncommon letters: ");
scanf("%d", &n);

head = deleteNthNode(head, n);

printf("Relationship: ");
if (head != NULL)
getRelationship(head->data);
else
printf("Invalid\n");

return 0;
}

OUTPUT: -
Q3) Write a program to maintain the records of students in an effective dynamic structure.
Search a particular record based on the roll number and display the previous and next values
of that node with time complexity of O (1).

Low Level: Implement the above problem. [6 marks]

Middle Level: Delete user specified record and then display the current predecessor and
successor. [2 marks]

High Level: Also for the first node make the last node as the predecessor and for the last
node make the first node as the successor and thus make it a circular doubly linked list. [2
marks]

SOURCE CODE: -

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

struct Node {
int rollNumber;
struct Node* prev;
struct Node* next;
};

void insert(struct Node** head, int rollNumber) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->rollNumber = rollNumber;
newNode->prev = NULL;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}

struct Node* search(struct Node* head, int rollNumber) {


struct Node* current = head;
while (current != NULL) {
if (current->rollNumber == rollNumber) {
return current;
}
current = current->next;
}
return NULL;
}

void delete(struct Node** head, int rollNumber) {


struct Node* toDelete = search(*head, rollNumber);
if (toDelete == NULL) {
printf("Record with roll number %d not found\n", rollNumber);
return;
}

if (toDelete->prev != NULL) {
printf("Previous roll number: %d\n", toDelete->prev->rollNumber);
}
if (toDelete->next != NULL) {
printf("Next roll number: %d\n", toDelete->next->rollNumber);
}

if (toDelete->prev != NULL) {
toDelete->prev->next = toDelete->next;
} else {
*head = toDelete->next;
}
if (toDelete->next != NULL) {
toDelete->next->prev = toDelete->prev;
}
free(toDelete);
}

void makeCircular(struct Node* head) {


if (head == NULL)
return;
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = head;
head->prev = current;
}

int main() {
struct Node* head = NULL;
int choice, rollNumber;

do {
printf("\nMenu:\n");
printf("1. Insert a record\n");
printf("2. Delete a record\n");
printf("3. Make the list circular\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the roll number: ");
scanf("%d", &rollNumber);
insert(&head, rollNumber);
printf("Record inserted successfully.\n");
break;
case 2:
printf("Enter the roll number to delete: ");
scanf("%d", &rollNumber);
delete(&head, rollNumber);
break;
case 3:
makeCircular(head);
printf("List made circular.\n");
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 4);

return 0;
}
OUTPUT: -
Q4) a) There is a garage where the access road can accommodate any number of trucks at
one time. The garage is built in such a way that only the last truck entered can be moved
out. Each of the trucks is identified by a positive integer (a truck_id). Implement dynamically
to handle truck moves, allowing for the following commands:

i) On_road (truck_id);
ii) Enter_garage (truck_ id);
iii) Exit_garage (truck_id);
iv) Show_trucks (garage or road);

If an attempt is made to get a truck out which is not the closest to the garage entry, the error
message “Truck x cannot be moved” should be displayed.

b) For the scenario, assume now a circular road and two entries: one for entry, another for
exit. Trucks can get out only in the order they got in. Write a program dynamically to handle
truck moves allowing for the following commands.

i) Enter garage (truck name)

ii) Exit garage (truck name)

iii) Show trucks

Low Level: Implement the above problem. [6 marks]

Middle Level: Also check the number of services done so far to decide if service charge is
required or not. [2 marks]

High Level: Assuming any nth truck will need a longer period of time for service, that
particular truck should be retained in the garage and the links in the stack should be
modified accordingly.

SOURCE CODE: -

A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

typedef struct {
Node* top;
} Stack;

Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}

int isEmpty(Stack* stack) {


return stack->top == NULL;
}

void push(Stack* stack, int data) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
}

int pop(Stack* stack) {


if (isEmpty(stack)) {
printf("Error: Stack is empty\n");
return -1;
}
int data = stack->top->data;
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
return data;
}

void displayStack(Stack* stack) {


if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
Node* current = stack->top;
printf("Stack: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

void enterGarage(Stack* road, Stack* garage, int truckId) {

push(garage, truckId);
printf("Truck %d entered the garage.\n", truckId);
}

void exitGarage(Stack* garage) {


if (isEmpty(garage)) {
printf("Garage is empty, cannot remove truck.\n");
return;
}

int truckId = pop(garage);


printf("Truck %d exited the garage.\n", truckId);
}

void showTrucks(Stack* garage, Stack* road, char* location) {


if (strcmp(location, "garage") == 0) {
printf("Trucks in the garage: ");
displayStack(garage);
} else if (strcmp(location, "road") == 0) {
printf("Trucks on the road: ");
displayStack(road);
} else {
printf("Invalid location specified.\n");
}
}

int main() {
Stack* road = createStack();
Stack* garage = createStack();
int choice, truckId;

do {
printf("\nMenu:\n");
printf("1. Enter Garage\n");
printf("2. Exit Garage\n");
printf("3. Show Trucks in Garage\n");
printf("4. Show Trucks on Road\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter truck ID to enter garage: ");
scanf("%d", &truckId);
enterGarage(road, garage, truckId);
break;
case 2:
exitGarage(garage);
break;
case 3:
showTrucks(garage, road, "garage");
break;
case 4:
showTrucks(garage, road, "road");
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);

return 0;
}

OUTPUT: -

A)
SOURCE CODE: -

B)

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

typedef struct Node {


int data;
struct Node* next;
} Node;

typedef struct {
Node* front;
Node* rear;
} Queue;

Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = NULL;
queue->rear = NULL;
return queue;
}

int isEmpty(Queue* queue) {


return queue->front == NULL;
}

void enqueue(Queue* queue, int data) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}

int dequeue(Queue* queue) {


if (isEmpty(queue)) {
printf("Error: Queue is empty\n");
return -1;
}
int data = queue->front->data;
Node* temp = queue->front;
queue->front = queue->front->next;
free(temp);
if (queue->front == NULL) {
queue->rear = NULL;
}
return data;
}

void displayQueue(Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty\n");
return;
}
Node* current = queue->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

void enterGarage(Queue* road, Queue* garage, int truckId) {


enqueue(garage, truckId);
printf("Truck %d entered the garage.\n", truckId);
}

void exitGarage(Queue* garage) {


if (isEmpty(garage)) {
printf("Garage is empty, cannot remove truck.\n");
return;
}
int truckId = dequeue(garage);
printf("Truck %d exited the garage.\n", truckId);
}

void showTrucks(Queue* garage, Queue* road, char* location) {


if (strcmp(location, "garage") == 0) {
printf("Trucks in the garage: ");
displayQueue(garage);
} else if (strcmp(location, "road") == 0) {
printf("Trucks on the road: ");
displayQueue(road);
} else {
printf("Invalid location specified.\n");
}
}

int main() {
Queue* road = createQueue();
Queue* garage = createQueue();
int choice, truckId;

do {
printf("\nMenu:\n");
printf("1. Enter Garage\n");
printf("2. Exit Garage\n");
printf("3. Show Trucks in Garage\n");
printf("4. Show Trucks on Road\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter truck ID to enter garage: ");
scanf("%d", &truckId);
enterGarage(road, garage, truckId);
break;
case 2:
exitGarage(garage);
break;
case 3:
showTrucks(garage, road, "garage");
break;
case 4:
showTrucks(garage, road, "road");
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);

return 0;
}

OUTPUT: -

B)

You might also like