Advertisement
tiffprag

Linked List Class(3)

Oct 25th, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>  //for creating text file
  4. using namespace std;
  5.  
  6. class linklist{
  7. private:
  8.   struct node{
  9.     Reservation info;
  10.     node *link;
  11.   }*head;
  12.  
  13. public:
  14.   linklist(){
  15.     head=NULL;
  16.   }
  17.  
  18.   void InsNewNodeByBookNo(int bNo, string na, int phNo, int pxNo, int d, int mon, int y, int t, int da){
  19.     //create new node with all the data
  20.     node*newNode = new node;
  21.     node*p,*q = head;
  22.     newNode -> info.setBookingNo(bNo);
  23.     newNode -> info.setName(na);
  24.     newNode -> info.setPhoneNo(phNo);
  25.     newNode -> info.setPaxNo(pxNo);
  26.     newNode -> info.setDay(d);
  27.     newNode -> info.setMonth(mon);
  28.     newNode -> info.setYear(y);
  29.     newNode -> info.setTime(t);
  30.     newNode -> info.setDate(da);
  31.  
  32.     //If no records
  33.     if (head == NULL){
  34.       head = newNode;
  35.       return;
  36.     }
  37.  
  38.     //Insert at the start of list
  39.     if (head -> info.getBookingNo() > newNode -> info.getBookingNo()){
  40.       newNode -> link = head;
  41.       head = newNode;
  42.       return;
  43.     }
  44.  
  45.     while(q->info.getBookingNo() < newNode -> info.getBookingNo() && q->link != NULL){
  46.       p = q;
  47.       q = q->link;
  48.     }
  49.     //Insert at the end of the list
  50.     if(q == NULL){
  51.       q = newNode;
  52.       newNode-> link = NULL;
  53.       return;
  54.     //Insert in the middle of the list
  55.     }else{
  56.       p = q;
  57.       q = q->link;
  58.       p->link = newNode;
  59.       newNode-> link = q;
  60.       return;
  61.     }
  62.   }
  63.  
  64.   void deleteNode(int bNo){
  65.     node *current;
  66.     current = head;
  67.     while (current != NULL){
  68.       //if the booking num is found
  69.       if(bNo == current->info.getBookingNo()){
  70.         //if its the first node
  71.         if(current == head){
  72.           node*temp = head;
  73.           head = head -> link;
  74.           delete temp;
  75.         //if its the last node
  76.         }else if(current->link == NULL){
  77.           node*p, *q = head;
  78.           while(q -> link != NULL){
  79.             p = q;
  80.             q = q -> link;
  81.           }
  82.           p->link = NULL;
  83.           delete q;
  84.  
  85.         }else{
  86.           node*p, *q = head;
  87.           while(q -> info.getBookingNo() != bNo){
  88.             p = q;
  89.             q = q -> link;
  90.           }
  91.           p -> link = q ->link;
  92.           delete q;
  93.         }
  94.       }
  95.       current = current->link;
  96.     }
  97.   }
  98.  
  99.   void PrintList(){
  100.     node *current;
  101.     current = head;
  102.     cout<<setfill(' ')<<setw(5)<<"| No. "
  103.         <<left<<setw(30)<<"Name"
  104.         <<setw(5)<<"Phone No."
  105.         <<right<<setw(16)<<"Pax No."
  106.         <<setw(10)<<"Date"
  107.         <<setw(16)<<"Time |"<<endl
  108.         <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  109.  
  110.     while (current != NULL){
  111.       cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  112.           <<left<<setw(30)<< current->info.getName()
  113.           <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  114.           <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  115.           <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  116.           <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  117.       current = current->link;
  118.     }
  119.     cout<<endl;
  120.   }
  121.  
  122.   void PrintBookingNoRecords(int bNo){
  123.     node *current;
  124.     current = head;
  125.     cout<<setfill(' ')<<setw(5)<<"| No. "
  126.         <<left<<setw(30)<<"Name"
  127.         <<setw(5)<<"Phone No."
  128.         <<right<<setw(16)<<"Pax No."
  129.         <<setw(10)<<"Date"
  130.         <<setw(16)<<"Time |"<<endl
  131.         <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  132.  
  133.     while (current != NULL){
  134.       //If there is any reservation with the same booking number
  135.       if(bNo == current->info.getBookingNo()){
  136.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  137.             <<left<<setw(30)<< current->info.getName()
  138.             <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  139.             <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  140.             <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  141.             <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  142.       }
  143.       current = current->link;
  144.     }
  145.     cout<<endl;
  146.   }
  147.  
  148.   void PrintNameRecords(string name){
  149.     node *current;
  150.     current = head;
  151.     cout<<setfill(' ')<<setw(5)<<"| No. "
  152.         <<left<<setw(30)<<"Name"
  153.         <<setw(5)<<"Phone No."
  154.         <<right<<setw(16)<<"Pax No."
  155.         <<setw(10)<<"Date"
  156.         <<setw(16)<<"Time |"<<endl
  157.         <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  158.  
  159.     while (current != NULL){
  160.       //If there is any reservation with the same name
  161.       if(name == current->info.getName()){
  162.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  163.             <<left<<setw(30)<< current->info.getName()
  164.             <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  165.             <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  166.             <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  167.             <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  168.       }
  169.       current = current->link;
  170.     }
  171.     cout<<endl;
  172.   }
  173.  
  174.   void PrintDateRecords(int date){
  175.     node *current;
  176.     current = head;
  177.     cout<<setfill(' ')<<setw(5)<<"| No. "
  178.         <<left<<setw(30)<<"Name"
  179.         <<setw(5)<<"Phone No."
  180.         <<right<<setw(16)<<"Pax No."
  181.         <<setw(10)<<"Date"
  182.         <<setw(16)<<"Time |"<<endl
  183.         <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  184.  
  185.     while (current != NULL){
  186.       //If there is any reservation with the same date
  187.       if(date == current->info.getDate()){
  188.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  189.             <<left<<setw(30)<< current->info.getName()
  190.             <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  191.             <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  192.             <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  193.             <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  194.       }
  195.       current = current->link;
  196.     }
  197.     cout<<endl;
  198.   }
  199.  
  200.   void PrintListTextFile(){
  201.     string fileName;
  202.     cout << "---------------=CREATE TEXT FILE=---------------" << endl;
  203.     //Create file with the file name user entered
  204.     cout << "Enter file name: ";
  205.     cin >> fileName;
  206.     cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  207.     fileName = fileName + ".txt";
  208.  
  209.     //output to .txt file
  210.     ofstream file(fileName.c_str());
  211.  
  212.     if (file.is_open()) {
  213.       //output all the reservation records
  214.       node *current;
  215.       current = head;
  216.       file<<setfill(' ')<<setw(5)<<"| No. "
  217.           <<left<<setw(30)<<"Name"
  218.           <<setw(5)<<"Phone No."
  219.           <<right<<setw(16)<<"Pax No."
  220.           <<setw(10)<<"Date"
  221.           <<setw(16)<<"Time |"<<endl
  222.           <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  223.  
  224.       while (current != NULL){
  225.         file<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  226.             <<left<<setw(30)<< current->info.getName()
  227.             <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  228.             <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  229.             <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  230.             <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  231.         current = current->link;
  232.       }
  233.       file.close(); //close file
  234.     }        
  235.   }
  236.  
  237.   //check if the booking number exist within the records
  238.   bool checker(int bNo){
  239.     node *current;
  240.     current = head;
  241.     while(current != NULL){
  242.       if(bNo == current->info.getBookingNo()){
  243.         return true;
  244.       }
  245.       current = current->link;
  246.     }
  247.     return false;
  248.   }
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement