Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Program. Flight Routes using Linked Lists The objective of this program is to create a list of flight route entries, which allows a user

C Program. Flight Routes using Linked Lists The objective of this program is to create a list of flight route entries, which allows a user to add, delete, and search for a given route. A linked list of flights will be maintained in which each node contains a flight structure. The flights will be initialized by reading them into a table from a text file.

Detail:

The initial flight will be read from a text file.The file will contain the FlightNumber (int), Departure City Code (int), Arrival City Code (int), and Cost in dollars (int, with no cents) for each flight. There will be one flight per line. Note that there may be more than one flight with the same Departure and Arrival City code. However, the Flight Numbers are unique. Also, the first line will have a single integer value that identifies the number of flight records contained within the file.

Example Flight Data File:

Flights.txt

4

4536 1 3 456

4543 2 6 1200

5651 2 3 546

6769 1 3 550

A structure (Flight struct) should be used with the following members:

int flightNumber; int depCityCode;

int arrCityCode;

int cost;

The Flight struct should be encapsulated into a node to support the linked-list structure. For example:

struct listNode {

Flight flight; // Flight Structure

struct listNode *nextPtr; // Pointer to next node

};

For program readability, it would be best to have a few typedefs such as:

typedef struct listNode ListNode; // Alias for listNode

typedef ListNode * ListNodePtr; //Alias for ListNode *

A linked list will be used to hold the list of flights. It should allow the user to add, remove, and search for given flights based on the Departure City and Arrival City.Each node in the list must contain the Flight struct structure. Also the list must be sorted in ascending order by flightNumber.

The following functions must be implemented:

int initFlightList(ListNodePtr * listPtr, const char * flightFileName);

int findFlight(ListNodePtr * listPtr, int deptCode,int arrCode);

int printMatchingFlights(ListNodePtr * listPtr, intdepCode, intarrCode);

int addFlight(ListNodePtr * listPtr, int flightNum,int depCode, intarrCode, int cost);

int removeFlight(ListNodePtr * listPtr, int flightNum);

void printFlightList(ListNodePtr * listPtr);

-----------------------------------------------------------------------------------------

int initFlightList(ListNodePtr * listPtr, const char * flightFileName);

TheinitFlightList() will accept two parameters, the first being a pointer to the pointer that points to the start of the list (contains the lowest numberedflightNumber) and the other being the file to use for initializing the Flight list. There needs to be code within this function to open, read, and close the inputFile. Also, the code must dynamically allocate each Flight struct. It should also reject any flightNumbers that are already in the list. The function should return the number of flights created. If an error occurs (file not found, file read error, etc...) it should return -1.

int findFlight(ListNodePtr * listPtr, int deptCode, int arrCode);

The findFlight() will accept three parameters, the first being a pointer to the pointer that points to the start of the list, the second is the Departure City code, and the third being the Arrival City code. The function should search sequentially through the list for a matching route with the given Departure and Arrival City codes. It should stop at the first match it finds.If no matches are found, it should return -1, otherwise, a 0 is returned. An entry is considered a match if both the given Departure City and Arrival City Codes match the ones in within the Flightstruct.

int printMatchingFlights(ListNodePtr * listPtr, intdepCode, intarrCode);

The printMatchingFlights() will accept three parameters, the first being a pointer to the pointer that points to the start of the list, the second is the Departure City code, and the third being the Arrival City code. The function should print out all the data for each of the Flight structs within the list that match the given Departure and Arrival City codes. It must return the total number of matching flights. An entry is considered a match if both the given Departure City and Arrival City Codes match the ones in within the flight entry.

int addFlight(ListNodePtr * listPtr, int flightNum, int depCode, int arrCode, int cost);

The addFlight() will accept four parameters. The first being a pointer to the pointer that points to the start of the list, the second is the Flight Number, the third is the Departure City code, the fourth is the Arrival City code, the lost is the flight cost. The function will create a new Flight with the given parameters and add it to the Flight list if there is no matching Flight Number (the FlightNumbers must be unique). It must use malloc() function for creating the Flightstruct and store its pointer into the list node. Upon success the function will return 0, otherwise the function will return -1 for an error.

int removeFlight(ListNodePtr * listPtr, int flightNum);

The removeFlight() will accept two parameters. The first being a pointer to the pointer that points to the start of the list, the second is the Flight Number. The function will remove the Flight from the list whose Flight Number matches the given Flight Number. It will scan the Flight list sequentially looking for a match. If the given Flight number does not exist, the function should return -1, otherwise a 0 value is returned for success.

void printFlightList(ListNodePtr * listPtr);

The printFlightList() will accept one parameter, a pointer to the pointer that points to the start of the list. The function should print all the Flight parameters for each flight in the list in ascending order by Flight Number. This function returns void therefore no return values are required.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the major social responsibilities of business managers ?

Answered: 1 week ago

Question

What are the skills of management ?

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago