Sheet 5
Sheet 5
Question 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void initializeTaskQueue(TaskQueue* q) {
q->firstTask = q->lastTask = NULL;
q->taskCount = 0;
}
if (q->lastTask == NULL) {
q->firstTask = q->lastTask = newTask;
} else {
Task* current = q->firstTask;
Task* previous = NULL;
if (previous == NULL) {
newTask->nextTask = q->firstTask;
q->firstTask = newTask;
} else {
previous->nextTask = newTask;
newTask->nextTask = current;
if (current == NULL) {
q->lastTask = newTask;
}
}
}
q->taskCount++;
}
void executeTask(TaskQueue* q) {
if (q->firstTask == NULL) {
printf("No tasks to execute.\n");
return;
}
q->firstTask = q->firstTask->nextTask;
if (q->firstTask == NULL) {
q->lastTask = NULL;
}
free(temp);
q->taskCount--;
}
void showWaitingTasks(TaskQueue* q) {
printf("Number of waiting tasks: %d\n", q->taskCount);
}
int main() {
TaskQueue queue;
initializeTaskQueue(&queue);
int option;
do {
printf("\nMenu:\n");
printf("1. Add a New Task\n");
printf("2. Execute a Task\n");
printf("3. Show Waiting Tasks\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1: {
int taskId, taskPriority, executionTime;
printf("Enter Task ID: ");
scanf("%d", &taskId);
printf("Enter Priority (smaller value = higher priority): ");
scanf("%d", &taskPriority);
printf("Enter Execution Time: ");
scanf("%d", &executionTime);
addTask(&queue, taskId, taskPriority, executionTime);
break;
}
case 2:
executeTask(&queue);
break;
case 3:
showWaitingTasks(&queue);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (option != 4);
return 0;
}
Question 2
#include <stdio.h>
#include <stdlib.h>
typedef struct Communication {
int communicationType;
int senderId;
int receiverId;
struct Communication* nextComm;
} Communication;
void initializeCommQueue(CommQueue* q) {
q->firstComm = q->lastComm = NULL;
q->commCount = 0;
}
if (q->lastComm == NULL) {
q->firstComm = q->lastComm = newComm;
} else if (commType == 3) {
newComm->nextComm = q->firstComm;
q->firstComm = newComm;
if (q->lastComm == NULL) {
q->lastComm = newComm;
}
} else if (commType == 2) {
Communication* current = q->firstComm;
Communication* previous = NULL;
if (previous == NULL) {
newComm->nextComm = q->firstComm;
q->firstComm = newComm;
} else {
previous->nextComm = newComm;
newComm->nextComm = current;
if (current == NULL) {
q->lastComm = newComm;
}
}
} else {
q->lastComm->nextComm = newComm;
q->lastComm = newComm;
}
q->commCount++;
}
void serveCommunication(CommQueue* q) {
if (q->firstComm == NULL) {
printf("No communications to serve.\n");
return;
}
q->firstComm = q->firstComm->nextComm;
if (q->firstComm == NULL) {
q->lastComm = NULL;
}
free(temp);
q->commCount--;
}
void dismissDataCommunications(CommQueue* q) {
if (q->firstComm == NULL) {
printf("No communications to dismiss.\n");
return;
}
if (current == NULL) {
printf("No data communications to dismiss.\n");
} else {
if (previous == NULL) {
q->firstComm = current->nextComm;
} else {
previous->nextComm = current->nextComm;
}
if (current == q->lastComm) {
q->lastComm = previous;
}
free(current);
q->commCount--;
printf("Dismissed a data communication.\n");
}
}
int main() {
CommQueue queue;
initializeCommQueue(&queue);
int option;
do {
printf("\nMenu:\n");
printf("1. Data Communication coming.\n");
printf("2. Voice Communication coming.\n");
printf("3. Emergency Communication coming.\n");
printf("4. Serve a Communication.\n");
printf("5. Dismiss Data Communications.\n");
printf("6. Exit.\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1: {
int sender, receiver;
printf("Enter Sender ID: ");
scanf("%d", &sender);
printf("Enter Receiver ID: ");
scanf("%d", &receiver);
addCommunication(&queue, 1, sender, receiver);
printf("Data Communication added.\n");
break;
}
case 2: {
int sender, receiver;
printf("Enter Sender ID: ");
scanf("%d", &sender);
printf("Enter Receiver ID: ");
scanf("%d", &receiver);
addCommunication(&queue, 2, sender, receiver);
printf("Voice Communication added.\n");
break;
}
case 3: {
int sender, receiver;
printf("Enter Sender ID: ");
scanf("%d", &sender);
printf("Enter Receiver ID: ");
scanf("%d", &receiver);
addCommunication(&queue, 3, sender, receiver);
printf("Emergency Communication added.\n");
break;
}
case 4:
serveCommunication(&queue);
break;
case 5:
dismissDataCommunications(&queue);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (option != 6);
return 0;
}
Question 3
#include <stdio.h>
#include <stdlib.h>
DoublyList initializeDoublyList() {
return NULL;
}
if (*list == NULL) {
*list = newNode;
return;
}
if (!current) {
printf("Key not found.\n");
return;
}
if (current->prevNode) {
current->prevNode->nextNode = current->nextNode;
} else {
*list = current->nextNode;
}
if (current->nextNode) {
current->nextNode->prevNode = current->prevNode;
}
while (current) {
DoubleNode* temp = current;
current = current->nextNode;
free(temp);
}
*list = NULL;
printf("List destroyed.\n");
}
int main() {
DoublyList doublyList = initializeDoublyList();
insertNode(&doublyList, 1, 'A');
insertNode(&doublyList, 5, 'B');
insertNode(&doublyList, 3, 'C');
deleteNode(&doublyList, 5);
return 0;
}