Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  22
Language: C/C++
Code:
#include "iostream"
#include<string.h>
using namespace std;
class Wagon
 {
     private:
     int bookedTickets;
     Wagon * nextWagon;
     
     public:
             void setBookedTickets(int bookedTickets)
             {
             this->bookedTickets=bookedTickets;
             }
         int getBookedTickets(){return this->bookedTickets;}
         
         void setNextWagon(Wagon * nt){this->nextWagon=nt;}
         
         Wagon * getNextWagon(){return this->nextWagon ; }
 };
 class Train
 {
         private:
         Wagon * headWagon;
         int size;
         
         public:
         void init(){
             this->size=0;
             this->headWagon= nullptr;
                     }
         void pushFront(int b){
         Wagon *tmp;
         tmp=new Wagon();
         tmp->setBookedTickets(b);
         tmp->setNextWagon(this->headWagon) ;
         this->headWagon=tmp;
         this->size ++ ;      }
         
         
         
         void destroy(){
             Wagon * tmp= this->headWagon;
             while(tmp!= nullptr)
             {
                 Wagon * tp = tmp;
                 tmp=tmp->getNextWagon();
                 delete tp;
             }
         }
         void printw() {Wagon * tmp = this->headWagon ;
            
             
 if((tmp->getBookedTickets()) %2 == 0) cout<<"Girl";
 else cout<<"Boy" ;
         
             
         }
         

 
            bool popFront(){
    if (headWagon == NULL) 
        return false; 
  
   else {
    Wagon* temp = this->headWagon; 
    this->headWagon = temp->getNextWagon();
    delete temp; 
  this->size -- ;
  return true;
   } 
   

 };

 
         
 
 





int main()

{
 Train tr;
 tr.init();
 char str3 [7] , str1[] = "finish" , str2[] = "pop";
 int a;
 gets(str3);
 while(strcmp(str1, str3) != 0)
 {
  if(strcmp(str2, str3) == 0) tr.popFront();
  else 
  {
   cin>>a;
   tr.pushFront(a);
  }
  gets(str3);   
 } 
 tr.printw();
 tr.destroy();

}
Comments: