include iostream include time include stdlib using namespace std typed

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
typedef enum{
_circle,
_rectangle,
_ellipse
}typeOfFigure;
typedef struct NODE{
void* data;
NODE* nextNode;
};
NODE* createList(int count){
srand(time(0));
NODE *head = new NODE, *currentNode = new NODE, *nextNode = new NODE;
head->nextNode = head;
currentNode = head;
for (int i = 0; i < count; i++) {
typeOfFigure type = (typeOfFigure)(rand()%3 + 0);
nextNode = new NODE;
switch (type) {
case _circle:
nextNode->data = new double(rand()%6 + 5);
break;
case _rectangle:
nextNode-> data = new int(rand()%6 + 5);
break;
case _ellipse:
nextNode-> data = new char(rand()%6 + 5);
break;
default:
nextNode-> data = new char(rand()%6 + 5);
break;
}
currentNode->nextNode = nextNode;
nextNode->nextNode = head;
currentNode = nextNode;
}
return head;
}
bool deleteNode(NODE* head, int indexOfNode){
NODE* currentNode, *nextNode;
if (head != NULL) {
currentNode = head->nextNode;
nextNode=currentNode->nextNode;
} else{
return false;
}
int i = 1;
while (currentNode != head){
if (i == indexOfNode - 1) {
currentNode->nextNode = nextNode->nextNode;
return true;
} else{
currentNode = currentNode->nextNode;
}
nextNode=currentNode->nextNode;
i++;
}
return false;
}
void BubbleSort(NODE* head){
NODE *currentNode, *nextNode, *temp;
while(true){
bool flag = true;
for (currentNode = head, nextNode = currentNode->nextNode; nextNode->nextNode != head; currentNode = currentNode->nextNode, nextNode = currentNode->nextNode) {
if (nextNode->data > (nextNode->nextNode)->data) { // если следующее меньше предыдущего
currentNode->nextNode = nextNode->nextNode;
nextNode->nextNode = (nextNode->nextNode)->nextNode;
(nextNode->nextNode)->nextNode = nextNode;
// currentNode->nextNode = nextNode->nextNode;
}
else{
continue;
}
flag = false; // на данном шаге сортировку выполнили, переходим на следующий
}
if(flag) break;
}
}
int main(){
NODE *list = createList(10);
for(NODE *currentNode = list->nextNode, *nextNode = currentNode->nextNode; nextNode !=list; currentNode = nextNode, nextNode = currentNode->nextNode){
cout << currentNode->data << "\n";
}
BubbleSort(list);
cout << "/////////////";
for(NODE *currentNode = list->nextNode, *nextNode = currentNode->nextNode; nextNode !=list; currentNode = nextNode, nextNode = currentNode->nextNode){
cout << currentNode->data << "\n";
}
return 0;
}