Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. struct Car {
  6.     char* brand;
  7.     int license_number;
  8.     int owner_id;
  9.     int price;
  10.     Car * next;
  11. };
  12.  
  13. Car *head = NULL;
  14.  
  15. class list
  16. {
  17. private:
  18.     Car *head, *tail;
  19. public:
  20.     list()
  21.     {
  22.         head=NULL;
  23.         tail=NULL;
  24.     }
  25.     void add_car(const char* NewBrand, int number, int id,int NewPrice)
  26.     {
  27.  
  28.         bool added=0;
  29.         Car *curr;
  30.         Car *temp=new Car;
  31.         temp->brand=new char[strlen(NewBrand) + 1];
  32.         strcpy(temp->brand, NewBrand);
  33.         temp->license_number=number;
  34.         temp->owner_id=id;
  35.         temp->price=NewPrice;
  36.         temp->next=NULL;
  37.         if(head==NULL)
  38.         {
  39.             head=temp;
  40.             tail=temp;
  41.             temp=NULL;
  42.             added=1;
  43.         }
  44.         curr=head;
  45.         while(!added)
  46.         {
  47.             if(curr->license_number<number)
  48.             {
  49.                 temp->next=curr->next;
  50.                 curr->next=temp;
  51.                 added=1;
  52.             }
  53.             else if(curr->next==tail)
  54.             {
  55.                 tail->next=temp;
  56.                 tail=temp;
  57.                 added=1;
  58.             }
  59.             else if (curr->license_number==number && curr->owner_id==id)
  60.             {
  61.                 curr->price=NewPrice;
  62.                 added=1;
  63.             }
  64.             curr=curr->next;
  65.         }
  66.     }
  67.     void remove_brand(int id)
  68.     {
  69.         bool deleted=0;
  70.         Car *prev;
  71.         Car *curr;
  72.         curr=head;
  73.         prev=head;
  74.         while(curr!=NULL && !deleted)
  75.         {
  76.             if(curr->owner_id==id)
  77.             {
  78.                 if(curr==head)
  79.                 {
  80.                     head=head->next;
  81.                 }
  82.                 prev->next=curr->next;
  83.                 deleted=1;
  84.             }
  85.             prev=curr;
  86.             curr=curr->next;
  87.         }
  88.     }
  89.     void print()
  90.     {
  91.         Car *curr;
  92.         curr=head;
  93.         while(curr!=NULL)
  94.         {
  95.             cout << curr->brand << " " << curr->license_number << " " << curr->owner_id << " " << curr->price << endl;
  96.             curr=curr->next;
  97.         }
  98.     }
  99. };
  100.  
  101. int main ()
  102. {
  103.     list obj;
  104.     obj.add_car("Audi",1,69,69420);
  105.     obj.add_car("BMW",3,96,42069);
  106.     obj.add_car("Volkswagen",2,42,65148);
  107.     obj.add_car("Volkswagen",6,997,1235);
  108.     obj.add_car("Volkswagen",9,85,1235);
  109.     obj.add_car("Volkswagen",7,99,1235);
  110.     obj.print();
  111.     cout << endl;
  112.     obj.remove_brand(69);
  113.     obj.print();
  114.     cout << endl;
  115.     obj.add_car("Volkswagen",2,42,123);
  116.     obj.print();
  117.     return 0;
  118. }
  119.  
  120.  
  121. /*Problems:
  122. in add: sorting doesnt work */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement