Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 68

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

Belih Dmitrii
Group: FAF-232

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

The purpose of the laboratory work


1
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.
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

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

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,

4
current->data.circulationDays);

current = current->next;

fclose(file);

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

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

5
{

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

if (file == NULL)

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

return;

StackNode *current = stack->top;

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;

6
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];

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

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

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: ");
8
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);

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: ");

scanf("%d", &newTrain.arrivalHour);
10
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);

printf("Arrival Hour: ");

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

printf("Arrival Minute: ");

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

printf("Circulation Days: ");


11
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);

while (!isEmptyStack(tempStack))

Train tempTrain = peek(tempStack);

push(stack, tempTrain);

pop(tempStack);

12
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");

else if (choice == 2)

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

if (file == NULL)

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

return 1;

if (choice2 == 1)

{
13
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");

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

14
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)

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


searchTrainNumber);

break;

case 5:

dequeue(queue);

break;

if (saving == 1)

15
{

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

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");

16
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);

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

17
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);

break;

if (saving == 1)

saveStackToFileText(stack, filePath);

else

saveStackToFileBinary(stack, filePath);

else

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

else

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

/*

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).*/

return 0;

Queue.c

#include <stdio.h>

#include <stdlib.h>

#include "queue.h"

Queue* createQueue() {

Queue* queue = (Queue*)malloc(sizeof(Queue));

if (queue) {

queue->front = queue->rear = NULL;

return queue;

void enqueue(Queue* queue, Train data) {

QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));

if (newNode) {

19
newNode->data = data;

newNode->next = NULL;

if (isEmptyQueue(queue)) {

queue->front = queue->rear = newNode;

} else {

queue->rear->next = newNode;

queue->rear = newNode;

} else {

printf("Memory allocation failed. Cannot enqueue into queue.\n");

Train dequeue(Queue* queue) {

if (isEmptyQueue(queue)) {

printf("Queue underflow. Cannot dequeue from an empty queue.\n");

Train emptyTrain = {0};

return emptyTrain;

QueueNode* temp = queue->front;

Train dequeuedData = temp->data;

queue->front = temp->next;

if (queue->front == NULL) {

queue->rear = NULL;

free(temp);

return dequeuedData;

int isEmptyQueue(Queue* queue) {

return queue->front == NULL;

void displayQueue(Queue* queue) {

if (isEmptyQueue(queue)) {

printf("Queue is empty.\n");

20
return;

printf("Queue contents:\n");

QueueNode* current = queue->front;

while (current != NULL) {

printf("Train Details:\n");

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

current = current->next;

Queue.h

#ifndef QUEUE_H

#define QUEUE_H

#include "train.h"

typedef struct QueueNode {

Train data;

struct QueueNode* next;

} QueueNode;

typedef struct {

QueueNode* front;

QueueNode* rear;

} Queue;

Queue* createQueue();

void enqueue(Queue* queue, Train data);

Train dequeue(Queue* queue);

int isEmptyQueue(Queue* queue);

void displayQueue(Queue* queue);

21
#endif

Stack.c

#include <stdio.h>

#include <stdlib.h>

#include "stack.h"

Stack* createStack() {

Stack* stack = (Stack*)malloc(sizeof(Stack));

if (stack) {

stack->top = NULL;

return stack;

void push(Stack* stack, Train data) {

StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));

if (newNode) {

newNode->data = data;

newNode->next = stack->top;

stack->top = newNode;

} else {

printf("Memory allocation failed. Cannot push onto stack.\n");

Train pop(Stack* stack) {

if (isEmptyStack(stack)) {

printf("Stack underflow. Cannot pop from an empty stack.\n");

Train emptyTrain = {0};

return emptyTrain;

StackNode* temp = stack->top;

Train poppedData = temp->data;

stack->top = temp->next;

22
free(temp);

return poppedData;

int isEmptyStack(Stack* stack) {

return stack->top == NULL;

void displayStack(Stack* stack) {

if (isEmptyStack(stack)) {

printf("Stack is empty.\n");

return;

printf("Stack contents:\n");

StackNode* current = stack->top;

while (current != NULL) {

printf("Train Details:\n");

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

current = current->next;

Train peek(Stack *stack) {

if (isEmptyStack(stack)) {

printf("Error: Stack is empty.\n");

Train dummyTrain = {0};

return dummyTrain;

return stack->top->data;

Stack.h

#ifndef STACK_H

23
#define STACK_H

#include "train.h"

typedef struct StackNode {

Train data;

struct StackNode* next;

} StackNode;

typedef struct {

StackNode* top;

} Stack;

Stack* createStack();

void push(Stack* stack, Train data);

Train pop(Stack* stack);

int isEmptyStack(Stack* stack);

void displayStack(Stack* stack);

Train peek(Stack *stack);

#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

void readFilePath(char *filePath) {

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

scanf("%s", filePath);

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);
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) {

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: ");

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

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;

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

27
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:

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;

28
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);

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);
29
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;

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;

30
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);

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

31
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");

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;
32
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) {

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

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

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);
34
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) {

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:

35
deleteDoubleNode(&list);

break;

if (saving == 1) {

saveDoubleListToFileText(list, filePath);

} else {

saveDoubleListToFileBinary(list, filePath);

freeDoubleList(list);

} else {

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

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

36
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");

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: ");

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

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

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) {

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

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

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

40
break;

case 5:

deleteCircularNode(&head);

break;

if (saving == 1) {

saveCircularListToFileText(head, filePath);

} else {

saveCircularListToFileBinary(head, filePath);

freeCircularList(head);

} else {

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

}else{

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

return 0;

Circular.c

#include <stdio.h>

#include <stdlib.h>

#include "circular.h"

// Create a new node for circular linked list

CircularNode* createCircularNode(Train newData) {

CircularNode* newNode = (CircularNode*)malloc(sizeof(CircularNode));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);
41
}

newNode->data = newData;

newNode->next = NULL;

return newNode;

// Insert a node at the end of the circular linked list

CircularNode* insertCircularNode(CircularNode* head, Train newData) {

CircularNode* newNode = createCircularNode(newData);

if (head == NULL) {

newNode->next = newNode; // Point to itself to form a circular list

return newNode;

CircularNode* current = head;

while (current->next != head) {

current = current->next;

current->next = newNode;

newNode->next = head;

return head;

// Display the contents of the circular linked list

void displayCircularList(CircularNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

CircularNode* current = head;

printf("List contents:\n");

do {

printf("%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;

42
} while (current != head);

// Search for an element in the circular linked list

void searchCircularList(CircularNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

int searchTrainNumber;

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

scanf("%d", &searchTrainNumber);

CircularNode* current = head;

int position = 1;

do {

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

printf("Train found at position %d.\n", position);

return;

current = current->next;

position++;

} while (current != head);

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

// Delete a node from the circular linked list

void deleteCircularNode(CircularNode** head) {

if (*head == NULL) {

printf("List is empty.\n");

return;

int position;

printf("Enter position of node to delete: ");

scanf("%d", &position);

if (position < 1) {

printf("Invalid position.\n");

43
return;

CircularNode* current = *head;

CircularNode* previous = NULL;

int count = 1;

do {

if (count == position) {

if (current->next == current) { // Only one node in the list

free(current);

*head = NULL;

printf("Node deleted successfully.\n");

return;

if (current == *head) { // Deleting the first node

CircularNode* lastNode = *head;

while (lastNode->next != *head) {

lastNode = lastNode->next;

*head = (*head)->next;

lastNode->next = *head;

free(current);

printf("Node deleted successfully.\n");

return;

previous->next = current->next;

free(current);

printf("Node deleted successfully.\n");

return;

previous = current;

current = current->next;

count++;

} while (current != *head);

printf("Position out of range.\n");

void saveCircularListToFileText(CircularNode* head, char* filePath) {


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

if (file == NULL) {

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

return;

CircularNode* current = head;

if (head != NULL) {

do {

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;

} while (current != head);

fclose(file);

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

void saveCircularListToFileBinary(CircularNode* head, char* filePath) {

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

if (file == NULL) {

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

return;

CircularNode* current = head;

if (head != NULL) {

do {

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

current = current->next;

} while (current != head);

}
45
fclose(file);

printf("Circular linked list contents saved to file '%s' successfully in binary mode.\n", filePath);

// Free memory allocated for the circular linked list

void freeCircularList(CircularNode* head) {

if (head == NULL) {

return;

CircularNode* current = head;

CircularNode* temp;

do {

temp = current;

current = current->next;

free(temp);

} while (current != head);

Circular.h

#ifndef CIRCULAR_H

#define CIRCULAR_H

#include "train.h"

typedef struct CircularNode {

Train data;

struct CircularNode* next;

} CircularNode;

CircularNode* createCircularNode(Train newData);

CircularNode* insertCircularNode(CircularNode* head, Train newData);

void displayCircularList(CircularNode* head);

void searchCircularList(CircularNode* head);

46
void deleteCircularNode(CircularNode** head);

void freeCircularList(CircularNode* head);

void saveCircularListToFileBinary(CircularNode* head, char* filePath);

void saveCircularListToFileText(CircularNode* head, char* filePath);

#endif

Double.c

#include <stdio.h>

#include <stdlib.h>

#include "double.h"

// Create a new node for double linked list

DoubleNode* createDoubleNode(Train newData) {

DoubleNode* newNode = (DoubleNode*)malloc(sizeof(DoubleNode));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);

newNode->data = newData;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Insert a node at the end of the double linked list

DoubleNode* insertDoubleNode(DoubleNode* head, Train newData) {

DoubleNode* newNode = createDoubleNode(newData);

if (head == NULL) {

return newNode;

DoubleNode* current = head;

while (current->next != NULL) {

current = current->next;

47
current->next = newNode;

newNode->prev = current;

return head;

// Display the contents of the double linked list

void displayDoubleList(DoubleNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

DoubleNode* current = head;

printf("List contents:\n");

while (current != NULL) {

printf("%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;

// Search for an element in the double linked list

void searchDoubleList(DoubleNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

int searchTrainNumber;

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

scanf("%d", &searchTrainNumber);

DoubleNode* current = head;

int position = 1;

while (current != NULL) {

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

printf("Train found at position %d.\n", position);

48
return;

current = current->next;

position++;

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

// Delete a node from the double linked list

void deleteDoubleNode(DoubleNode** head) {

if (*head == NULL) {

printf("List is empty.\n");

return;

int position;

printf("Enter position of node to delete: ");

scanf("%d", &position);

if (position < 1) {

printf("Invalid position.\n");

return;

DoubleNode* current = *head;

for (int i = 1; i < position && current != NULL; i++) {

current = current->next;

if (current == NULL) {

printf("Position out of range.\n");

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

printf("Node deleted successfully.\n");

void saveDoubleListToFileText(DoubleNode* head, char* filePath) {

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

if (file == NULL) {

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

return;

DoubleNode* current = head;

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("Doubly linked list contents saved to file '%s' successfully.\n", filePath);

void saveDoubleListToFileBinary(DoubleNode* head, char* filePath) {

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

if (file == NULL) {

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

return;

DoubleNode* current = head;

while (current != NULL) {

50
fwrite(&current->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);

// Free memory allocated for the double linked list

void freeDoubleList(DoubleNode* head) {

DoubleNode* current = head;

while (current != NULL) {

DoubleNode* temp = current;

current = current->next;

free(temp);

Double.h

#ifndef DOUBLE_H

#define DOUBLE_H

#include "train.h"

typedef struct DoubleNode {

Train data;

struct DoubleNode* prev;

struct DoubleNode* next;

} DoubleNode;

DoubleNode* createDoubleNode(Train newData);

DoubleNode* insertDoubleNode(DoubleNode* head, Train newData);

void displayDoubleList(DoubleNode* head);

51
void searchDoubleList(DoubleNode* head);

void deleteDoubleNode(DoubleNode** head);

void freeDoubleList(DoubleNode* head);

void saveDoubleListToFileBinary(DoubleNode* head, char* filePath);

void saveDoubleListToFileText(DoubleNode* head, char* filePath);

#endif

Simple.c

#include <stdio.h>

#include <stdlib.h>

#include "simple.h"

// Create a new node for simple linked list

SimpleNode* createSimpleNode(Train newData) {

SimpleNode* newNode = (SimpleNode*)malloc(sizeof(SimpleNode));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);

newNode->data = newData;

newNode->next = NULL;

return newNode;

// Insert a node at the end of the simple linked list

SimpleNode* insertSimpleNode(SimpleNode* head, Train newData) {

SimpleNode* newNode = createSimpleNode(newData);

if (head == NULL) {

return newNode;

SimpleNode* current = head;

while (current->next != NULL) {

current = current->next;

52
current->next = newNode;

return head;

// Display the contents of the simple linked list

void displaySimpleList(SimpleNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

SimpleNode* current = head;

printf("List contents:\n");

while (current != NULL) {

printf("%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;

// Search for an element in the simple linked list

void searchSimpleList(SimpleNode* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

int searchTrainNumber;

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

scanf("%d", &searchTrainNumber);

SimpleNode* current = head;

int position = 1;

while (current != NULL) {

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

printf("Train found at position %d.\n", position);

return;

53
}

current = current->next;

position++;

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

// Delete a node from the simple linked list

void deleteSimpleNode(SimpleNode** head) {

if (*head == NULL) {

printf("List is empty.\n");

return;

int position;

printf("Enter position of node to delete: ");

scanf("%d", &position);

if (position < 1) {

printf("Invalid position.\n");

return;

if (position == 1) {

SimpleNode* temp = *head;

*head = (*head)->next;

free(temp);

printf("Node deleted successfully.\n");

return;

SimpleNode* current = *head;

SimpleNode* previous = NULL;

for (int i = 1; i < position && current != NULL; i++) {

previous = current;

current = current->next;

if (current == NULL) {

printf("Position out of range.\n");

return;

}
54
previous->next = current->next;

free(current);

printf("Node deleted successfully.\n");

void saveSimpleListToFileText(SimpleNode* head, char* filePath) {

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

if (file == NULL) {

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

return;

SimpleNode* current = head;

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("Simple linked list contents saved to file '%s' successfully.\n", filePath);

void saveSimpleListToFileBinary(SimpleNode* head, char* filePath) {

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

if (file == NULL) {

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

return;

SimpleNode* current = head;

while (current != NULL) {

55
fwrite(&current->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);

// Free memory allocated for the simple linked list

void freeSimpleList(SimpleNode* head) {

SimpleNode* current = head;

while (current != NULL) {

SimpleNode* temp = current;

current = current->next;

free(temp);

Simple.h

#ifndef SIMPLE_H

#define SIMPLE_H

#include "train.h"

typedef struct SimpleNode {

Train data;

struct SimpleNode* next;

} SimpleNode;

SimpleNode* createSimpleNode(Train newData);

SimpleNode* insertSimpleNode(SimpleNode* head, Train newData);

void displaySimpleList(SimpleNode* head);

void searchSimpleList(SimpleNode* head);

void deleteSimpleNode(SimpleNode** head);

56
void freeSimpleList(SimpleNode* head);

void saveSimpleListToFileBinary(SimpleNode* head, char* filePath);

void saveSimpleListToFileText(SimpleNode* head, char* filePath);

#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

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

67
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.

68

You might also like