0% found this document useful (0 votes)
29 views43 pages

Report 6. Titerez Vladislav

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)
29 views43 pages

Report 6. Titerez Vladislav

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/ 43

MINISTRY OF EDUCATION, CULTURE AND RESEARCH

OF THE REPUBLIC OF MOLDOVA


Technical University of Moldova
Faculty of Computers, Informatics and Microelectronics
Department of Software and Automation Engineering

Titerez Vladislav
Group: FAF-233
Report
Laboratory Work No.6

of the "Data Structures and Algorithms" course


Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
FCIM Faculty, UTM

Chisinau – 2024

(I did it in google docs, so Microsoft Word PC version can look worse )

The purpose of the laboratory work


The purpose of the laboratory work is solving the problems using STACK / QUEUE
ADT, FILE data type & UDT (user-defined data type) in C / C++. To develop a procedural-
style program in C / C++, using own written functions. Using memory allocation functions.
Familiarization with different linked lists.

Conditions of the problems

Develop a procedural-style program in C / C++, using own written functions. Data


processing in your program should be organized according to a given length of input
records based on memory allocation functions.
1
Your solution should:
A. use the pointers;
B. have to be presented in your report, emerging from the content of the
problem statement.
C. To draw the block diagram corresponding to the solved problem.
Attention! The grading will take into account the originality, complexity,
and quality of the proposed and solved changes.
Using the custom data type (CDT) (structs and structs members (both the initial
ones and the output dataset)), that have been assigned to you in problem set no.5 (see
the preview set of laboratories (available in: CDT from Set of Labs No. 5)) develop a
procedural-style program in C / C++, using own written functions.
Data processing in your program should be organized according to a given length
of input records based on memory allocation functions.
Your solution should:
1. use the pointers;
2. have to be presented in your report, emerging from the content of the problem
statement, in two versions:
A. The procedural version of your solution should be developed 1. using the
structures (declared by the keyword STRUC and typedef or some
UNION); 2. if necessary, nested structures (ore union) will be created; 3.
using the notations specific to pointers.
B. The presented solution will be unified through a header file that will be
called in the main function to allow the program to run, using the
following options regarding the user's choices:
2
฀ The version that will capitalize on your structure: should be
implemented in a dynamic Stack;
฀ The version that will capitalize on your structure: should be
implemented in a dynamic Queue.
The elaborated code will contain the functions to be called in the main, such as:
I. Stack / Queue creating, crossing, and displaying.
II. Insert an element into the Stack / Queue.
III. Search for an element by position or by value.
IV. Deleting an element from the Stack / Queue.
V. Registering the newly formed Stack / Queue after (adding/removing an
element into the Stack / Queue) in a *.* file (which will give the user the
possibility to record info, both in *.txt and binary mode), the address of which
will read from the keyboard (a function to read the full address of the file to be
registered will also be developed. The given file must be possible both to be
created, opened, reopened, but also to be deleted).

6B. Solving the problems using LINKED LISTS ADT, FILE data type
& UDT (user-defined data type) in C / C++
Average level
Task:
Develop a procedural-style program in C / C++, using own written functions. Data
processing in your program should be organized according to a given length of input
records based on memory allocation functions.
Your solution should:
D. use the pointers;
E. have to be presented in your report, emerging from the content of the
problem statement.
F. To draw the block diagram corresponding to the solved problem.
Attention! The grading will take into account the originality, complexity,
and quality of the proposed and solved changes.
3
Using the custom data type (CDT) (structs and structs members (both the initial
ones and the output dataset)), that have been assigned to you in problem set no.5 (see
2
the preview set of laboratories (available in: CDT from Set of Labs No. 5)) develop a
procedural-style program in C / C++, using own written functions.
Data processing in your program should be organized according to a given length
of input records based on memory allocation functions.
Your solution should:
1. use the pointers;
2. have to be presented in your report, emerging from the content of the problem
statement, in two versions:
C. The procedural version of your solution should be developed 1. using the
structures (declared by the keyword STRUC and typedef or some
UNION); 2. if necessary, nested structures (ore union) will be created; 3.
using the notations specific to pointers.
D. The presented solution will be unified through a header file that will be
called in the main function to allow the program to run, using the
following options regarding the user's choices:
฀ The version that will capitalize on your structure: should be
implemented in a SIMPLE LINKED LIST;
฀ The version that will capitalize on your structure: should be
implemented in a DOUBLE LINKED LIST;
฀ The version that will capitalize on your structure: should be
implemented in a CIRCULAR LINKED LIST.
The elaborated code will contain the functions to be called in the main, such as:
VI. SIMPLE / DOUBLE LINKED LIST / CURCULAR LINKED LIST
creating, crossing, and displaying.
VII. Insert a node element into the SIMPLE / DOUBLE LINKED LIST /
CURCULAR LINKED LIST (the position and the value of the inserting
node will be read from the keyboard).
VIII. Search for an element by position or by value.
IX. Deleting a node element from the SIMPLE / DOUBLE LINKED LIST /
CURCULAR LINKED LIST
X. Registering the newly formed SIMPLE / DOUBLE LINKED LIST /
CURCULAR LINKED LIST after (adding/removing a node element into
4
the SIMPLE / DOUBLE LINKED LIST / CIRCULAR LINKED LIST)
in a *.* file (which will give the user the possibility to record info, both in
*.txt and binary mode), the address of which will read from the keyboard (a
function to read the full address of the file to be registered will also be
developed. The given file must be possible both to be created, opened,
reopened, but also to be deleted).

Program Code

It is really big code, totally it has about 2000 lines of code, so I decided only to put
here main functions (900 lines of code) from part I and II, another code I put on else with
this rapport, you can open it.

Part I
#include <stdio.h>

#include <stdlib.h>

#include "queue.h"

3
#include "stack.h"

#include "train.h"

// Function to read the full address of the file from the user

void readFilePath(char *filePath) {

printf("Enter the full address of the file (including the file name and
extension): ");

scanf("%s", filePath);

// Function to save the contents of the Stack into a file in text mode

void saveStackToFileText(Stack *stack, char *filePath) {

FILE *file = fopen(filePath, "w");

if (file == NULL) {

printf("Error: Unable to open file for writing.\n");

return;

StackNode *current = stack->top;

while (current != NULL) {

fprintf(file, "%d %s %02d:%02d %02d:%02d %s\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current->data.departureMinute,

current->data.arrivalHour, current->data.arrivalMinute,

current->data.circulationDays);

current = current->next;

fclose(file);

printf("Stack contents saved to file '%s' successfully.\n", filePath);

4
// Function to save the contents of the Queue into a file in text mode

void saveQueueToFileText(Queue *queue, char *filePath) {

FILE *file = fopen(filePath, "w");

if (file == NULL) {

printf("Error: Unable to open file for writing.\n");

return;

QueueNode *current = queue->front;

while (current != NULL) {

fprintf(file, "%d %s %02d:%02d %02d:%02d %s\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current->data.departureMinute,

current->data.arrivalHour, current->data.arrivalMinute,

current->data.circulationDays);

current = current->next;

fclose(file);

printf("Queue contents saved to file '%s' successfully.\n", filePath);

// Function to save the contents of the Stack into a file in binary mode

void saveStackToFileBinary(Stack *stack, char *filePath) {

FILE *file = fopen(filePath, "wb");

if (file == NULL) {

printf("Error: Unable to open file for writing.\n");

return;

StackNode *current = stack->top;

5
while (current != NULL) {

fwrite(&current->data, sizeof(Train), 1, file);

current = current->next;

fclose(file);

printf("Stack contents saved to file '%s' successfully in binary mode.\n",


filePath);

// Function to save the contents of the Queue into a file in binary mode

void saveQueueToFileBinary(Queue *queue, char *filePath) {

FILE *file = fopen(filePath, "wb");

if (file == NULL) {

printf("Error: Unable to open file for writing.\n");

return;

QueueNode *current = queue->front;

while (current != NULL) {

fwrite(&current->data, sizeof(Train), 1, file);

current = current->next;

fclose(file);

printf("Queue contents saved to file '%s' successfully in binary mode.\n",


filePath);

int main() {

int choice, choice2, numberT, saving;

char filePath[100];
6
char mode[10];

printf("Do you want to write everething by your own(1) or to read data from
'result.txt'(2)?\n");

scanf("%d", &choice);

printf("Do you want to do it in queue(1) or in stack(2)?\n");

scanf("%d", &choice2);

printf("Save file as txt(1) or binary(2)\n");

scanf("%d", &saving);

//In my case: C:\Users\User\Desktop\SDA\laboratory_6\part_1\output.txt /


or .bin if for binary

readFilePath(filePath);

if(choice==1){

printf("Enter the number of trains: ");

scanf("%d", &numberT);

if(choice2==1){

Queue* queue = createQueue();

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

Train newTrain;

printf("Enter details for Train %d:\n", i+1);

printf("Train Number: ");

scanf("%d", &newTrain.trainNumber);

printf("Destination: ");

scanf("%s", newTrain.destination);

printf("Departure Hour: ");

scanf("%d", &newTrain.departureHour);
7
printf("Departure Minute: ");

scanf("%d", &newTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &newTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &newTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", newTrain.circulationDays);

enqueue(queue, newTrain);

printf("Trains enqueued successfully!\n\n");

displayQueue(queue);

int k=0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the Queue.\n3 - Display queue\n4 - Search for an element by position or by value.\
n5 - Deleting an element from the Queue");

while(k!=1){

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);
8
printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

enqueue(queue, nextTrain);

break;

case 3:

displayQueue(queue);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

QueueNode *current = queue->front;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination, current->data.departureHour, current-
>data.departureMinute, current->data.arrivalHour, current->data.arrivalMinute, current-
>data.circulationDays);

break;

current = current->next;

position++;

if (current == NULL) {

9
printf("Train with Train Number %d not found in the
queue.\n", searchTrainNumber);

break;

case 5:

dequeue(queue);

break;

if(saving==1){

saveQueueToFileText(queue, filePath);

} else{

saveQueueToFileBinary(queue, filePath);

else if(choice2 == 2){

Stack* stack = createStack();

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

Train newTrain;

printf("Enter details for Train %d:\n", i+1);

printf("Train Number: ");

scanf("%d", &newTrain.trainNumber);

printf("Destination: ");

scanf("%s", newTrain.destination);

printf("Departure Hour: ");

scanf("%d", &newTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &newTrain.departureMinute);

printf("Arrival Hour: ");

10
scanf("%d", &newTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &newTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", newTrain.circulationDays);

push(stack, newTrain);

printf("Trains pushed onto stack successfully!\n\n");

displayStack(stack);

int k=0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the Stack.\n3 - Display stack\n4 - Search for an element by position or by value.\
n5 - Deleting an element from the Stack");

while(k!=1){

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);
11
printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

push(stack, nextTrain);

break;

case 3:

displayStack(stack);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber;

scanf("%d", &searchTrainNumber);

Stack* tempStack = createStack();

Train foundTrain;

while (!isEmptyStack(stack)) {

Train currentTrain = peek(stack);

if (currentTrain.trainNumber == searchTrainNumber) {

foundTrain = currentTrain;

printf("Train found in the stack: %d %s %02d:%02d


%02d:%02d %s\n", currentTrain.trainNumber, currentTrain.destination,
currentTrain.departureHour, currentTrain.departureMinute, currentTrain.arrivalHour,
currentTrain.arrivalMinute, currentTrain.circulationDays);

break;

push(tempStack, currentTrain);

pop(stack);

12
while (!isEmptyStack(tempStack)) {

Train tempTrain = peek(tempStack);

push(stack, tempTrain);

pop(tempStack);

if (isEmptyStack(stack)) {

printf("Train with Train Number %d not found in the


stack.\n", searchTrainNumber);

break;

case 5:

pop(stack);

break;

if(saving==1){

saveStackToFileText(stack, filePath);

} else{

saveStackToFileBinary(stack, filePath);

else{

printf("Invalid choice. Exiting program.\n");

13
} else if (choice==2) {

FILE* file = fopen("output/result.txt", "r");

if (file == NULL) {

printf("Error opening file.\n");

return 1;

if(choice2==1){

Queue* queue = createQueue();

Train newTrain;

while (fscanf(file, "%d %s %d:%d %d:%d %s", &newTrain.trainNumber,


newTrain.destination, &newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute, newTrain.circulationDays) == 7) {

enqueue(queue, newTrain);

fclose(file);

printf("Trains enqueued successfully from 'result.txt'!\n\n");

displayQueue(queue);

int k=0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the Queue.\n3 - Display queue\n4 - Search for an element by position or by value.\
n5 - Deleting an element from the Queue");

while(k!=1){

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

14
printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

enqueue(queue, nextTrain);

break;

case 3:

displayQueue(queue);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

QueueNode *current = queue->front;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination, current->data.departureHour, current-
>data.departureMinute, current->data.arrivalHour, current->data.arrivalMinute, current-
>data.circulationDays);

15
break;

current = current->next;

position++;

if (current == NULL) {

printf("Train with Train Number %d not found in the


queue.\n", searchTrainNumber);

break;

case 5:

dequeue(queue);

break;

if(saving==1){

saveQueueToFileText(queue, filePath);

} else{

saveQueueToFileBinary(queue, filePath);

else if (choice2==2) {

Stack* stack = createStack();

Train newTrain;

while (fscanf(file, "%d %s %d:%d %d:%d %s", &newTrain.trainNumber,


newTrain.destination, &newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute, newTrain.circulationDays) == 7) {

push(stack, newTrain);

fclose(file);

16
printf("Trains pushed onto stack successfully from
'output/result.txt'!\n\n");

displayStack(stack);

int k=0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the Stack.\n3 - Display stack\n4 - Search for an element by position or by value.\
n5 - Deleting an element from the Stack");

while(k!=1){

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

push(stack, nextTrain);

break;

case 3:

displayStack(stack);

17
break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber;

scanf("%d", &searchTrainNumber);

Stack* tempStack = createStack();

Train foundTrain;

while (!isEmptyStack(stack)) {

Train currentTrain = peek(stack);

if (currentTrain.trainNumber == searchTrainNumber) {

foundTrain = currentTrain;

printf("Train found in the stack: %d %s %02d:%02d


%02d:%02d %s\n", currentTrain.trainNumber, currentTrain.destination,
currentTrain.departureHour, currentTrain.departureMinute, currentTrain.arrivalHour,
currentTrain.arrivalMinute, currentTrain.circulationDays);

break;

push(tempStack, currentTrain);

pop(stack);

while (!isEmptyStack(tempStack)) {

Train tempTrain = peek(tempStack);

push(stack, tempTrain);

pop(tempStack);

if (isEmptyStack(stack)) {

printf("Train with Train Number %d not found in the


stack.\n", searchTrainNumber);

break;

case 5:

pop(stack);

18
break;

if(saving==1){

saveStackToFileText(stack, filePath);

} else{

saveStackToFileBinary(stack, filePath);

else{

printf("Invalid choice. Exiting program.\n");

} else{

printf("Invalid choice. Exiting program.\n");

return 0;

Part II
#include <stdio.h>

#include <stdlib.h>

#include "simple.h"

#include "double.h"

#include "circular.h"

#include "train.h"

// Function to read the full address of the file from the user

void readFilePath(char *filePath) {

printf("Enter the full address of the file (including the file name and
extension): ");

scanf("%s", filePath);

19
}

int main() {

int choice, numberT, saving, choice2;

char filePath[100];

printf("Do you want to write everything by yourself (1) or to read data from
'result.txt' (2)?\n");

scanf("%d", &choice);

printf("Save file as txt (1) or binary (2)\n");

scanf("%d", &saving);

printf("Here you need to choose list from 1 to 3(1-simple; 2-double; 3-


circular)\n");

scanf("%d", &choice2);

//In my case: C:\Users\User\Desktop\SDA\laboratory_6\part_2\output.txt /


or .bin if for binary

readFilePath(filePath);

if(choice2==1){

if (choice == 1) {

printf("Enter the number of trains: ");

scanf("%d", &numberT);

SimpleNode* list = NULL;

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

Train newTrain;

printf("Enter details for Train %d:\n", i + 1);

printf("Train Number: ");

20
scanf("%d", &newTrain.trainNumber);

printf("Destination: ");

scanf("%s", newTrain.destination);

printf("Departure Hour: ");

scanf("%d", &newTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &newTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &newTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &newTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", newTrain.circulationDays);

list = insertSimpleNode(list, newTrain);

printf("Trains added to the list successfully!\n\n");

displaySimpleList(list);

int k = 0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");

while (k != 1) {

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");


21
printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

list = insertSimpleNode(list, nextTrain);

break;

case 3:

displaySimpleList(list);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

SimpleNode* current = list;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,
22
current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

current = current->next;

position++;

if (current == NULL) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteSimpleNode(&list);

break;

if (saving == 1) {

saveSimpleListToFileText(list, filePath);

} else {

saveSimpleListToFileBinary(list, filePath);

freeSimpleList(list);

} else if (choice == 2) {

FILE* file = fopen("output/result.txt", "r");

if (file == NULL) {

printf("Error opening file.\n");

return 1;
23
}

SimpleNode* list = NULL;

Train newTrain;

while (fscanf(file, "%d %s %d:%d %d:%d %s", &newTrain.trainNumber,


newTrain.destination,

&newTrain.departureHour, &newTrain.departureMinute,

&newTrain.arrivalHour, &newTrain.arrivalMinute,

newTrain.circulationDays) == 7) {

list = insertSimpleNode(list, newTrain);

fclose(file);

printf("Trains added to the list successfully from


'output/result.txt'!\n\n");

displaySimpleList(list);

int k = 0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");

while (k != 1) {

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

24
printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

list = insertSimpleNode(list, nextTrain);

break;

case 3:

displaySimpleList(list);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

SimpleNode* current = list;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,

current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

25
}

current = current->next;

position++;

if (current == NULL) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteSimpleNode(&list);

break;

if (saving == 1) {

saveSimpleListToFileText(list, filePath);

} else {

saveSimpleListToFileBinary(list, filePath);

freeSimpleList(list);

} else {

printf("Invalid choice. Exiting program.\n");

else if(choice2==2){

if (choice == 1) {

printf("Enter the number of trains: ");

scanf("%d", &numberT);

26
DoubleNode* list = NULL;

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

Train newTrain;

printf("Enter details for Train %d:\n", i + 1);

printf("Train Number: ");

scanf("%d", &newTrain.trainNumber);

printf("Destination: ");

scanf("%s", newTrain.destination);

printf("Departure Hour: ");

scanf("%d", &newTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &newTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &newTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &newTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", newTrain.circulationDays);

list = insertDoubleNode(list, newTrain);

printf("Trains added to the list successfully!\n\n");

displayDoubleList(list);

int k = 0;

printf("\nWhat do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");
27
while (k != 1) {

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

list = insertDoubleNode(list, nextTrain);

break;

case 3:

displayDoubleList(list);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

28
DoubleNode* current = list;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,

current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

current = current->next;

position++;

if (current == NULL) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteDoubleNode(&list);

break;

if (saving == 1) {

saveDoubleListToFileText(list, filePath);

} else {

saveDoubleListToFileBinary(list, filePath);

29
freeDoubleList(list);

} else if (choice == 2) {

FILE* file = fopen("output/result.txt", "r");

if (file == NULL) {

printf("Error opening file.\n");

return 1;

DoubleNode* list = NULL;

Train newTrain;

while (fscanf(file, "%d %s %d:%d %d:%d %s", &newTrain.trainNumber,


newTrain.destination,

&newTrain.departureHour, &newTrain.departureMinute,

&newTrain.arrivalHour, &newTrain.arrivalMinute,

newTrain.circulationDays) == 7) {

list = insertDoubleNode(list, newTrain);

fclose(file);

printf("Trains added to the list successfully from


'output/result.txt'!\n\n");

displayDoubleList(list);

int k = 0;

printf("\nWhat do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");

while (k != 1) {

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

30
switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

list = insertDoubleNode(list, nextTrain);

break;

case 3:

displayDoubleList(list);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

DoubleNode* current = list;

int position = 1;

while (current != NULL) {

if (current->data.trainNumber == searchTrainNumber) {

31
printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-
>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,

current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

current = current->next;

position++;

if (current == NULL) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteDoubleNode(&list);

break;

if (saving == 1) {

saveDoubleListToFileText(list, filePath);

} else {

saveDoubleListToFileBinary(list, filePath);

freeDoubleList(list);

} else {

printf("Invalid choice. Exiting program.\n");

32
}

} else if(choice2==3){

if (choice == 1) {

printf("Enter the number of trains: ");

scanf("%d", &numberT);

CircularNode* head = NULL;

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

Train newTrain;

printf("Enter details for Train %d:\n", i + 1);

printf("Train Number: ");

scanf("%d", &newTrain.trainNumber);

printf("Destination: ");

scanf("%s", newTrain.destination);

printf("Departure Hour: ");

scanf("%d", &newTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &newTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &newTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &newTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", newTrain.circulationDays);

head = insertCircularNode(head, newTrain);

printf("Trains added to the list successfully!\n\n");

33
displayCircularList(head);

int k = 0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");

while (k != 1) {

Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

head = insertCircularNode(head, nextTrain);

break;

case 3:

displayCircularList(head);

break;
34
case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

CircularNode* current = head;

do {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,

current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

current = current->next;

} while (current != head);

if (current == head) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteCircularNode(&head);

break;

if (saving == 1) {

saveCircularListToFileText(head, filePath);

35
} else {

saveCircularListToFileBinary(head, filePath);

freeCircularList(head);

} else if (choice == 2) {

FILE* file = fopen("output/result.txt", "r");

if (file == NULL) {

printf("Error opening file.\n");

return 1;

CircularNode* head = NULL;

Train newTrain;

while (fscanf(file, "%d %s %d:%d %d:%d %s", &newTrain.trainNumber,


newTrain.destination,

&newTrain.departureHour, &newTrain.departureMinute,

&newTrain.arrivalHour, &newTrain.arrivalMinute,

newTrain.circulationDays) == 7) {

head = insertCircularNode(head, newTrain);

fclose(file);

printf("Trains added to the list successfully from


'output/result.txt'!\n\n");

displayCircularList(head);

int k = 0;

printf("What do you want to do?\n1 - Nothing\n2 - Insert an element


into the list.\n3 - Display list\n4 - Search for an element by position or by
value.\n5 - Deleting an element from the list");

while (k != 1) {

36
Train nextTrain;

printf("\nYour decision: ");

scanf("%d", &k);

switch (k) {

case 2:

printf("Enter details for the new Train:\n");

printf("Train Number: ");

scanf("%d", &nextTrain.trainNumber);

printf("Destination: ");

scanf("%s", nextTrain.destination);

printf("Departure Hour: ");

scanf("%d", &nextTrain.departureHour);

printf("Departure Minute: ");

scanf("%d", &nextTrain.departureMinute);

printf("Arrival Hour: ");

scanf("%d", &nextTrain.arrivalHour);

printf("Arrival Minute: ");

scanf("%d", &nextTrain.arrivalMinute);

printf("Circulation Days: ");

scanf("%s", nextTrain.circulationDays);

head = insertCircularNode(head, nextTrain);

break;

case 3:

displayCircularList(head);

break;

case 4:

printf("Enter Train Number to search: ");

int searchTrainNumber = 0;

scanf("%d", &searchTrainNumber);

CircularNode* current = head;

37
do {

if (current->data.trainNumber == searchTrainNumber) {

printf("%d %s %02d:%02d %02d:%02d %s\n\n", current-


>data.trainNumber, current->data.destination,

current->data.departureHour, current-
>data.departureMinute,

current->data.arrivalHour, current-
>data.arrivalMinute,

current->data.circulationDays);

break;

current = current->next;

} while (current != head);

if (current == head) {

printf("Train with Train Number %d not found in the list.\


n", searchTrainNumber);

break;

case 5:

deleteCircularNode(&head);

break;

if (saving == 1) {

saveCircularListToFileText(head, filePath);

} else {

saveCircularListToFileBinary(head, filePath);

freeCircularList(head);

} else {

38
printf("Invalid choice. Exiting program.\n");

}else{

printf("Invalid choice. Exiting program.\n");

return 0;

Results

Part I

39
Part II

40
41
Conclusions

In conclusion, the tasks provided entail the development of procedural-style programs


in C/C++, emphasizing the utilization of custom data types, memory allocation functions,
and pointers. Two distinct sets of challenges were presented, each focusing on different data
structures and implementation approaches.

The first set of tasks (A-F) required the creation of solutions using structures and
offered options for dynamic stacks or queues. Key functions included creating, traversing,
inserting, searching, deleting, and registering data structures in files.

On the other hand, the second set of tasks (D-X) mandated solutions implemented
using linked lists, including simple, double, and circular variations. Similar functions for

42
data structure manipulation and file handling were necessary, with an added emphasis on
linked list operations.

Throughout both sets of tasks, attention to detail, adherence to programming best


practices, and proficiency in memory management were critical. Proper documentation,
including clear explanations and block diagrams, was essential for communicating the
problem-solving approach effectively.

In summary, the completion of these tasks required a comprehensive understanding of


C/C++ programming concepts, data structures, memory management techniques, and file
handling operations. By meeting the outlined requirements and implementing efficient
algorithms, the resulting programs could effectively manage and process data, demonstrating
originality, complexity, and quality in the solutions provided.

43

You might also like