Final Project of the "Algoritmi e Principi dell'Informatica" Course - 2022-2023 Edition.
-
AVL Tree Implementation:
- The stations are stored in an AVL tree, which ensures balanced and efficient searching, insertion, and deletion operations.
- Functions for right and left rotations, balancing, and maintaining the height of the nodes are implemented to keep the tree balanced.
- The
addStation,removeStation, andfindStationfunctions manage the stations within the AVL tree.
-
Linked List for Cars:
- Each station maintains a linked list of cars.
- Functions such as
addCar,removeCar, andfindMaxRangemanage the cars within each station. - The linked list implementation allows efficient insertion and deletion of cars within a station.
-
Command Handling:
- The
mainfunction reads commands from the standard input and calls the appropriate handler functions. - Supported commands include
aggiungi-stazione,demolisci-stazione,aggiungi-auto,rottama-auto,pianifica-percorso, andstampa. - Each command triggers specific operations like adding or removing stations and cars, or printing the station details in order.
- The
-
Route Planning:
- The code includes functions to plan routes between stations, either forwards or backwards.
- It ensures that the routes are correctly identified and handles cases where no route is available.
-
Memory Management:
- The code dynamically allocates and deallocates memory for stations and cars using
mallocandfree. - Careful attention is given to updating parent pointers when stations are removed.
- The code dynamically allocates and deallocates memory for stations and cars using
-
Input and Output Handling:
- The code uses
scanffor reading input andprintffor outputting results, which makes it suitable for command-line interaction. - Error handling is included to manage incorrect inputs gracefully.
- The code uses
-
Efficiency Considerations:
- The AVL tree ensures that the operations related to stations are performed in logarithmic time, maintaining efficiency even as the number of stations grows.
- The linked list allows efficient management of cars within each station, although it may not be as efficient for large numbers of cars compared to more complex data structures.
-
Modular Structure:
- The code is divided into several functions, each responsible for a specific task, improving readability and maintainability.
- Separate sections for AVL tree functions and linked list functions are clearly marked.
-
AVL Tree Functions:
rightRotate,leftRotate: Perform rotations to maintain tree balance.getBalance,height: Calculate balance factors and heights of nodes.addStation,removeStation: Add or remove stations in the AVL tree.findStation,minValueStation: Search for stations and find the minimum value station.
-
Linked List Functions:
addCar,removeCar: Add or remove cars in a station.findMaxRange: Find the car with the maximum range in a station.
-
Command Handlers:
handle_aggiungi_stazione,handle_demolisci_stazione: Handle adding and demolishing stations.handle_aggiungi_auto,handle_rottama_auto: Handle adding and scrapping cars.handle_pianifica_percorso: Handle route planning.printInOrder: Print stations in order.
Compile the code using a C compiler:
gcc -o station_manager main2.cRun the program and provide commands through standard input:
./station_managerExample commands:
aggiungi-stazione 10 100
aggiungi-auto 10 50
stampa
pianifica-percorso 10 20