Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <windows.h>
- using namespace std;
- class simple_list {
- struct element {
- int data;
- element* next;
- element* prev;
- element(int _data, element* _next, element* _prev) : data(_data), next(_next), prev(_prev) {}
- };
- element* head;
- element* tail;
- int num;
- void swap(element* first, element* second) {
- if (first == nullptr || second == nullptr) return;
- if (first == tail || second == head) swap(first, second);
- bool neighbours = first->next == second;
- element* fPrev = first->prev;
- element* sNext = second->next;
- if (!neighbours) {
- element* fNext = first->next;
- element* sPrev = second->prev;
- fNext->prev = second;
- second->next = fNext;
- sPrev->next = first;
- first->prev = sPrev;
- }
- if (fPrev != nullptr) {
- fPrev->next = second;
- } else {
- head = second;
- }
- second->prev = fPrev;
- if (sNext != nullptr) {
- sNext->prev = first;
- } else {
- tail = first;
- }
- first->next = sNext;
- if (neighbours) {
- first->prev = second;
- second->next = first;
- }
- }
- public:
- class list_iterator {
- element* data;
- public:
- list_iterator(element* _data) : data(_data) {}
- bool operator== (const list_iterator right) {
- return data == right.data;
- }
- bool operator != (const list_iterator right) {
- return !(*this == right);
- }
- list_iterator operator++ (int unused) {
- if (data == nullptr) {
- throw new exception();
- }
- element* temp = data;
- data = data->next;
- return *(new list_iterator(temp));
- }
- int operator* () {
- if (data == nullptr) throw new exception();
- return data->data;
- }
- };
- simple_list() {
- head = nullptr;
- tail = nullptr;
- num = 0;
- }
- simple_list(vector<int> arr) : simple_list() {
- for (int i = 0; i < arr.size(); i ++) {
- push_back(arr[i]);
- }
- }
- void push_back(int n) {
- if (num != 0) {
- tail->next = new element(n, nullptr, tail);
- tail = tail->next;
- } else {
- head = new element(n, nullptr, nullptr);
- tail = head;
- }
- num ++;
- }
- list_iterator begin() {
- return list_iterator(head);
- }
- list_iterator end() {
- return list_iterator(nullptr);
- }
- void swap(int i, int j) {
- if (i < 0 || j < 0 || i >= num || j >= num) return;
- if (i > j) swap(i, j);
- element* elI = head;
- element* elJ = nullptr;
- for (int k = 0; k < i; k ++) {
- elI = elI->next;
- }
- elJ = elI;
- for (int k = i; k < j; k ++) {
- elJ = elJ->next;
- }
- swap(elI, elJ);
- }
- void print() {
- cout << endl;
- element* temp = head;
- while(temp != nullptr) {
- cout << temp->data << ' ';
- temp = temp->next;
- }
- }
- void sort() {
- if (num <= 1) return;
- for (int i = num; i > 0; i --) {
- element* tempL = head;
- for (int j = 1; j < i; j ++) {
- if (tempL->data > tempL->next->data) {
- swap(tempL, tempL->next);
- } else {
- tempL = tempL->next;
- }
- }
- }
- }
- int size() {
- return num;
- }
- };
- int main()
- {
- srand(time(0));
- vector<int> kek;
- for (int i = 11; i > 0; i --) {
- kek.push_back(rand() % 50);
- }
- simple_list lol(kek);
- lol.print();
- lol.sort();
- lol.print();
- cout << endl;
- for (auto a = lol.begin(); a != lol.end(); a ++) {
- cout << *a << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment