I am making airport reservation with linked list. I am using c++ on Xcode.
A- Airport
F- Flight
C- customer
I want to make it like this
A -> F -> F -> F -> F -> ∞(NULL);
↓ ↓ ↓ ↓ ↓
C. C. C. C. C
↓. ↓. ↓. ↓. ↓
∞. ∞. ∞. ∞. ∞
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct Node{
char name[20];
Node* pLink;
};
struct List{
char portName[20];
Node headerNode;
List* pLink;
};
struct airport{
int numberOfFlight = 0;
List headerNode;
};
airport* createAirport(){
airport* pReturn = NULL;
pReturn = new airport;
if(pReturn != NULL){
memset(pReturn, 0, sizeof(airport));
}
else{
cout << "Not enough Space" << endl;
}
return pReturn;
}
void addList(airport* pList, char *newPortName){
List* pNewFlight = NULL;
List* pPreNode = NULL;
if(pList != NULL){
pNewFlight = new List;
if(pNewFlight != NULL){
strcpy(pNewFlight->portName, newPortName);
pNewFlight->pLink = NULL;
pPreNode = &(pList->headerNode);
while(pPreNode -> pLink != NULL){
pPreNode = pPreNode -> pLink;
}
pNewFlight -> pLink = pPreNode ->pLink;
pPreNode -> pLink = pNewFlight;
}
}
pList->numberOfFlight++;
}
void addNode(airport* pList, char *newPortName, char *newName){
List* pCurFlight = NULL;
Node *pNode = NULL, *pPreNode = NULL;
if(pList != NULL){
pNode = new Node;
if(pNode != NULL){
strcpy(pNode->name, newName);
pNode -> pLink = NULL;
pCurFlight = pList->headerNode.pLink;
while(strcmp(pCurFlight -> portName, newPortName)!=0){
pCurFlight = pCurFlight->pLink;
}
pPreNode = &(pCurFlight->headerNode);
while(pPreNode -> pLink != NULL){
pPreNode = pPreNode->pLink;
}
pNode -> pLink = pPreNode -> pLink;
pPreNode -> pLink = pNode;
}
}
}
void displayList(airport* pList, char *newFlightName){
List* pCurFlight = NULL;
Node* pNode = NULL;
if(pList != NULL){
pCurFlight = pList -> headerNode.pLink;
while(strcmp(pCurFlight->portName, newFlightName)!=0){
pCurFlight = pCurFlight -> pLink;
}
pNode = pCurFlight->headerNode.pLink;
while(pNode != NULL){
cout << pNode->name << endl;
pNode = pNode->pLink;
}
}
}
int main() {
airport* pList = NULL;
char portName[20];
char name[20];
pList = createAirport();
if(pList != NULL){
cout << "Enter a Port Name: " << endl;
cin >> portName;
addList(pList, portName);
cout << "Enter Your Name: " << endl;
cin >> name;
addNode(pList, portName, name);
cout << "Check reservation with airport: " << endl;
cin >> portName;
displayList(pList, portName);
}
}
Last edited on