Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <ctime>
  5. #include<windows.h>
  6. using namespace std;
  7. #define maxLevel 5;
  8.  
  9. struct node{
  10. double x;
  11. int key;
  12. node *next;
  13. node *up;
  14. node *bottom;
  15. node *prev;
  16. };
  17. bool flip()//for Randomly Generating LEVELS
  18. {
  19. return rand()%2;
  20.  
  21. }
  22. void add(node *pre, node *curr, double val)//Add NODE after the given node
  23. {
  24. if (pre->x>val) {
  25. cout<<"pret"<<pre->x<<endl;
  26.  
  27. cout<<"pre_aftert"<<pre->x<<endl;
  28.  
  29. }
  30.  
  31.  
  32. curr->next=pre->next;
  33. curr->prev=pre;
  34. pre->next=curr;
  35. curr->x=val;
  36. curr->up=NULL;
  37. curr->bottom=NULL;
  38.  
  39. }
  40. void display(node *temp)//Display
  41. {
  42. while (temp) {
  43. if (temp->prev) {
  44. cout<<temp->x<<"t";
  45. }
  46.  
  47. temp=temp->next;
  48. }
  49. cout<<endl;
  50. }
  51. node *bottom(node *temp){
  52. return temp->bottom;
  53. }
  54. node *up(node *temp){
  55. return temp->up;
  56. }
  57. node *prev(node *temp){
  58. return temp->prev;
  59. }
  60. node *next(node *temp){
  61. return temp->next;
  62. }
  63. node *maxNode(node *temp){
  64. while (temp->up) {
  65. temp=temp->up;
  66. }
  67. return temp;
  68. }
  69. node *maxNode(node *temp, int level){
  70. for (int i=2; i<=level; i++) {
  71. temp=up(temp);
  72. }
  73. return temp;
  74. }
  75. node *position(node *temp, double val){
  76. // cout<<temp->x<<endl;
  77. int le=1;
  78. while (temp->bottom) {
  79.  
  80. if (temp->next) {
  81. node *a=temp->next;
  82.  
  83. while (temp->x<=val && temp->next && a->x<=val )
  84. {
  85.  
  86. a=a->next;
  87.  
  88. temp=temp->next;
  89.  
  90. }
  91. }
  92.  
  93.  
  94. temp=temp->bottom;
  95. ++le;
  96. }
  97.  
  98. node *v=temp->next;
  99. while (temp->next && temp->x<=val && v->x<=val) {
  100.  
  101. temp=temp->next;
  102. v=v->next;
  103. }
  104.  
  105. return temp;
  106. }
  107. void insertAFterAbove(node *temp, node *curr){
  108. node *a=temp->next;
  109. curr->next=a;
  110. if (a) {
  111. a->prev=curr;
  112. }
  113.  
  114. curr->prev=temp;
  115. temp->next=curr;
  116.  
  117. }
  118. node* level(node *temp, int lev){
  119. node *curr=new node;
  120. temp->up=curr;
  121. curr->x=temp->x;
  122. curr->bottom=temp;
  123. curr->next=NULL;
  124. curr->prev=NULL;
  125. while (temp->prev && !temp->up) {
  126. temp=prev(temp);
  127. }
  128. valid:
  129. int check=1;
  130. while (check!=lev && temp->up ) {
  131. temp=up(temp);
  132. ++check;
  133. }
  134. if (check!=lev) {
  135. temp=prev(temp);
  136. while (temp->prev && !temp->up) {
  137. temp=prev(temp);
  138. }
  139. goto valid;
  140. }
  141. insertAFterAbove(temp, curr);
  142. return curr;
  143. }
  144. node* creaTenode(node *curr){
  145. node *temp=new node;
  146. temp->x=curr->x;
  147. temp->bottom=curr;
  148. temp->up=NULL;
  149. temp->next=NULL;
  150. temp->prev=NULL;
  151. curr->up=temp;
  152. return temp;
  153.  
  154. }
  155. void deleteNode(node *temp){
  156. node *pre=temp->prev;
  157.  
  158. pre->next=temp->next;
  159. temp=temp->next;
  160. if (temp) {
  161. temp->prev=pre;
  162. }
  163.  
  164. }
  165. void menu(){
  166. cout<<"1 == > INSERTn";
  167. cout<<"2 == > DELETEn";
  168. cout<<"3 == > DISPLAYn";
  169. }
  170. int main(int argc, const char * argv[])
  171. {
  172. srand (time(NULL));
  173. node *start;
  174. start=new node;
  175. start->next=NULL;
  176. start->bottom=NULL;
  177. start->up=NULL;
  178. start->prev=NULL;
  179. start->x=-888888888888888;
  180. node *temp=start;
  181. int max=maxLevel;
  182. for (int i=2; i<=max; i++) {
  183. node *curr=new node;
  184. curr->next=NULL;
  185. curr->bottom=temp;
  186. curr->next=NULL;
  187. curr->prev=NULL;
  188. curr->up=NULL;
  189. curr->x=i*-888888888888888;
  190. temp->up=curr;
  191. temp=curr;
  192. }
  193. cout<<endl;
  194. while (1) {
  195. system("cls");
  196. system("color 4f");
  197. if (start->next) {
  198.  
  199. }
  200. else {
  201. cout<<"LIST EMPTY!nn";
  202. }
  203. menu();
  204. int choice;
  205. cout<<"YOUR CHOICE :t";
  206. cin>>choice;
  207. system("color 30");
  208. switch (choice) {
  209. case 1:
  210. {
  211. system("cls");
  212.  
  213. //Initializing...............
  214. double num;
  215. temp=start;
  216. cout<<"ENTER THE NUMBER :t";
  217. cin>>num;
  218. if(!start->next)
  219. {
  220. temp=start;
  221. node *curr=new node;
  222. curr->prev=start;
  223. start->next=curr;
  224. curr->x=num;
  225. curr->up=NULL;
  226. curr->bottom=NULL;
  227. curr->next=NULL;
  228. }
  229. else {
  230. temp=position(maxNode(temp), num);
  231. node *curr=new node;
  232. add(temp, curr, num);
  233. int check=1;
  234. while (check!=max) {
  235. int coin=flip();
  236. if (!coin) {
  237. break;
  238. }
  239.  
  240. curr=creaTenode(curr);
  241. while (!temp->up) {
  242.  
  243. temp=temp->prev;
  244.  
  245.  
  246. }
  247.  
  248. temp=temp->up;
  249.  
  250. insertAFterAbove(temp, curr);
  251. ++check;
  252.  
  253. }
  254.  
  255. }
  256. system("pause");
  257. break;
  258.  
  259. }
  260. case 2:{
  261. system("cls");
  262. if (start->next) {
  263. cout<<"n==================n";
  264. display(start);
  265. cout<<"n==================n";
  266. cout<<"ENTER THE VALUE TO DELETE :t";
  267. double del;
  268. cin>>del;
  269. temp=start;
  270. temp=position(temp, del);
  271. if (temp->x==del) {
  272. while (temp) {
  273. deleteNode(temp);
  274. temp=temp->up;
  275. }
  276. cout<<"n------->DELETED!n";
  277. }
  278. else {
  279. system("color 4f");
  280. cout<<"Incorrect Number entered!n";
  281. }
  282.  
  283. }
  284. else {
  285. system("color 4f");
  286. cout<<"n==================n";
  287. cerr<<"OOOOOOOPS! LIST IS EMPTY__!n";
  288. cout<<"n==================n";
  289. }
  290. system("pause");
  291. break;
  292.  
  293. }//case-----2----ends
  294. case 3:{
  295. system("cls");
  296. if (start->next) {
  297. cout<<"n==================n";
  298.  
  299. temp=start;
  300.  
  301. display(temp);
  302.  
  303. cout<<"n==================n";
  304. }
  305. else {
  306. system("color 4f");
  307. cout<<"n==================n";
  308. cerr<<"OOOOOOOPS! LIST IS EMPTY__!n";
  309. cout<<"n==================n";
  310. }
  311. system("pause");
  312. break;
  313. }//case 3 ___ENDs;;;;;;
  314. default:{
  315.  
  316. system("color 4f");
  317. cerr<<"INCORRRECT CHOICEn";
  318. system("pause");
  319. break;
  320. }
  321. }
  322. }
  323.  
  324. return 0;
  325. }
  326.  
  327. void add(node *pre, node *curr, double val)//Add NODE after the given node
  328. {
  329. if (pre->x>val) {
  330. cout<<"pret"<<pre->x<<endl;
  331.  
  332. cout<<"pre_aftert"<<pre->x<<endl;
  333.  
  334. }
  335.  
  336.  
  337. curr->next=pre->next;
  338. curr->prev=pre;
  339. pre->next=curr;
  340. curr->x=val;
  341. curr->up=NULL;
  342. curr->bottom=NULL;
  343.  
  344. }
  345.  
  346. void add(node *pre, node *curr, double val)//Add NODE after the given node
  347. {
  348. if (pre->x>val) {
  349. cout<<"pret"<<pre->x<<endl;
  350. cout<<"pre_aftert"<<pre->x<<endl;
  351. }
  352. curr->next=pre->next;
  353. curr->prev=pre;
  354. pre->next=curr;
  355. curr->x=val;
  356. curr->up=NULL;
  357. curr->bottom=NULL;
  358. }
  359.  
  360. #include<windows.h>
  361.  
  362. using std::cout;
  363.  
  364. bool flip()//for Randomly Generating LEVELS
  365. {
  366. return rand()%2;
  367.  
  368. }
  369.  
  370. srand(time(0));
  371.  
  372. // ...
  373.  
  374. rand();
  375.  
  376. #define maxLevel 5;
  377.  
  378. const int maxLevel = 5;
  379.  
  380. constexpr int maxLevel = 5;
  381.  
  382. system("pause");
  383. system("cls");
  384. system("color 4f");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement