Dsa Da4
Dsa Da4
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;
};
while (1) {
if (L1 == NULL) {
tail->next = L2;
break;
} else if (L2 == NULL) {
tail->next = L1;
break;
}
return dummy.next;
}
int main() {
struct student *L1 = NULL;
struct student *L2 = NULL;
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.
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;
};
int main() {
struct Node* head = NULL;
head = insertLetters(head);
int n;
printf("Enter the number of uncommon letters: ");
scanf("%d", &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).
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;
};
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
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);
}
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.
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* top;
} Stack;
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}
push(garage, truckId);
printf("Truck %d entered the garage.\n", truckId);
}
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* front;
Node* rear;
} Queue;
Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = NULL;
queue->rear = NULL;
return queue;
}
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)