The task: to create a class - a doubly linked list, to create a constructor, a destructor. Methods of working with the list: add, delete items, sort the list, search in it.

Sketched a small class structure, with C ++ almost not familiar, perhaps there is a more correct way to implement.

Is it correct to enter data directly in the designer? Or for this you need to create a private method and call it already?

struct tList { int key; tList * prev; tList * next; }; class List { private: tList * beginList; tList * endList; tList* getElement(void){ //выделение памяти //заполнение данных в структуре } public: List(){ //ввод данных в список //вызов addElement } void addElementEnd(){ //вызов getElement //добавление элемента в конец списка } void addElementBegin(){ //вызов getElement //добавление элемента в начало списка } void deleteElement(tList * element){ // удаление элемента } void sortAsc(){ // по возростанию } void sortDesc(){ // по убыванию } tList* searchElement(int key){ //поиск элемента } void printList(){ //печать списка } ~List(){ //освобождение памяти } }; 
  • Explain what input is in question? In the constructor, no data is entered. - Cerbo
  • I meant the indication of the number of elements in the list and the subsequent call to addElement - Hardc0re

2 answers 2

For a start, it is better to make the tList structure the internal structure of the List class, as it relates to the details of the list implementation.

The class must have a default constructor so that you can create an empty list.

You can also create a constructor that accepts the initialization list std::initializer_list<int> , and immediately generates a list of the elements of this initialization list. In this case, you can use in the body of the constructor both the open function addElementEnd , which adds elements to the end of the list, and a separately-written private class function, if its writing is justified by its greater efficiency compared to the open class function.

As for specifying an array and its length as parameters for the constructor, this is at your discretion. In the general case, users themselves elementwise in a loop can add new elements to an already created list. They might as well have added items from any other container to the list. Do not write for each such container, which serves as a source of elements for the list, a separate list constructor?

In the C ++ standard for standard containers, such a constructor is defined. This is a constructor that takes two iterators (the third parameter has an argument by default). But this is a template constructor. Here's what he looks like

 template <class InputIterator> list(InputIterator first, InputIterator last, const Allocator& = Allocator()); 

If you do not want to communicate with templates, you can write a similar constructor that takes two pointers: the beginning of the array and the place after the last element of the array. That is, it will look like a specialization of the standard template constructor for concrete iterators of const int * .

 List( const int *first, const int *last ); 

or you can as you originally indicated

 List( const int a[], size_t n ); 

In all other cases, in addition to copying lists, this is a class user who must add new elements to the class using the class's public interface.

    Yes, of course, you can and even need to create as many constructors as you expect from the use of class objects. Unless your list has any special function.

    In general, when implementing any containers, I recommend that you be guided first and foremost by design solutions from the standard library of STL templates.

    Also, as offtopic, getElement not a good name, it is better to call createElement or makeElement . Just get assumes access to an existing object.