Advertisement
tiffprag

Linked List Class(2)

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