Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /* односвязный список реализация */
- template<typename T>
- class List
- {
- public:
- List();
- ~List();
- //удаление первого элемента в списке
- void pop_front();
- // очистить список
- void clear();
- // получить количество элементов в списке
- int GetSize(){ return Size; }
- // перегруженный оператор []
- T& operator[](const int index);
- //добавление элемента в начало списка
- void push_front(T data);
- //замена местами 2-ух элементов в списке
- void swap_val(int ind_1, int ind_2);
- private:
- template<typename N>
- class Node
- {
- public:
- Node * pNext;
- T data;
- Node(T data = T(), Node *pNext = NULL)
- {
- this->data = data;
- this->pNext = pNext;
- }
- };
- int Size;
- Node<T> *head;
- };
- template<typename T>
- List<T>::List()
- {
- Size = 0;
- head = NULL;
- }
- 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>::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 != NULL){
- if (counter == index){
- return current->data;}
- current = current->pNext;
- counter++;
- }
- }
- template<typename T>
- void List<T>::push_front(T data){
- head = new Node<T>(data, head);
- Size++;
- }
- template<typename T>
- void List<T>::swap_val(int ind_1, int ind_2){
- Node<T> *p1, *prev_p1, *p2, *prev_p2, *tmp_pos;
- Node<T> *current = this->head;
- prev_p1 = head;
- prev_p2 = head;
- for(int i = 1; i < ind_1; i++) {
- prev_p2 = prev_p2->pNext;
- }
- p2 = prev_p2->pNext;
- for(int i = 1; i < ind_2; i++) {
- prev_p1 = prev_p1->pNext;
- }
- p1 = prev_p1->pNext;
- tmp_pos = p1->pNext;
- p1->pNext = p2->pNext;
- p2->pNext = tmp_pos;
- prev_p1->pNext = p2;
- prev_p2->pNext = p1;
- }
- int main(){
- setlocale(LC_ALL, "ru");
- List<int> lst;
- lst.push_front(7);
- lst.push_front(6);
- lst.push_front(5);
- lst.push_front(4);
- lst.push_front(3);
- lst.push_front(2);
- lst.push_front(1);
- lst.push_front(0);
- for (int i = 0; i < lst.GetSize(); i++){
- cout << lst[i] << " ";
- }
- cout << endl << "Введите индексы элементов которые хотите поменять местами" << endl << endl;
- cout << endl << "Первый индекс: " << endl << endl;
- int first_ind;
- while(!(cin >> first_ind) || (first_ind > lst.GetSize() - 1)){
- cout << "Ошибка, повторите ввод\n";
- cin.clear();
- fflush(stdin);
- }
- cout << endl << "Второй индекс: " << endl << endl;
- int second_ind;
- while(!(cin >> second_ind) || (first_ind > lst.GetSize() - 1)){
- cout << "Ошибка, повторите ввод\n";
- cin.clear();
- fflush(stdin);
- }
- lst.swap_val(first_ind, second_ind);
- cout << endl << "swap_val(" << first_ind << ", " << second_ind << ")"<< endl << endl;
- for (int i = 0; i < lst.GetSize(); i++){
- cout << lst[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement