Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdlib>
  4. #include <stdlib.h>
  5. #include <ctime>
  6. #include <conio.h>
  7. #include <stdio.h>
  8.  
  9.  
  10. using namespace std;
  11. //int LMAX=10;
  12.  
  13. struct skiplist{
  14. int key;
  15. int height;
  16. char litera;
  17. struct skiplist **next;
  18. };
  19.  
  20. int random_level(int LMAX)
  21. {
  22. int level=1;
  23. while((rand()%100<0.5*100)&(level<LMAX))
  24. {
  25. level++;
  26. }
  27. return(level);
  28. }
  29.  
  30.  
  31. bool insert(skiplist* head, int new_key, int LMAX)
  32. {
  33. skiplist* x=head;
  34. int height=random_level(LMAX);
  35. skiplist* update[height];
  36.  
  37.  
  38. for(int i=height-1;i>=0;i--)
  39. {
  40. while(x->next[i]->key < new_key)
  41. {
  42. x=x->next[i];
  43. }
  44. update[i]=x;
  45.  
  46. }
  47. x=x->next[0];
  48. if(x->key==new_key)
  49. {
  50. x->litera='D';
  51. return false;
  52. }
  53. skiplist* new_node = new skiplist;
  54. new_node->next=new skiplist*[height];
  55.  
  56. new_node->key=new_key;
  57. new_node->height=height;
  58.  
  59. for(int i=0; i<new_node->height;i++)
  60. {
  61. new_node->next[i]=update[i]->next[i];
  62. update[i]->next[i]=new_node;
  63. new_node->litera='T';
  64. }
  65. return true;
  66. }
  67.  
  68. skiplist *createSkipList(int LMAX)
  69. {
  70. skiplist* skip = new skiplist;
  71. skiplist* tail = new skiplist;
  72. skip->next=new skiplist*[LMAX];
  73. tail->next=new skiplist*[LMAX];
  74. int height=LMAX;
  75.  
  76. skip->key=-2147483647;
  77. skip->height=LMAX;
  78.  
  79. tail->key=2147483647;
  80. tail->height=LMAX;
  81. for(int i=0;i<height;i++)
  82. {
  83. skip->next[i]=tail;
  84. tail->next[i]=nullptr;
  85. }
  86. return skip;
  87. }
  88.  
  89. bool search(skiplist* head, int key)
  90. {
  91. skiplist* x = new skiplist;
  92. x = head;
  93. int height=x->height;
  94. for(int i=height-1;i>=0;i--)
  95. {
  96. while(x->next[i]->key<key)
  97. {
  98. x=x->next[i];
  99. }
  100. }
  101. x=x->next[0];
  102. if(x->key==key)
  103. {
  104. return(true);
  105. }
  106. return(false);
  107. }
  108.  
  109. void insert_all(skiplist* head, int N, int LMAX)
  110. {
  111. int liczba;
  112. for(int i=0; i<N;i++)
  113. {
  114. do
  115. {
  116. liczba = (rand() % 99900)+ 99;
  117. }while(insert(head,liczba, LMAX)==false);
  118. }
  119. }
  120.  
  121.  
  122. string usun(skiplist* head, int key, int LMAX)
  123. {
  124. skiplist* x=head;
  125. skiplist* update[10];
  126. for(int i=LMAX-1; i>=0;i--)
  127. {
  128. while(x->next[i]->key<key)
  129. {
  130. x=x->next[i];
  131. }
  132. update[i]=x;
  133. }
  134. x=x->next[0];
  135. if(x->key>key)
  136. {
  137. return("blad");
  138. }
  139. for(int i=0; i<x->height; i++)
  140. {
  141. update[i]->next[i]=x->next[i];
  142. }
  143. delete(x);
  144. return("OK");
  145. }
  146.  
  147. string delete_all(skiplist* head)
  148. {
  149. skiplist* element=head;
  150. skiplist* tmp;
  151.  
  152. while(element)
  153. {
  154. tmp=element->next[0];
  155. delete(element->next);
  156. delete(element);
  157. element=tmp;
  158. }
  159.  
  160. }
  161.  
  162. void wypisz(skiplist* head, int Y, int height)
  163. {
  164. skiplist* x=head;
  165. /*for(int i=0; i<Y; i++)
  166. {
  167. if(x->key==-2147483647)
  168. {
  169. x=x->next[0];
  170. }
  171. if(x->height>=height)
  172. {
  173. cout<< x->key<< endl;
  174. x=x->next[0];
  175. continue;
  176. }
  177. else
  178. {
  179. while(x->height<height)
  180. {
  181. x=x->next[0];
  182. }
  183. cout<< x->key<<endl;
  184. x=x->next[0];
  185. }
  186.  
  187.  
  188. }*/
  189. for(int i=0; i<Y; i++)
  190. {
  191. x=x->next[height-1];
  192. cout<<x->key<<endl;
  193. }
  194.  
  195.  
  196. }
  197.  
  198. int liczba_wezlow(skiplist* head)
  199. {
  200. int licznik=1;
  201. skiplist* x=head;
  202. while(x->next[0]!=nullptr)
  203. {
  204. x=x->next[0];
  205. licznik++;
  206. }
  207. licznik=licznik-2;
  208. return licznik;
  209. }
  210.  
  211.  
  212.  
  213. int main()
  214. {
  215. int LMAX=7;
  216. //2001 7 13666 4 7 -1 100001
  217. srand((unsigned int)time(NULL));
  218. int N=2001;
  219. int k1=13666;
  220. int k2=4;
  221. int k3=7;
  222. int k4=-1;
  223. int k5=100001;
  224.  
  225.  
  226. clock_t begin, end;
  227. double time_spent;
  228. begin = clock();
  229.  
  230. skiplist* head= new skiplist;
  231.  
  232. head=createSkipList(LMAX);
  233.  
  234. insert_all(head,N,LMAX);
  235.  
  236.  
  237. if(search(head,101)==false)
  238. {
  239. cout<<"Nie znaleziono"<<endl;
  240. }
  241. else
  242. {
  243. cout<<"Znaleziono klucz k1"<<endl;
  244. }
  245.  
  246.  
  247.  
  248. search(head,100);
  249. insert(head,100,LMAX);
  250.  
  251. skiplist* wyswietl=head;
  252. for(int i=0;i<13;i++)
  253. {
  254. cout<<wyswietl->key<<" "<<wyswietl->litera<<" "<< wyswietl->height<<" "<<" "<<wyswietl<<" "<< wyswietl->next[0]<<" ";
  255. if(wyswietl->height>=2)
  256. {
  257. cout<<wyswietl->next[2]<<" ";
  258. }
  259. if(wyswietl->height>=2)
  260. {
  261. cout<<wyswietl->next[1]<<endl;
  262. }
  263. wyswietl=wyswietl->next[0];
  264. }
  265. cout << "" <<endl;
  266.  
  267. usun(head,100,LMAX);
  268.  
  269.  
  270. //wypisz(head, 10, 3);
  271.  
  272. wyswietl=head;
  273.  
  274.  
  275.  
  276. int licznik;
  277.  
  278. licznik=liczba_wezlow(head);
  279.  
  280. delete_all(head);
  281.  
  282. cout << "Liczba wezlow:" << licznik << endl;
  283.  
  284. time_spent = (double)(end-begin) / CLOCKS_PER_SEC;
  285. cout << "Czas wykonania:" << time_spent << endl;
  286.  
  287.  
  288.  
  289. return 0;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement