In: Computer Science
Write a simple airline ticket reservation program. The program should display a menu with the following operations: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list of flights with each node including a pointer to a linked list of passengers.
In C++ language.
#include <iostream>
using namespace std;
class Passenger;
class Flight;
int PrintPassenger(const Passenger* pas);
int PrintFlightAndPass(const Flight * list);
template <typename Type>
class LinkedList
{
public:
LinkedList()
{
  this->head=NULL;
}
~LinkedList()
{
  while(this->head !=NULL)
  {
   SNode<Type>
*node=this->head->next;
   delete this->head;
   this->head =node;
  }
}
public:
template <typename Type>
inline bool Add(Type *pdata)
{
  if(this->FindNode<Type>(pdata->name)!=NULL)
   return false;
  SNode<Type> *node=new
SNode<Type>(pdata);
  if(head==NULL)
  {
   head=node;
   return true;
  }
  SNode<Type> *tnode=this->head;
  if(*(node->pdata) < *(tnode->pdata))
  {
   node->next=tnode;
   head=node;
   return true;
  }
  while(tnode->next!=NULL &&
   *(node->pdata) >
*(tnode->next->pdata))
  {
   tnode=tnode->next;
  }
  if(tnode->next==NULL)
  {
   tnode->next=node;
  }
  else
  {
   node->next=tnode->next;
   tnode->next=node;
  }
  return true;
}
public:
template<typename Type>
inline Type* FindNode(const char *name)
{
  SNode<Type> *tnode=this->head;
  while(tnode!=NULL &&
!tnode->pdata->HasName(name))
  {
   tnode=tnode->next;
  }
  if(tnode!=NULL)
  {
   return tnode->pdata;
  }
  return NULL;
}
template <typename Type>
inline bool Delete(const char * name)
{
  if(this->head==NULL) return false;
  SNode<Type> *tnode=this->head;
  SNode<Type> *delnode=NULL;
  if(tnode->pdata->HasName(name))
  {
   delnode=tnode;
   head=tnode->next;
   delete delnode;
   return true;
  }
  while(tnode->next!=NULL &&
   !tnode->next->pdata->HasName(name))
  {
   tnode=tnode->next;
  }
  if(tnode->next!=NULL)
  {
   delnode=tnode->next;
   tnode->next=delnode->next;
   delete delnode;
   return true;
  }
  return false;
}
inline void PrintAllNodes()
{
  SNode<Type> *tnode=this->head;
  while(tnode != NULL)
  {
   cout<<tnode->pdata->name<<"\t";
   tnode=tnode->next;
  }
  cout<<endl;
}
template <typename Type>
inline void ForEach(int (*operate)(const Type *args)) const
{
  SNode<Type> *tnode=this->head;
  while(tnode != NULL)
  {
   (*operate)(tnode->pdata);
   tnode=tnode->next;
  }
}
private:
template <typename Type>
class SNode
{
public:
  Type *pdata;
  SNode *next;
  SNode()
  {
   pdata=NULL;
   next=NULL;
  }
  SNode(Type * pdata)
  {
   this->pdata=pdata;
   this->next=NULL;
  }
  ~SNode()
  {
   if(this->pdata!=NULL)
   {
    delete pdata;
    pdata=NULL;
   }
  }
};
SNode<Type> *head;
//int  count;
};
class Passenger
{
public:
char  name[40];
Passenger(){this->name[0]=0;}
Passenger(char * name)
{
  if(strlen(name)<40)
   strcpy(this->name,name);
}
inline bool operator==(const Passenger& operhand) const
{
  if(!strcmp(this->name,operhand.name))
   return true;
  return false;
}
inline bool operator<(const Passenger& operhand)const
{
  if(strcmp(this->name,operhand.name )<0)
   return true;
  return false;
}
inline bool operator>(const Passenger& operhand)const
{
  return !((*this)<operhand);
}
inline bool HasName(const char* name)
{
  if(!strcmp(name,this->name))
   return true;
  return false;
}
};
class Flight
{
private:
LinkedList<Passenger> *plist;
public:
char     name[40];
int      reversed;
int      capacity;
Flight()
{
  this->name[0]=0;
  capacity=10;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
Flight(char *name)
{
  strcpy_s(this->name,40,name);
  capacity=10;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
Flight(char *name,int capacity)
{
  strcpy_s(this->name,40,name);
  this->capacity=capacity;
  reversed=0;
  plist=new LinkedList<Passenger>();
}
~Flight()
{
  if(plist!=NULL)
  {
   delete plist;
   plist=NULL;
  }
}
inline bool operator==(const Flight &operhand)const
{
  if(this->name==operhand.name &&
   this->capacity==operhand.capacity)
   return true;
  return false;
}
inline bool operator<(const Flight& operhand)const
{
  if(strcmp(this->name,operhand.name )<0)
   return true;
  return false;
}
inline bool operator>(const Flight& operhand)const
{
  return !((*this)<operhand);
}
inline bool HasName(const char* name) const
{
  if(!strcmp(name,this->name))
   return true;
  return false;
}
inline bool AddPassenger(Passenger* ppass)
{
  if(reversed<capacity)
   if(plist->Add<Passenger>(ppass))
   {
    reversed++;
    return true;
   }
   return false;
}
inline bool DelPassengerByName(const char * name)
{
  if(plist->Delete<Passenger>(name))
  {
   reversed--;
   return true;
  }
  return false;
}
inline int GetFreeTicketCount() const
{
  return capacity-reversed;
}
inline void PrintAllPassengers() const
{
  plist->ForEach<Passenger>(PrintPassenger);
  cout<<endl;
}
};
int PrintPassenger(const Passenger* pas)
{
cout<<pas->name<<"\t";
return 0;
}
int PrintFlightAndPass(const Flight * list)
{
const Flight * t=list;
cout<<t->name<<"
("<<t->reversed<<" Reversed "<<
  t->GetFreeTicketCount()<<"
Free)"<<endl;
cout<<"\t";
t->PrintAllPassengers ();
return 0;
}
int main(int argc,char **argv)
{
LinkedList<Flight> flist;
flist.Add(new Flight("CA1033"));
flist.Add(new Flight("CU3254"));
flist.Add(new Flight("AA3333",3));
flist.FindNode<Flight>("CA1033")->AddPassenger(new
Passenger("Zhao"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new
Passenger("Qian"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new
Passenger("Sun"));
flist.FindNode<Flight>("CA1033")->AddPassenger(new
Passenger("Li"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new
Passenger("Zhou"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new
Passenger("Wu"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new
Passenger("Zheng"));
flist.FindNode<Flight>("AA3333")->AddPassenger(new
Passenger("Wang"));
flist.FindNode<Flight>("CU3254")->AddPassenger(new
Passenger("Li"));
cout<<"--START--"<<endl;
flist.ForEach<Flight> (PrintFlightAndPass);
cout<<"--END--"<<endl;
flist.FindNode<Flight>("CA1033")->DelPassengerByName("Li");
flist.Delete<Flight>("AA3333");
cout<<"--START--"<<endl;
flist.ForEach<Flight> (PrintFlightAndPass);
cout<<"--START--"<<endl;
return 0;
}