Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- /*
- * односв€зный список
- * реализаци€
- */
- template<typename T>
- class List
- {
- public:
- List();
- ~List();
- //удаление первого элемента в списке
- void pop_front();
- //добавление элемента в конец списка
- void push_back(T data);
- // очистить список
- void clear();
- // получить количество елементов в списке
- int GetSize() { return Size; }
- // перегруженный оператор []
- T& operator[](const int index);
- //добавление элемента в начало списка
- void push_front(T data);
- //добавление элемента в список по указанному индексу
- void insert(T data, int index);
- //удаление элемента в списке по указанному индексу
- void removeAt(int index);
- //удаление последнего элемента в списке
- void pop_back();
- private:
- template<typename T1>
- class Node
- {
- public:
- Node * pNext;
- T data;
- Node(T1 data = T1(), Node *pNext = nullptr)
- {
- this->data = data;
- this->pNext = pNext;
- }
- };
- int Size;
- Node<T> *head;
- };
- template<typename T>
- List<T>::List()
- {
- Size = 0;
- head = nullptr;
- }
- template<typename T>
- List<T>::~List()
- {
- clear();
- }
- template<typename T>
- void List<T>::pop_front()
- {
- Node<T> *temp = head;
- head = head->pNext;
- delete temp;
- Size--;
- }
- template<typename T>
- void List<T>::push_back(T data)
- {
- if (head == nullptr)
- {
- head = new Node<T>(data);
- }
- else
- {
- Node<T> *current = this->head;
- while (current->pNext != nullptr)
- {
- current = current->pNext;
- }
- current->pNext = new Node<T>(data);
- }
- Size++;
- }
- template<typename T>
- void List<T>::clear()
- {
- while (Size)
- {
- pop_front();
- }
- }
- template<typename T>
- T & List<T>::operator[](const int index)
- {
- int counter = 0;
- Node<T> *current = this->head;
- while (current != nullptr)
- {
- if (counter == index)
- {
- return current->data;
- }
- current = current->pNext;
- counter++;
- }
- return current->data;
- }
- template<typename T>
- void List<T>::push_front(T data)
- {
- head = new Node<T>(data, head);
- Size++;
- }
- template<typename T>
- void List<T>::insert(T data, int index)
- {
- if (index == 0)
- {
- push_front(data);
- }
- else
- {
- Node<T> *previous = this->head;
- for (int i = 0; i < index - 1; i++)
- {
- previous = previous->pNext;
- }
- Node<T> *newNode = new Node<T>(data, previous->pNext);
- previous->pNext = newNode;
- Size++;
- }
- }
- template<typename T>
- void List<T>::removeAt(int index)
- {
- if (index == 0)
- {
- pop_front();
- }
- else
- {
- Node<T> *previous = this->head;
- for (int i = 0; i < index - 1; i++)
- {
- previous = previous->pNext;
- }
- Node<T> *toDelete = previous->pNext;
- previous->pNext = toDelete->pNext;
- delete toDelete;
- Size--;
- }
- }
- template<typename T>
- void List<T>::pop_back()
- {
- removeAt(Size - 1);
- }
- int main()
- {
- setlocale(LC_ALL, "ru");
- List<string> w;
- List<string> p;
- int n;
- string word,page;
- cout << "Введите количество слов: ";
- cin >> n;
- cin.ignore();
- for(int i=0;i<n;i++){
- cout << "Введите слово: ";
- getline(cin,word);
- w.push_back(word);
- cout << "Введите номера страниц: ";
- getline(cin,page);
- p.push_back(page);
- }
- cout << "Полученный предметный указатель: " << endl;
- for(int i=0;i<n;i++){
- cout << w[i] << " : ";
- cout << p[i] << " ";
- cout << endl;
- }
- cout << "Введите слово для которого хотите узнать страницы: ";
- string wo;
- getline(cin,wo);
- for(int i=0;i<n;i++){
- if(wo == w[i]){
- cout <<"Номера страниц для этого слова: " << p[i] << endl;
- }
- }
- cout << "Слово с максимальным количеством страниц: ";
- int max = 0;
- int count = 0;
- for(int i=0;i<n;i++){
- if(p[i].length() > max){
- max = p[i].length();
- count = i;
- }
- }
- cout << w[count] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement