Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- struct Car {
- char* brand;
- int license_number;
- int owner_id;
- int price;
- Car * next;
- };
- Car *head = NULL;
- class list
- {
- private:
- Car *head, *tail;
- public:
- list()
- {
- head=NULL;
- tail=NULL;
- }
- void add_car(const char* NewBrand, int number, int id,int NewPrice)
- {
- bool added=0;
- Car *curr;
- Car *temp=new Car;
- temp->brand=new char[strlen(NewBrand) + 1];
- strcpy(temp->brand, NewBrand);
- temp->license_number=number;
- temp->owner_id=id;
- temp->price=NewPrice;
- temp->next=NULL;
- if(head==NULL)
- {
- head=temp;
- tail=temp;
- temp=NULL;
- added=1;
- }
- curr=head;
- while(!added)
- {
- if(curr->license_number<number)
- {
- temp->next=curr->next;
- curr->next=temp;
- added=1;
- }
- else if(curr->next==tail)
- {
- tail->next=temp;
- tail=temp;
- added=1;
- }
- else if (curr->license_number==number && curr->owner_id==id)
- {
- curr->price=NewPrice;
- added=1;
- }
- curr=curr->next;
- }
- }
- void remove_brand(int id)
- {
- bool deleted=0;
- Car *prev;
- Car *curr;
- curr=head;
- prev=head;
- while(curr!=NULL && !deleted)
- {
- if(curr->owner_id==id)
- {
- if(curr==head)
- {
- head=head->next;
- }
- prev->next=curr->next;
- deleted=1;
- }
- prev=curr;
- curr=curr->next;
- }
- }
- void print()
- {
- Car *curr;
- curr=head;
- while(curr!=NULL)
- {
- cout << curr->brand << " " << curr->license_number << " " << curr->owner_id << " " << curr->price << endl;
- curr=curr->next;
- }
- }
- };
- int main ()
- {
- list obj;
- obj.add_car("Audi",1,69,69420);
- obj.add_car("BMW",3,96,42069);
- obj.add_car("Volkswagen",2,42,65148);
- obj.add_car("Volkswagen",6,997,1235);
- obj.add_car("Volkswagen",9,85,1235);
- obj.add_car("Volkswagen",7,99,1235);
- obj.print();
- cout << endl;
- obj.remove_brand(69);
- obj.print();
- cout << endl;
- obj.add_car("Volkswagen",2,42,123);
- obj.print();
- return 0;
- }
- /*Problems:
- in add: sorting doesnt work */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement