Suppose a program has to maintain the flight lists for all of the flights depart
ID: 3727129 • Letter: S
Question
Suppose a program has to maintain the flight lists for all of the flights departing for an airline called HowU Air. You have decided to implement the flight lists as a doubly linked list. However, you have decided to hold all of the flights for your airline in an array because you know that five flights have taken off from various cities within the past 24 hours. A node of the list can be represented as:
Create an array to hold five flight lists as shown below:
NOTE EVERY FLIGHT LIST ENDS IN THE CITY WHERE IT BEGAN
Dallas, Houston, Chicago, Baltimore, Detroit, Denver, Kansas City
Los Angeles, San Francisco, Salt Lake City
Little Rock, Wichita, Minneapolis
Houston, Las Alamos, Las Vegas, Phoenix
Oakland, San Diego, Denver, Memphis, Greenville
Your program must implement the following methods: Append, Prepend, Remove, InsertAfter, PrintList, and PrintGateInfo
The Append function, appends data to the end of the list
The Prepend function, prepends data to the beginning of the list
The Remove function, removes a node from the list and adjusts the length of the list
The InsertAfter function, inserts a node after another node in your list
The PrintList function must print an entire doubly linked list
The PrintAirlineInfo function must print all the flight lists. The name of the Gate is Gate 56.
For list number 1, Prepend Atlanta to the list
For list number 2, Remove San Francisco
For list number 3, Insert Indianapolis to the list after Wichita
For list number 4, Append San Antonio to the list
List 5 remains the same.
Example Output for all of the flight lists for Gate 56 are below:
code i already have
/main.cpp
#include<iostream>
#include <string>
using namespace std;
#include "NODE.h"
#include "DoublyList.cpp"
DoublyList::DoublyList() {
this->head = nullptr;
this->tail = nullptr;
}
void DoublyList::InsertAfter(FLIGHT flight, int target, DoublyList * HUFlight, FLIGHT new_flight) {
NODE *newbie = new NODE;
newbie->flight = new_flight;
NODE* curNode = head;
while (curNode != nullptr) {
if (curNode->flight.destination == flight.destination) {
target--;
if (target == 0) {
if (curNode == tail) {
//if we have found ourselves at the tail, might as well call the Append function
HUFlight->Append(new_flight);
break;
}
else {
newbie->next = curNode->next;//connect new node to
curNode->next = newbie;
newbie->previous = curNode;
curNode = curNode->next;
curNode->previous = newbie;
break;
}
}
}
curNode = curNode->next;
if (curNode == head) {
break;
//breaks after hitting head the second time
}
}
}
void DoublyList::Remove(string place, int target) {
NODE* curNode = head;
while (curNode != nullptr) {
// because this is a circular linked list
if (curNode->flight.destination == place) {
target--;
if (target == 0) {
(curNode->next)->previous = curNode->previous;
(curNode->previous)->next = curNode->next;
break;
//connects previous and next node to each other
}
if (curNode == tail) {
tail = curNode->previous;
//if it is that we had removed the tail, we set it to the previous node
}
else if (curNode == head) {
head = curNode->next;
//if it is that we had removed the head, we set it to the next node
}
}
curNode = curNode->next;
if (curNode == head) {
break;
//breaks after hitting head the second time
}
}
}
void DoublyList::Prepend(FLIGHT flight) {
NODE *current = new NODE;
current->flight = flight;
if (head == NULL) {
// if nothing in list it points head and tail to new node
current->next = nullptr;
head = current;
tail = current;
}
else {
current->next = head;
head->previous = current;
head = current;
current->previous = tail;
}
}
void DoublyList::Append(FLIGHT flight) {
NODE *current = new NODE;
current->flight = flight;
if (head == NULL) {
// if nothing in list it points head and tail to new node
current->next = nullptr;
head = current;
tail = current;
}
else {
current->previous = tail;
tail->next = current;
tail = current;
current->next = head;
}
}
void DoublyList::Print() {
int count = 1;
NODE* curNode = head;
string first_place;
first_place = curNode->flight.destination;
curNode = head->next;
cout << "Flight Records for HowardAir Flight CSCI0136:" << endl;
while (curNode != NULL) {
cout << count << ". " << first_place << " to " << curNode->flight.destination << endl;
if (curNode == head) {
break;
//breaks after hitting head the second time
}
first_place = curNode->flight.destination;
count++;
curNode = curNode->next;//Traverses list
}
}
int main() {
DoublyList * HUFlight = new DoublyList;
FLIGHT fly, fly1;
fly.destination = "Houston";
fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Chicago";fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Arizona";fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Baltimore";fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Detroit";fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Denver";fly.flightNum = 0136;
HUFlight->Append(fly);
fly.destination = "Houston";fly.flightNum = 0136;
HUFlight->Append(fly);
//Below are steps instructed to execute
fly.destination = "Dallas";fly.flightNum = 0136;
HUFlight->Prepend(fly);
HUFlight->Remove("Arizona", 1);fly.flightNum = 0136;
fly.destination = "Kansas City";fly.flightNum = 0136;
HUFlight->Append(fly);
fly1.destination = "Minneapolis";fly.flightNum = 0136;
HUFlight->InsertAfter(fly, 1, HUFlight, fly1);
HUFlight->Remove("Houston", 2);fly.flightNum = 0136;
HUFlight->Print();
return 0;
}
------------------------------------------------------------------------------------------------------------------
//DoublyList.cpp
#include<iostream>
using namespace std;
#include "NODE.h"
class DoublyList {
public:
DoublyList();
void Remove(string place, int target);
void InsertAfter(FLIGHT flight, int target, DoublyList * HUFlight, FLIGHT new_flight);
void Append(FLIGHT flight);
void Prepend(FLIGHT flight);
void Print();
private:
NODE * head = NULL;
NODE* tail = NULL;
};
----------------------------------------------------------------------------------------------------------------
//Node.h
#ifndef NODE_H
#define NODE_H
#include<iostream>
using namespace std;
struct FLIGHT {
int flightNum;
string destination;
};
struct NODE {
FLIGHT flight;
NODE *next;
NODE *previous;
};
#endif //!NODE_H
it must be done in c++
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
//STRUCTURE
typedef struct{
char flightNo[5];
char date[12];
char time[6];
char gate[3];
}Flight;
Flight flight={"YZ22","10-12-2008","20:30","RT"};
typedef struct{
char name[30];
char booking_ID[3];
int seats;
}Seat;
Seat choice[4][5];
void displaymenu();
void booking();
void seat();
void ticket();
void records();
void looping();
void exit_();
//Variables
int selection;
int i;
int j;
int seats_num[20]={0};
int booking_ID=100;
int seatsAvailable=20;
int password;
int main(void)
{
displaymenu();
while(selection!=4)
{
looping();
}
return 0;
}
void displaymenu()
{
printf(" ");
printf(" Airline System "
" ======================= "
" MENU "
" ======================= "
" 1.BOOKING "
" 2.SEAT "
" 3.RECORDS "
" 4.EXIT ");
printf(" Enter your selection : ");
scanf("%d",&selection);
looping();
return;
}
//looping()
void looping()
{
switch(selection)
{
case 1:
booking();
break;
case 2:
seat();
break;
case 3:
records();
break;
case 4:
exit_();
break;
default:
printf(" Invalid selecion.Try again ");
}
return;
}
//booking
void booking()
{
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
printf(" Please enter seats number : ");
scanf("%d",&choice[i][j].seats);
fflush(stdin);
if(choice[i][j].seats<=seatsAvailable)
{
printf(" Please enter passenger name : ");
scanf("%[^ ]",choice[i][j].name);
fflush(stdin);
ticket();
booking_ID++;
}
seatsAvailable=seatsAvailable-choice[i][j].seats;
system("pause");
system("cls");
displaymenu();
}
if (seatsAvailable<0)
{
printf(" ");
printf(" SORRY, the flight is fully booked ");
printf(" =================END================= ");
displaymenu();
}
if(choice[i][j].seats>seatsAvailable)
{
printf(" ");
printf(" The flight leave %d seats ",seatsAvailable);
displaymenu();
}
return;
}
void ticket()
{
printf(" ");
printf(" -----------------AIRLINE BOOKING TICKET---------------- ");
printf(" ============================================================ ");
printf(" Booking ID : %d Flight No : %s ",booking_ID,flight.flightNo);
printf(" Passenger : %s ",choice[i][j].name);
printf(" Date : %s ",flight.date);
printf(" Time : %s ",flight.time);
printf(" Gate : %s ",flight.gate);
printf(" Seats No. : %d%c ",i+1,j+65);
printf(" ============================================================ ");
return;}
//seat
void seat()
{
printf(" A B C D E ");
for(j=0;j<5;j++)
{
printf("%d ",booking_ID);
}
for(i=0;i<4;i++)
{
printf(" ");
printf("%d ",i+1);
}
system("pause");
system("cls");
displaymenu();
return;
}
void records() //For Staff to View the flight's records
{
printf(" Please enter password: ");
scanf("%d", &password); //111
if (password==111)
{
system("cls");
printf(" ==================================== ");
printf(" ALL FLIGHT RECORDS ");
printf(" ==================================== ");
printf(" Seats Available left : %d ",seatsAvailable);
ticket();
system("pause");
system("cls");
displaymenu();
}
else
{
printf(" Invalid password ");
system("pause");
system("cls");
displaymenu();
}
return;
}
void exit_()
{
printf(" Thank you for using this system ");
exit(1);
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.