Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- class list
- {
- private:
- struct cell
- {
- string surname;
- string serialNumber;
- cell* next;
- cell()
- {
- next = nullptr;
- }
- cell(string surname, string serialNumber, cell* next)
- : surname(surname), serialNumber(serialNumber), next(next) {};
- };
- cell* first;
- string carName;
- list* next;
- public:
- list()
- {
- first = nullptr;
- next = nullptr;
- }
- list(string carName, list* next) : carName(carName), next(next)
- {
- first = nullptr;
- }
- void setCarName(string _carName);
- string getCarName();
- void setNext(list* _next);
- list* getNext();
- bool isEmpty();
- void print();
- void add(string surname, string serialNumber);
- };
- class carList : public list
- {
- private:
- list* first;
- public:
- carList()
- {
- first = nullptr;
- }
- bool isEmpty();
- void print();
- void add(string carName);
- list* getList(string carName);
- };
- int main()
- {
- carList cars;
- cars.add("Toyota");
- cars.getList("Toyota")->add("A", "B");
- cars.getList("Toyota")->add("C", "D");
- cars.getList("Toyota")->print();
- return 0;
- }
- void list::setCarName(string _carName)
- {
- carName = _carName;
- }
- string list::getCarName()
- {
- return carName;
- }
- void list::setNext(list* _next)
- {
- next = _next;
- }
- list* list::getNext()
- {
- return next;
- };
- bool
- list::isEmpty()
- {
- return first == nullptr;
- }
- void
- list::print()
- {
- if (isEmpty())
- {
- cout << "List is empty" << endl;
- return;
- }
- cell* it = first;
- size_t k = 1;
- while (it != nullptr)
- {
- cout << k << ") Surname is " << it->surname << " | Serial Number is " << it->serialNumber << endl;
- k++;
- it = it->next;
- }
- }
- void
- list::add(string surname, string serialNumber)
- {
- cell* newCell = new cell(surname, serialNumber, nullptr);
- if (isEmpty())
- {
- first = newCell;
- }
- else
- {
- cell* it = first;
- while (it->next != nullptr)
- it = it->next;
- it->next = newCell;
- }
- }
- void
- carList::add(string carName)
- {
- list* newList = new list(carName, nullptr);
- if (isEmpty())
- {
- first = newList;
- }
- else
- {
- list* it = first;
- while (it->getNext() != nullptr)
- it = it->getNext();
- it->setNext(newList);
- }
- }
- list* carList::getList(string carName)
- {
- if (first == nullptr)
- return nullptr;
- list* it = first;
- while(it->getNext() != nullptr)
- {
- if(it->getCarName() == carName)
- {
- return it;
- }
- it = it->getNext();
- }
- return nullptr;
- }
- bool
- carList::isEmpty()
- {
- return first == nullptr;
- }
- void
- carList::print()
- {
- if (isEmpty())
- {
- cout << "CarList is empty" << endl;
- return;
- }
- list* it = first;
- size_t k = 1;
- while (it != nullptr)
- {
- cout << k << ") Car name is " << it->getCarName() << endl;
- k++;
- it = it->getNext();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement