backward up forward refresh
home
login
about

:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

using namespace std;

// lista

struct list {
    int data;
    list *next;
    };
struct bst {
    int data;
    int key;
    bst *left;
    bst *right;
    };
list *ListHead = NULL;
list *ListCurrent = ListHead;
int ListMax = 0;
bst *BSTRoot = NULL;

//    wstawianie elementu

void list_insert (int data) {
    if (!ListHead) {
        ListCurrent = new list ();
        ListCurrent->next = NULL;
        ListCurrent->data = data;
        ListHead = ListCurrent;
        ListMax = data;
        return;
        }
    ListCurrent = ListHead;
    list *ListPrevious = NULL;
    while (data > ListCurrent->data) {
        ListPrevious = ListCurrent;
        if (ListCurrent->data == ListMax) {
            ListCurrent = NULL;
            break;
            }
        else ListCurrent = ListCurrent->next;
        }
    if (data > ListMax) ListMax = data;
    list *ListNext = ListCurrent;
    ListCurrent = new list ();
    ListCurrent->next = ListNext;
    ListCurrent->data = data;
    if (ListPrevious) ListPrevious->next = ListCurrent;
    else ListHead = ListCurrent;
    }

//    usuwanie listy

void list_delete () {
    ListCurrent = ListHead;
    list *ListTemp;
    while (ListMax) {
        if (ListCurrent->data == ListMax && !ListCurrent->next) {
            delete ListCurrent;
            break;
            }
        ListTemp = ListCurrent->next;
        delete ListCurrent;
        ListCurrent = ListTemp;
        }
    ListHead = NULL;
    ListMax = 0;
    }

// Binary Search Tree

//    wstawianie elementu

void bst_insert (int data) {
    bst * BSTTemp;
    bst * BSTNode;
    bst * BSTNodeParent;
    int Mode;
    BSTNode = new bst ();
    BSTNode->data = data;
    BSTNode->left = NULL;
    BSTNode->right = NULL;
    BSTNodeParent = BSTRoot;
    while (1) {
        if (BSTNodeParent == NULL) {
            if (!BSTRoot) BSTRoot = BSTNode;
            else {
                if (Mode) BSTTemp->right = BSTNode;
                else BSTTemp->left = BSTNode;
                }
            return;
            }
        BSTTemp = BSTNodeParent;
        if (BSTNode->data < BSTNodeParent->data) {
            BSTNodeParent = BSTNodeParent->left;
            Mode = 0;
            }
        else {
            BSTNodeParent = BSTNodeParent->right;
            Mode = 1;
            }
        }
    }

//    usuwanie BST

void bst_delete (bst * BST) {
    bst *BSTTemp;
    if (!BST) return;
    bst_delete (BST->left);
    bst_delete (BST->right);
    BSTTemp = BST;
    delete BSTTemp;
    BST = NULL;
    }

//    wyswietlanie BST

void bst_show (bst * BST) {
    if (BST == NULL) return;
    bst_show (BST->left);
    bst_show (BST->right);
    cout << "\t" << BST->data << "\n";
    }

// pobieranie czasu

double time () {
    struct timeval tv;
    struct timezone tz;
    gettimeofday (&tv, &tz);
    return (((1.0 * tv.tv_usec) / 1000000) + tv.tv_sec);
    }

// program glowny

int main () {
    int Option = 1, Temp, Step = 100;
    double Start;
    bool Mode = 0;
    char FileName[100];
    fstream File;
    cout << "Wybierz strukture danych:\n"
    << "\t0 - lista\n"
    << "\t1 - BST\n";
    cin >> Mode;
    while (Option) {
        fflush (stdin);
        cout << "Wybierz opcje:\n"
        << "\t1 - wprowadz pojedyncza liczbe;\n"
        << "\t2 - wprowadz liczby z pliku;\n"
        << "\t3 - usun dane;\n"
        << "\t4 - wyswietl dane;\n"
        << "\t5 - przeprowadz pomiary;\n"
        << "\t0 - koniec programu.\n";
        fflush (stdin);
        cin >> Option;
        switch (Option) {
            case 1:
            cout << "Podaj liczbe:\n";
            cin >> Temp;
            if (Mode) bst_insert (Temp);
            else list_insert (Temp);
            break;
            case 2:
                cout << "Podaj nazwe pliku do odczytu:\n";
                fflush (stdin);
                cin >> FileName;
                File.open (FileName, ios::out);
                if (File.fail ()) {
                    cout << "Blad podczas proby otwarcia pliku!\n";
                    break;
                    }
                while (!File.eof ()) {
                    File >> Temp;
                    if (Mode) bst_insert (Temp);
                    else list_insert (Temp);
                    }
                File.close ();
            break;
            case 3:
                if (Mode) {
                    bst_delete (BSTRoot);
                    BSTRoot = NULL;
                    }
                else list_delete ();
                cout << "Struktura zostala usunieta z pamieci.\n";
            break;
            case 4:
                cout << "Zawartosc struktury:\n";
                if (Mode) {
                    if (BSTRoot != NULL) bst_show (BSTRoot);
                    else cout << "Struktura nie zawiera elementow.\n";
                    }
                else {
                    if (!ListHead) cout << "Struktura nie zawiera elementow.\n";
                    ListCurrent = ListHead;
                    while (ListMax && ListCurrent != NULL) {
                        cout << "\t" << ListCurrent->data << "\n";
                        ListCurrent = ListCurrent->next;
                        }
                    }
            break;
            case 5:
                cout << "Podaj wielkosc kroku (od 1 do 10000):\n";
                cin >> Step;
                cout << "Badanie struktury " << (Mode?"BST":"listy") << " z krokiem " << Step << ":\n";
                for (int i = 1; i < 16; i++) {
                    cout << "\t" << (i * Step) << " elementow:\n";
                    File.open ("data.txt", ios::in);
                    if (File.fail ()) {
                        cout << "Wystapil blad podczas proby otwarcia pliku!\n";
                        break;
                        }
                    Start = time ();
                    for (int j = 0; j < (i * Step); j++) {
                        File >> Temp;
                        if (Mode) bst_insert (Temp);
                        else list_insert (Temp);
                        }
                    File.close ();
                    cout << "\t\tTworzenie: " << ((time () - Start) * 1000) << " ms\n";
                    Start = time ();
                    if (Mode) {
                        bst_delete (BSTRoot);
                        BSTRoot = NULL;
                        }
                    else list_delete ();
                    cout << "\t\tUsuwanie: " << ((time () - Start) * 1000) << " ms\n";
                    }
            break;
            case 0:
                cout << "Koniec programu.\n";
                return 0;
            default:
                cout << "Niewlasciwa opcja!\n";
            }
        }
    return 0;
    }
Source file (CPP), size: 5.08 KB