Advertisement
tiffprag

Linked List Class

Oct 25th, 2019
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 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.     total--;
  95.   }
  96.  
  97.   void PrintList(){
  98.     node *current;
  99.     current = head;
  100.     cout<<setfill(' ')<<setw(5)<<"| No. "
  101.     <<left<<setw(30)<<"Name"
  102.     <<setw(5)<<"Phone No."
  103.     <<right<<setw(16)<<"Pax No."
  104.     <<setw(10)<<"Date"
  105.     <<setw(16)<<"Time |"<<endl
  106.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  107.  
  108.     while (current != NULL){
  109.       cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  110.       <<left<<setw(30)<< current->info.getName()
  111.       <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  112.       <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  113.       <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  114.       <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  115.       current = current->link;
  116.     }
  117.     cout<<endl;
  118.   }
  119.  
  120.   void PrintBookingNoRecords(int bNo){
  121.     node *current;
  122.     current = head;
  123.     cout<<setfill(' ')<<setw(5)<<"| No. "
  124.     <<left<<setw(30)<<"Name"
  125.     <<setw(5)<<"Phone No."
  126.     <<right<<setw(16)<<"Pax No."
  127.     <<setw(10)<<"Date"
  128.     <<setw(16)<<"Time |"<<endl
  129.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  130.  
  131.     while (current != NULL){
  132.       //If there is any reservation with the same name
  133.       if(bNo == current->info.getBookingNo()){
  134.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  135.         <<left<<setw(30)<< current->info.getName()
  136.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  137.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  138.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  139.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  140.       }
  141.       current = current->link;
  142.     }
  143.     cout<<endl;
  144.   }
  145.  
  146.   void PrintNameRecords(string name){
  147.     node *current;
  148.     current = head;
  149.     cout<<setfill(' ')<<setw(5)<<"| No. "
  150.     <<left<<setw(30)<<"Name"
  151.     <<setw(5)<<"Phone No."
  152.     <<right<<setw(16)<<"Pax No."
  153.     <<setw(10)<<"Date"
  154.     <<setw(16)<<"Time |"<<endl
  155.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  156.  
  157.     while (current != NULL){
  158.       //If there is any reservation with the same name
  159.       if(name == current->info.getName()){
  160.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  161.         <<left<<setw(30)<< current->info.getName()
  162.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  163.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  164.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  165.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  166.       }
  167.       current = current->link;
  168.     }
  169.     cout<<endl;
  170.   }
  171.  
  172.   void PrintDateRecords(int date){
  173.     node *current;
  174.     current = head;
  175.     cout<<setfill(' ')<<setw(5)<<"| No. "
  176.     <<left<<setw(30)<<"Name"
  177.     <<setw(5)<<"Phone No."
  178.     <<right<<setw(16)<<"Pax No."
  179.     <<setw(10)<<"Date"
  180.     <<setw(16)<<"Time |"<<endl
  181.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  182.  
  183.     while (current != NULL){
  184.       //If there is any reservation with the same date
  185.       if(date == current->info.getDate()){
  186.         cout<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  187.         <<left<<setw(30)<< current->info.getName()
  188.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  189.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  190.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  191.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  192.       }
  193.       current = current->link;
  194.     }
  195.     cout<<endl;
  196.   }
  197.  
  198.   void PrintListTextFile(){
  199.     string fileName;
  200.     cout << "---------------=CREATE TEXT FILE=---------------" << endl;
  201.     //Create file with the file name user entered
  202.     cout << "Enter file name: ";
  203.     cin >> fileName;
  204.     cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  205.     fileName = fileName + ".txt";
  206.  
  207.     //output to .txt file
  208.     ofstream file(fileName.c_str());
  209.  
  210.     if (file.is_open()) {
  211.       //output all the reservation records
  212.       node *current;
  213.       current = head;
  214.       file<<setfill(' ')<<setw(5)<<"| No. "
  215.       <<left<<setw(30)<<"Name"
  216.       <<setw(5)<<"Phone No."
  217.       <<right<<setw(16)<<"Pax No."
  218.       <<setw(10)<<"Date"
  219.       <<setw(16)<<"Time |"<<endl
  220.       <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  221.  
  222.       while (current != NULL){
  223.         file<<"| "<<setfill(' ')<<current->info.getBookingNo()<<"   "
  224.         <<left<<setw(30)<< current->info.getName()
  225.         <<right<<setw(10)<<setfill('0')<< current->info.getPhoneNo()
  226.         <<setw(12)<<setfill(' ')<< current->info.getPaxNo()<<setw(6)<<setfill(' ')<<" "
  227.         <<setw(2)<<setfill('0')<< current->info.getDay() <<"/"<<setw(2)<<setfill('0')<< current->info.getMonth() <<"/"<<setw(4)<<setfill('0')<< current->info.getYear()<<setw(7)<<setfill(' ')<<" "
  228.         <<setw(4)<<setfill('0')<< current->info.getTime() <<" |"<<endl;
  229.         current = current->link;
  230.       }
  231.       file.close(); //close file
  232.     }        
  233.   }
  234.  
  235.   bool checker(int bNo){
  236.     node *current;
  237.     current = head;
  238.     while(current != NULL){
  239.       if(bNo == current->info.getBookingNo()){
  240.         return true;
  241.       }
  242.       current = current->link;
  243.     }
  244.     return false;
  245.   }
  246. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement