Professional Documents
Culture Documents
Report6 Belih Dmitrii.
Report6 Belih Dmitrii.
Belih Dmitrii
Group: FAF-232
Report
Laboratory Work No.6
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
FCIM Faculty, UTM
Chisinau – 2024
6B. Solving the problems using LINKED LISTS ADT, FILE data type
& UDT (user-defined data type) in C / C++
Average level
2
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
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
3
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
Main.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#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,
4
current->data.circulationDays);
current = current->next;
fclose(file);
// 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
5
{
if (file == NULL)
return;
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;
6
fclose(file);
int main()
char filePath[100];
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;
7
printf("Train Number: ");
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);
enqueue(queue, newTrain);
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;
scanf("%d", &k);
switch (k)
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
8
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)
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)
Train newTrain;
scanf("%d", &newTrain.trainNumber);
printf("Destination: ");
scanf("%s", newTrain.destination);
scanf("%d", &newTrain.departureHour);
scanf("%d", &newTrain.departureMinute);
scanf("%d", &newTrain.arrivalHour);
10
printf("Arrival Minute: ");
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
push(stack, newTrain);
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;
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);
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);
while (!isEmptyStack(tempStack))
push(stack, tempTrain);
pop(tempStack);
12
if (isEmptyStack(stack))
break;
case 5:
pop(stack);
break;
if (saving == 1)
saveStackToFileText(stack, filePath);
else
saveStackToFileBinary(stack, filePath);
else
else if (choice == 2)
if (file == NULL)
return 1;
if (choice2 == 1)
{
13
Queue *queue = createQueue();
Train newTrain;
enqueue(queue, newTrain);
fclose(file);
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;
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);
14
printf("Circulation Days: ");
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)
break;
case 5:
dequeue(queue);
break;
if (saving == 1)
15
{
saveQueueToFileText(queue, filePath);
else
saveQueueToFileBinary(queue, filePath);
else if (choice2 == 2)
Train newTrain;
push(stack, newTrain);
fclose(file);
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;
scanf("%d", &k);
switch (k)
case 2:
16
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);
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;
17
break;
push(tempStack, currentTrain);
pop(stack);
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
18
printf("Invalid choice. Exiting program.\n");
else
/*
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
return 0;
Queue.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
Queue* createQueue() {
if (queue) {
return queue;
if (newNode) {
19
newNode->data = data;
newNode->next = NULL;
if (isEmptyQueue(queue)) {
} else {
queue->rear->next = newNode;
queue->rear = newNode;
} else {
if (isEmptyQueue(queue)) {
return emptyTrain;
queue->front = temp->next;
if (queue->front == NULL) {
queue->rear = NULL;
free(temp);
return dequeuedData;
if (isEmptyQueue(queue)) {
printf("Queue is empty.\n");
20
return;
printf("Queue contents:\n");
printf("Train Details:\n");
current = current->next;
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include "train.h"
Train data;
} QueueNode;
typedef struct {
QueueNode* front;
QueueNode* rear;
} Queue;
Queue* createQueue();
21
#endif
Stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
Stack* createStack() {
if (stack) {
stack->top = NULL;
return stack;
if (newNode) {
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
} else {
if (isEmptyStack(stack)) {
return emptyTrain;
stack->top = temp->next;
22
free(temp);
return poppedData;
if (isEmptyStack(stack)) {
printf("Stack is empty.\n");
return;
printf("Stack contents:\n");
printf("Train Details:\n");
current = current->next;
if (isEmptyStack(stack)) {
return dummyTrain;
return stack->top->data;
Stack.h
#ifndef STACK_H
23
#define STACK_H
#include "train.h"
Train data;
} StackNode;
typedef struct {
StackNode* top;
} Stack;
Stack* createStack();
#endif
Train.h
#ifndef TRAIN_H
#define TRAIN_H
typedef struct {
int trainNumber;
char destination[50];
int departureHour;
int departureMinute;
int arrivalHour;
int arrivalMinute;
char circulationDays[20];
24
} Train;
#endif
Part II
Main Function
#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);
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);
25
//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) {
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);
displaySimpleList(list);
26
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;
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);
27
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:
deleteSimpleNode(&list);
break;
if (saving == 1) {
saveSimpleListToFileText(list, filePath);
} else {
saveSimpleListToFileBinary(list, filePath);
freeSimpleList(list);
} else if (choice == 2) {
if (file == NULL) {
return 1;
28
SimpleNode* list = NULL;
Train newTrain;
&newTrain.departureHour, &newTrain.departureMinute,
&newTrain.arrivalHour, &newTrain.arrivalMinute,
newTrain.circulationDays) == 7) {
fclose(file);
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;
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);
29
printf("Circulation Days: ");
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;
current = current->next;
position++;
if (current == NULL) {
break;
case 5:
deleteSimpleNode(&list);
break;
30
if (saving == 1) {
saveSimpleListToFileText(list, filePath);
} else {
saveSimpleListToFileBinary(list, filePath);
freeSimpleList(list);
} else {
else if(choice2==2){
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);
31
list = insertDoubleNode(list, newTrain);
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;
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;
32
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;
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);
33
} 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;
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;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
printf("Destination: ");
scanf("%s", nextTrain.destination);
scanf("%d", &nextTrain.departureHour);
34
printf("Departure Minute: ");
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) {
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:
35
deleteDoubleNode(&list);
break;
if (saving == 1) {
saveDoubleListToFileText(list, filePath);
} else {
saveDoubleListToFileBinary(list, filePath);
freeDoubleList(list);
} else {
} 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);
36
printf("Arrival Minute: ");
scanf("%d", &newTrain.arrivalMinute);
scanf("%s", newTrain.circulationDays);
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;
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);
37
scanf("%s", nextTrain.circulationDays);
break;
case 3:
displayCircularList(head);
break;
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);
} else {
saveCircularListToFileBinary(head, filePath);
38
}
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;
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;
scanf("%d", &k);
switch (k) {
case 2:
scanf("%d", &nextTrain.trainNumber);
39
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);
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) {
40
break;
case 5:
deleteCircularNode(&head);
break;
if (saving == 1) {
saveCircularListToFileText(head, filePath);
} else {
saveCircularListToFileBinary(head, filePath);
freeCircularList(head);
} else {
}else{
return 0;
Circular.c
#include <stdio.h>
#include <stdlib.h>
#include "circular.h"
if (newNode == NULL) {
exit(EXIT_FAILURE);
41
}
newNode->data = newData;
newNode->next = NULL;
return newNode;
if (head == NULL) {
return newNode;
current = current->next;
current->next = newNode;
newNode->next = head;
return head;
if (head == NULL) {
printf("List is empty.\n");
return;
printf("List contents:\n");
do {
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
42
} while (current != head);
if (head == NULL) {
printf("List is empty.\n");
return;
int searchTrainNumber;
scanf("%d", &searchTrainNumber);
int position = 1;
do {
if (current->data.trainNumber == searchTrainNumber) {
return;
current = current->next;
position++;
if (*head == NULL) {
printf("List is empty.\n");
return;
int position;
scanf("%d", &position);
if (position < 1) {
printf("Invalid position.\n");
43
return;
int count = 1;
do {
if (count == position) {
free(current);
*head = NULL;
return;
lastNode = lastNode->next;
*head = (*head)->next;
lastNode->next = *head;
free(current);
return;
previous->next = current->next;
free(current);
return;
previous = current;
current = current->next;
count++;
if (file == NULL) {
return;
if (head != NULL) {
do {
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
fclose(file);
if (file == NULL) {
return;
if (head != NULL) {
do {
current = current->next;
}
45
fclose(file);
printf("Circular linked list contents saved to file '%s' successfully in binary mode.\n", filePath);
if (head == NULL) {
return;
CircularNode* temp;
do {
temp = current;
current = current->next;
free(temp);
Circular.h
#ifndef CIRCULAR_H
#define CIRCULAR_H
#include "train.h"
Train data;
} CircularNode;
46
void deleteCircularNode(CircularNode** head);
#endif
Double.c
#include <stdio.h>
#include <stdlib.h>
#include "double.h"
if (newNode == NULL) {
exit(EXIT_FAILURE);
newNode->data = newData;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
if (head == NULL) {
return newNode;
current = current->next;
47
current->next = newNode;
newNode->prev = current;
return head;
if (head == NULL) {
printf("List is empty.\n");
return;
printf("List contents:\n");
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
if (head == NULL) {
printf("List is empty.\n");
return;
int searchTrainNumber;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
48
return;
current = current->next;
position++;
if (*head == NULL) {
printf("List is empty.\n");
return;
int position;
scanf("%d", &position);
if (position < 1) {
printf("Invalid position.\n");
return;
current = current->next;
if (current == NULL) {
return;
if (current->prev != NULL) {
current->prev->next = current->next;
if (current->next != NULL) {
current->next->prev = current->prev;
if (current == *head) {
*head = current->next;
49
}
free(current);
if (file == NULL) {
return;
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
fclose(file);
if (file == NULL) {
return;
50
fwrite(¤t->data, sizeof(Train), 1, file);
current = current->next;
fclose(file);
printf("Doubly linked list contents saved to file '%s' successfully in binary mode.\n", filePath);
current = current->next;
free(temp);
Double.h
#ifndef DOUBLE_H
#define DOUBLE_H
#include "train.h"
Train data;
} DoubleNode;
51
void searchDoubleList(DoubleNode* head);
#endif
Simple.c
#include <stdio.h>
#include <stdlib.h>
#include "simple.h"
if (newNode == NULL) {
exit(EXIT_FAILURE);
newNode->data = newData;
newNode->next = NULL;
return newNode;
if (head == NULL) {
return newNode;
current = current->next;
52
current->next = newNode;
return head;
if (head == NULL) {
printf("List is empty.\n");
return;
printf("List contents:\n");
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
if (head == NULL) {
printf("List is empty.\n");
return;
int searchTrainNumber;
scanf("%d", &searchTrainNumber);
int position = 1;
if (current->data.trainNumber == searchTrainNumber) {
return;
53
}
current = current->next;
position++;
if (*head == NULL) {
printf("List is empty.\n");
return;
int position;
scanf("%d", &position);
if (position < 1) {
printf("Invalid position.\n");
return;
if (position == 1) {
*head = (*head)->next;
free(temp);
return;
previous = current;
current = current->next;
if (current == NULL) {
return;
}
54
previous->next = current->next;
free(current);
if (file == NULL) {
return;
current->data.departureHour, current->data.departureMinute,
current->data.arrivalHour, current->data.arrivalMinute,
current->data.circulationDays);
current = current->next;
fclose(file);
if (file == NULL) {
return;
55
fwrite(¤t->data, sizeof(Train), 1, file);
current = current->next;
fclose(file);
printf("Simple linked list contents saved to file '%s' successfully in binary mode.\n", filePath);
current = current->next;
free(temp);
Simple.h
#ifndef SIMPLE_H
#define SIMPLE_H
#include "train.h"
Train data;
} SimpleNode;
56
void freeSimpleList(SimpleNode* head);
#endif
Train.h
#ifndef TRAIN_H
#define TRAIN_H
typedef struct {
int trainNumber;
char destination[50];
int departureHour;
int departureMinute;
int arrivalHour;
int arrivalMinute;
char circulationDays[20];
} Train;
#endif
Results
Part I
Stack
57
58
Queue
59
60
61
Part II
62
63
64
65
66
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
67
data structure manipulation and file handling were necessary, with an added emphasis on
linked list operations.
68