Реализовать двунаправленный список и отсортировать его!
/*Athor Igor Dombrovsky
Realiztion SORT LIST <====>
*/
#include <iostream> // COUT
#include <time.h> // RAND
#include <stdlib.h> // RAND
using namespace std; // STD
#define TEXT_COLOR_CMD_WINDOWS_GREEN system("color 0A"); // COLOR CMD
#define STOP_CMD_WINDOWS system("pause"); // SYSTEM PAUSE
template<class T> // TEMPLATE CLASS LIST
class List
{
public:
List();
~List();
void addElement(T element);
void listSort();
int getSizeList();
void showListHead();
void showListTail();
private:
List * head;
List * tail;
List * next;
List * previuos;
T data;
int size;
};
template<class T>
List<T>::List() : size(0) // CONSTRUCTOR
{
head = tail = next = previuos = NULL;
}
template<class T>
List<T>::~List() // DESTRUCTOR
{
size = 0;
}
template<class T>
void List<T>::addElement(T element) // ADD ELEMENTS LIST
{
List * temp = new List;
if (head == NULL)
{
head = tail = temp;
head->previuos = NULL;
tail->next = NULL;
head->data = element;
this->size++;
}
else
{
tail->next = temp;
temp->previuos = tail;
tail = temp;
tail->data = element;
this->size++;
}
}
template<class T>
void List<T>::listSort() // BOOBLE SORT LIST
{
List * temp = new List;
List * indexHead = head;
List * indexTail = tail;
for (size_t i = 1; i <= this->size; i++)
{
for (size_t j = this->size - 1; j >= i; j--)
{
if (indexHead->data > indexTail->data)
{
temp->data = indexHead->data;
indexHead->data = indexTail->data;
indexTail->data = temp->data;
}
indexTail = indexTail->previuos;
}
indexHead = indexHead->next;
indexTail = tail;
}
delete temp;
}
template<class T>
int List<T>::getSizeList() // GET SIZE LIST
{
return this->size;
}
template<class T>
void List<T>::showListHead() // DISPLAY LIST MIN MAX
{
List * temp = head;
while (temp != NULL)
{
cout << temp->data << "\t";
temp = temp->next;
}
cout << endl;
}
template<class T>
void List<T>::showListTail() // DISPLAY LIST MAX MIN
{
List * temp = tail;
while (temp != NULL)
{
cout << temp->data << "\t";
temp = temp->previuos;
}
cout << endl;
}
int main() {
TEXT_COLOR_CMD_WINDOWS_GREEN // GREEN TEXT CMD BACKGROUND BLACK
srand(time(0)); // RAND TIME ZERO
List<int> l; // LIST
//*********************************************
// ADD LIST RANDOM ELEMENTS
//*********************************************
for (size_t i = 1; i <= 10; i++)
l.addElement(rand() % (i * 10));
//*********************************************
// DISPLAY LIST MIN MAX
//*********************************************
l.showListHead();
//*********************************************
// DISPLAY LIST MAX MIN
//*********************************************
l.showListTail();
//*********************************************
// GET SIZE LIST
//*********************************************
cout << "SIZE = " << l.getSizeList() << endl;
//*********************************************
// SORT LIST
//*********************************************
l.listSort();
//*********************************************
// DISPLAY LIST MIN MAX
//*********************************************
l.showListHead();
//*********************************************
// DISPLAY LIST MAX MIN
//*********************************************
l.showListTail();
//*********************************************
STOP_CMD_WINDOWS // SYSTEM PAUSE CMD WINDOWS
return 0;
}