0% found this document useful (0 votes)
5 views11 pages

Sheet 5

Uploaded by

sararedmi22
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)
5 views11 pages

Sheet 5

Uploaded by

sararedmi22
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/ 11

sheet 5

‫آالء كمال فاروق اسماعيل‬


20230087

Question 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Task {


int taskId;
int taskPriority;
int executionTime;
struct Task* nextTask;
} Task;

typedef struct TaskQueue {


Task* firstTask;
Task* lastTask;
int taskCount;
} TaskQueue;

void initializeTaskQueue(TaskQueue* q) {
q->firstTask = q->lastTask = NULL;
q->taskCount = 0;
}

void addTask(TaskQueue* q, int taskId, int taskPriority, int executionTime) {


Task* newTask = (Task*)malloc(sizeof(Task));
newTask->taskId = taskId;
newTask->taskPriority = taskPriority;
newTask->executionTime = executionTime;
newTask->nextTask = NULL;

if (q->lastTask == NULL) {
q->firstTask = q->lastTask = newTask;
} else {
Task* current = q->firstTask;
Task* previous = NULL;

while (current != NULL && current->taskPriority <= taskPriority) {


previous = current;
current = current->nextTask;
}

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;
}

Task* temp = q->firstTask;


printf("Executing Task ID: %d, Priority: %d, Execution Time: %d\n",
temp->taskId, temp->taskPriority, temp->executionTime);

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;

typedef struct CommQueue {


Communication* firstComm;
Communication* lastComm;
int commCount;
} CommQueue;

void initializeCommQueue(CommQueue* q) {
q->firstComm = q->lastComm = NULL;
q->commCount = 0;
}

void addCommunication(CommQueue* q, int commType, int sender, int receiver) {


Communication* newComm = (Communication*)malloc(sizeof(Communication));
newComm->communicationType = commType;
newComm->senderId = sender;
newComm->receiverId = receiver;
newComm->nextComm = NULL;

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;

while (current != NULL && current->communicationType == 3) {


previous = current;
current = current->nextComm;
}

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;
}

Communication* temp = q->firstComm;


printf("Serving Communication - Type: %d, Sender: %d, Receiver: %d\n",
temp->communicationType, temp->senderId, temp->receiverId);

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;
}

Communication* current = q->firstComm;


Communication* previous = NULL;
while (current != NULL && current->communicationType != 1) {
previous = current;
current = current->nextComm;
}

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>

typedef struct DoubleNode {


int keyValue;
char infoData;
struct DoubleNode* prevNode;
struct DoubleNode* nextNode;
} DoubleNode;

typedef DoubleNode* DoublyList;

DoublyList initializeDoublyList() {
return NULL;
}

DoubleNode* createDoubleNode(int keyValue, char infoData) {


DoubleNode* newNode = (DoubleNode*)malloc(sizeof(DoubleNode));
if (!newNode) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->keyValue = keyValue;
newNode->infoData = infoData;
newNode->prevNode = NULL;
newNode->nextNode = NULL;
return newNode;
}

void insertNode(DoublyList* list, int keyValue, char infoData) {


DoubleNode* newNode = createDoubleNode(keyValue, infoData);

if (*list == NULL) {
*list = newNode;
return;
}

DoubleNode* current = *list;


while (current && current->keyValue < keyValue) {
current = current->nextNode;
}
if (current == NULL) {
DoubleNode* tail = *list;
while (tail->nextNode) {
tail = tail->nextNode;
}
tail->nextNode = newNode;
newNode->prevNode = tail;
} else if (current->keyValue == keyValue) {
printf("Duplicate key not allowed.\n");
free(newNode);
} else {
newNode->nextNode = current;
newNode->prevNode = current->prevNode;
if (current->prevNode) {
current->prevNode->nextNode = newNode;
} else {
*list = newNode;
}
current->prevNode = newNode;
}
}

void deleteNode(DoublyList* list, int keyValue) {


if (*list == NULL) {
printf("List is empty.\n");
return;
}

DoubleNode* current = *list;


while (current && current->keyValue != keyValue) {
current = current->nextNode;
}

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;
}

printf("Deleted node with key: %d and data: %c\n", current->keyValue, current->infoData);


free(current);
}

void destroyDoublyList(DoublyList* list) {


DoubleNode* current = *list;

while (current) {
DoubleNode* temp = current;
current = current->nextNode;
free(temp);
}
*list = NULL;
printf("List destroyed.\n");
}

void displayDoublyList(DoublyList list) {


DoubleNode* current = list;
while (current) {
printf("Key: %d, Data: %c\n", current->keyValue, current->infoData);
current = current->nextNode;
}
}

int main() {
DoublyList doublyList = initializeDoublyList();

insertNode(&doublyList, 1, 'A');
insertNode(&doublyList, 5, 'B');
insertNode(&doublyList, 3, 'C');

printf("List after insertion:\n");


displayDoublyList(doublyList);

deleteNode(&doublyList, 5);

printf("List after deletion:\n");


displayDoublyList(doublyList);
destroyDoublyList(&doublyList);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy