Report 6. Titerez Vladislav
Report 6. Titerez Vladislav
Titerez Vladislav
Group: FAF-233
Report
Laboratory Work No.6
Chisinau – 2024
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
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
if (file == NULL) {
return;
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
fclose(file);
4
// Function to save the contents of the Queue into a file in text mode
if (file == NULL) {
return;
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
fclose(file);
// Function to save the contents of the Stack into a file in binary mode
if (file == NULL) {
return;
5
while (current != NULL) {
current = current->next;
fclose(file);
// Function to save the contents of the Queue into a file in binary mode
if (file == NULL) {
return;
current = current->next;
fclose(file);
int main() {
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);
scanf("%d", &choice2);
scanf("%d", &saving);
readFilePath(filePath);
if(choice==1){
scanf("%d", &numberT);
if(choice2==1){
Train newTrain;
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
7
printf("Departure Minute: ");
scanf("%d", &newTrain.departureMinute);
scanf("%d", &newTrain.arrivalHour);
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
enqueue(queue, newTrain);
displayQueue(queue);
int k=0;
while(k!=1){
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
8
printf("Departure Minute: ");
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
enqueue(queue, nextTrain);
break;
case 3:
displayQueue(queue);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
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);
Train newTrain;
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
scanf("%d", &newTrain.departureMinute);
10
scanf("%d", &newTrain.arrivalHour);
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
push(stack, newTrain);
displayStack(stack);
int k=0;
while(k!=1){
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
11
printf("Arrival Hour: ");
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
push(stack, nextTrain);
break;
case 3:
displayStack(stack);
break;
case 4:
int searchTrainNumber;
scanf("%d", &searchTrainNumber);
Train foundTrain;
while (!isEmptyStack(stack)) {
if (currentTrain.trainNumber == searchTrainNumber) {
foundTrain = currentTrain;
break;
push(tempStack, currentTrain);
pop(stack);
12
while (!isEmptyStack(tempStack)) {
push(stack, tempTrain);
pop(tempStack);
if (isEmptyStack(stack)) {
break;
case 5:
pop(stack);
break;
if(saving==1){
saveStackToFileText(stack, filePath);
} else{
saveStackToFileBinary(stack, filePath);
else{
13
} else if (choice==2) {
if (file == NULL) {
return 1;
if(choice2==1){
Train newTrain;
enqueue(queue, newTrain);
fclose(file);
displayQueue(queue);
int k=0;
while(k!=1){
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
14
printf("Train Number: ");
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
enqueue(queue, nextTrain);
break;
case 3:
displayQueue(queue);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
15
break;
current = current->next;
position++;
if (current == NULL) {
break;
case 5:
dequeue(queue);
break;
if(saving==1){
saveQueueToFileText(queue, filePath);
} else{
saveQueueToFileBinary(queue, filePath);
else if (choice2==2) {
Train newTrain;
push(stack, newTrain);
fclose(file);
16
printf("Trains pushed onto stack successfully from
'output/result.txt'!\n\n");
displayStack(stack);
int k=0;
while(k!=1){
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
push(stack, nextTrain);
break;
case 3:
displayStack(stack);
17
break;
case 4:
int searchTrainNumber;
scanf("%d", &searchTrainNumber);
Train foundTrain;
while (!isEmptyStack(stack)) {
if (currentTrain.trainNumber == searchTrainNumber) {
foundTrain = currentTrain;
break;
push(tempStack, currentTrain);
pop(stack);
while (!isEmptyStack(tempStack)) {
push(stack, tempTrain);
pop(tempStack);
if (isEmptyStack(stack)) {
break;
case 5:
pop(stack);
18
break;
if(saving==1){
saveStackToFileText(stack, filePath);
} else{
saveStackToFileBinary(stack, filePath);
else{
} else{
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
printf("Enter the full address of the file (including the file name and
extension): ");
scanf("%s", filePath);
19
}
int main() {
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);
scanf("%d", &saving);
scanf("%d", &choice2);
readFilePath(filePath);
if(choice2==1){
if (choice == 1) {
scanf("%d", &numberT);
Train newTrain;
20
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
scanf("%d", &newTrain.departureMinute);
scanf("%d", &newTrain.arrivalHour);
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
displaySimpleList(list);
int k = 0;
while (k != 1) {
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displaySimpleList(list);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
current->data.departureHour, current-
>data.departureMinute,
22
current->data.arrivalHour, current-
>data.arrivalMinute,
current->data.circulationDays);
break;
current = current->next;
position++;
if (current == NULL) {
break;
case 5:
deleteSimpleNode(&list);
break;
if (saving == 1) {
saveSimpleListToFileText(list, filePath);
} else {
saveSimpleListToFileBinary(list, filePath);
freeSimpleList(list);
} else if (choice == 2) {
if (file == NULL) {
return 1;
23
}
Train newTrain;
&newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute,
newTrain.circulationDays) == 7) {
fclose(file);
displaySimpleList(list);
int k = 0;
while (k != 1) {
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
24
printf("Departure Hour: ");
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displaySimpleList(list);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
current->data.departureHour, current-
>data.departureMinute,
current->data.arrivalHour, current-
>data.arrivalMinute,
current->data.circulationDays);
break;
25
}
current = current->next;
position++;
if (current == NULL) {
break;
case 5:
deleteSimpleNode(&list);
break;
if (saving == 1) {
saveSimpleListToFileText(list, filePath);
} else {
saveSimpleListToFileBinary(list, filePath);
freeSimpleList(list);
} else {
else if(choice2==2){
if (choice == 1) {
scanf("%d", &numberT);
26
DoubleNode* list = NULL;
Train newTrain;
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
scanf("%d", &newTrain.departureMinute);
scanf("%d", &newTrain.arrivalHour);
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
displayDoubleList(list);
int k = 0;
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displayDoubleList(list);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
28
DoubleNode* current = list;
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
current->data.departureHour, current-
>data.departureMinute,
current->data.arrivalHour, current-
>data.arrivalMinute,
current->data.circulationDays);
break;
current = current->next;
position++;
if (current == NULL) {
break;
case 5:
deleteDoubleNode(&list);
break;
if (saving == 1) {
saveDoubleListToFileText(list, filePath);
} else {
saveDoubleListToFileBinary(list, filePath);
29
freeDoubleList(list);
} else if (choice == 2) {
if (file == NULL) {
return 1;
Train newTrain;
&newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute,
newTrain.circulationDays) == 7) {
fclose(file);
displayDoubleList(list);
int k = 0;
while (k != 1) {
Train nextTrain;
scanf("%d", &k);
30
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displayDoubleList(list);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
int position = 1;
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) {
break;
case 5:
deleteDoubleNode(&list);
break;
if (saving == 1) {
saveDoubleListToFileText(list, filePath);
} else {
saveDoubleListToFileBinary(list, filePath);
freeDoubleList(list);
} else {
32
}
} else if(choice2==3){
if (choice == 1) {
scanf("%d", &numberT);
Train newTrain;
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
scanf("%d", &newTrain.departureMinute);
scanf("%d", &newTrain.arrivalHour);
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
33
displayCircularList(head);
int k = 0;
while (k != 1) {
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displayCircularList(head);
break;
34
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
do {
if (current->data.trainNumber == searchTrainNumber) {
current->data.departureHour, current-
>data.departureMinute,
current->data.arrivalHour, current-
>data.arrivalMinute,
current->data.circulationDays);
break;
current = current->next;
if (current == head) {
break;
case 5:
deleteCircularNode(&head);
break;
if (saving == 1) {
saveCircularListToFileText(head, filePath);
35
} else {
saveCircularListToFileBinary(head, filePath);
freeCircularList(head);
} else if (choice == 2) {
if (file == NULL) {
return 1;
Train newTrain;
&newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute,
newTrain.circulationDays) == 7) {
fclose(file);
displayCircularList(head);
int k = 0;
while (k != 1) {
36
Train nextTrain;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
scanf("%d", &nextTrain.departureMinute);
scanf("%d", &nextTrain.arrivalHour);
scanf("%d", &nextTrain.arrivalMinute);
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displayCircularList(head);
break;
case 4:
int searchTrainNumber = 0;
scanf("%d", &searchTrainNumber);
37
do {
if (current->data.trainNumber == searchTrainNumber) {
current->data.departureHour, current-
>data.departureMinute,
current->data.arrivalHour, current-
>data.arrivalMinute,
current->data.circulationDays);
break;
current = current->next;
if (current == head) {
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{
return 0;
Results
Part I
39
Part II
40
41
Conclusions
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.
43