There is a list template:

#include <iostream> template<class T> class List { private: struct Element { T l_data; Element *l_next; }; Element *pHead{}; // указатель на первый элемент списка Element *pPrev{}; // указатель на последний элемент списка int countElem; // количество элементов в списке public: List() { pHead = nullptr; countElem = 0; } ~List() { while (pHead != NULL) { Element *pTemp = pHead; pHead = pHead->l_next; delete pTemp; } } void addToList(T data) { Element *temp; temp = new Element; if (pHead == NULL) pHead = temp; else pPrev->l_next = temp; temp->l_data = data; temp->l_next = NULL; pPrev = temp; ++countElem; } void printList() { Element *pTemp = pHead; while (pTemp != NULL) { std::cout << pTemp->l_data << " "; pTemp = pTemp->l_next; } std::cout << std::endl << "Количество: " << countElem << std::endl; } /* T & findElement(T &aim) { Element *pTemp = pHead; while (pTemp != nullptr) { if (pTemp->l_data == aim){ } else pTemp = pTemp->l_next; } }*/ }; 

And there is a class describing the "essence" of the bus:

  #include <cstring> class Bus { public: int b_numRoute; int b_numBus; char *b_Name; Bus() : b_numRoute(0), b_numBus(0), b_Name(nullptr) {} Bus(int numRoute, int numBus, const char *Name) { b_numBus = numBus; b_numRoute = numRoute; b_Name = new char[strlen(Name)]; strcpy(b_Name, Name); } ~Bus() { delete[]b_Name; } friend const bool operator == (Bus &bus1, Bus &bus2) { return bus2.b_numBus == bus1.b_numBus; } friend std::ostream &operator<<(std::ostream &s, Bus &bus) { s << "Номер маршрута: " << bus.b_numRoute << std::endl << "Номер автобуса: " << bus.b_numBus << std::endl; /*s << "Имя водителя: "; for (int i = 0; i < strlen(bus.b_Name); i++) s << bus.b_Name[i];*/ s << std::endl << std::endl; return s; } }; 

The question is: I need to create two lists, then by the bus number to find an element in one list, and add it to another. I tried to redefine equality in the Bus class and write a regular patterned search in the List template. But due to inexperience does not come out, please help advice how best to implement such a thing. I would be happy for examples. Thank you all in advance!

  • What does it mean "does not go" - VTT
  • one
    It seems all obvious: to iterate over the sheet until the element with the desired data is encountered. When we meet, we delete from one list, add to another. - free_ze
  • It’s not so easy to search for an element in the list by comparing data with c, but I don’t need to compare two Bus objects, but I only need to compare one field from Bus - Victor
  • @Victor, try to do the same as stl authors - pass the predicate to the search function. In general, everything has long been written . For the sake of sporting interest, you can write iterators for your list and use the standard algorithm for searching. Moreover, it will allow you to make your List friends at once with all the algorithms from the standard library - yrHeTaTeJlb
  • For this b_Name = new char[strlen(Name)]; strcpy(b_Name, Name); b_Name = new char[strlen(Name)]; strcpy(b_Name, Name); for little programmers, the tailbone is massaged :) - and the place for the terminating zero character? - Harry

0