1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <cstdio>
#include <iostream>
using namespace std;
#define PASSENGER_NAME 36
#define FLIGHTS_NUM 5
#define SEATS_NUM 24
struct Passenger {
char *name;
char *flight;
int seat_num;
float price_paid;
Passenger *next;
};
bool isEmpty(Passenger *head);
char menu();
void insertAsFirst(Passenger *&head, Passenger *&last, char *p_name,
char *p_flight, int p_seat_num, float p_price_paid);
void addPassenger(Passenger *&head, Passenger *&last, char *p_name,
char *p_flight, int p_seat_num, float p_price_paid);
void removePassenger(Passenger *&head, Passenger *&last);
void showlist(Passenger *curent);
bool isEmpty(Passenger *head)
{
if (head == NULL)
return true;
else return false;
}
char menu()
{
char choice;
printf("\nOption: ");
scanf("%c", &choice);
return choice;
}
void insertAsFirst(Passenger *&head, Passenger *&last, char *p_name,
char *p_flight, int p_seat_num, float p_price_paid)
{
Passenger *temp = new Passenger;
temp -> name = p_name;
temp -> flight = p_flight;
temp -> seat_num = p_seat_num;
temp -> price_paid = p_price_paid;
temp -> next = NULL;
head = temp;
last = temp;
}
void addPassenger(Passenger *&head, Passenger *&last, char *p_name,
char *p_flight, int p_seat_num, float p_price_paid)
{
if(isEmpty(head))
{
insertAsFirst(head, last, p_name, p_flight, p_seat_num, p_price_paid);
}
else {
Passenger *temp = new Passenger;
temp -> name = p_name;
temp -> flight = p_flight;
temp -> seat_num = p_seat_num;
temp -> price_paid = p_price_paid;
temp -> next = NULL;
last -> next = temp;
last = temp;
}
}
void removePassenger(Passenger *&head, Passenger *&last)
{
if(isEmpty(head))
printf("Error: The list is already empty!\n");
else if (head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
Passenger *temp = head;
head = head -> next;
delete temp;
}
}
void showlist(Passenger *current)
{
if(isEmpty(current))
printf("Error: Cannot display list becaue it is empty!\n");
else
{
printf("Passengers: \n");
while(current != NULL)
{
printf("\nName: %s\nFlight: %s\nSeat: %d\nPaid: %f\n", current -> name, current -> flight, current -> seat_num, current -> price_paid);
printf("\n");
current = current -> next;
}
}
}
int main(int argc, char **argv)
{
//Used for menu manipulations
char choice;
char name[PASSENGER_NAME];
char flight[FLIGHTS_NUM];
int seat_num = 0;
float price_paid = 0;
Passenger *head = NULL;
Passenger *last = NULL;
printf("Menu: \n1.Add a Passenger\n2.Remove a Passenger\n3.Show List of Passengers\n4.Exit\n");
do {
choice = menu();
switch(choice)
{
case '1': cout << "Name: ";
cin >> name;
cout << "\nFlight: ";
cin >> flight;
cout << "\nSN: ";
cin >> seat_num;
cout << "\nPaid: ";
cin >> price_paid;
addPassenger(head, last, name, flight, seat_num, price_paid);
break;
case '2': removePassenger(head, last);
break;
case '3': showlist(head);
break;
default: printf("\n");
}
} while (choice != '4');
return 0;
}
|