Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. //lifo
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. typedef struct element {
  6. int info;
  7. struct element *next;
  8. struct element *prev;
  9. };
  10. class doublelist {
  11. private :
  12. int length;
  13. element *start,*current,*finalist;
  14. public :
  15. doublelist() { length=0; }
  16. int showlenght() {cout<<endl<<length;}
  17. int addLIFO(int thisvalue) {
  18. if(length == 0)
  19. {
  20. element * nou = new element;
  21. nou->info = thisvalue;
  22. start = nou;
  23. start->prev = NULL;
  24. start->next = NULL;
  25. finalist = start;
  26. length++;
  27. return 0;
  28. }
  29. element * nou = new element;
  30. nou->info = thisvalue;
  31. finalist->next = nou;
  32. finalist->next->prev = finalist;
  33. finalist = finalist->next;
  34. finalist->next = NULL;
  35. length++;
  36. return 0;
  37. }
  38. int showlist(bool direction)
  39. {
  40. if(length == 0)
  41. {
  42. return 0;
  43. }
  44. if(direction)
  45. {
  46. current = start;
  47. while(current != finalist->next)
  48. {
  49. cout<<current->info<<" ";
  50. current = current->next;
  51. }
  52. return 0;
  53. }
  54. current = finalist;
  55. while(current != start->prev)
  56. {
  57. cout<<current->info<<" ";
  58. current = current->prev;
  59. }
  60. }
  61. int delLIFO()
  62. {
  63. if(length == 0)
  64. {
  65. return 0;
  66. }
  67. else if(length == 1)
  68. {
  69. start->next = NULL;
  70. start->info = NULL;
  71. length --;
  72. return 0;
  73. }
  74. finalist->info = NULL;
  75. finalist = finalist->prev;
  76. finalist->next = NULL;
  77. length--;
  78. }
  79. protected :
  80. } somelist;
  81.  
  82.  
  83.  
  84. int main()
  85. {
  86. for(int i=1;i<=10;i++)
  87. {
  88. somelist.addLIFO(i);
  89. somelist.showlist(1);
  90. cout<<endl;
  91. }
  92. for(int i = 1; i <= 10; i++)
  93. {
  94. somelist.delLIFO();
  95. somelist.showlist(1);
  96. cout<<endl;
  97. }
  98. for(int i=1;i<=10;i++)
  99. {
  100. somelist.addLIFO(i);
  101. somelist.showlist(0);
  102. cout<<endl;
  103. }
  104. for(int i = 1; i <= 10; i++)
  105. {
  106. somelist.delLIFO();
  107. somelist.showlist(0);
  108. cout<<endl;
  109. }
  110. }
  111. ====================================================================
  112. //fifo
  113. #include<iostream>
  114. using namespace std;
  115. typedef struct element {
  116. int info;
  117. struct element *next;
  118. struct element *prev;
  119. };
  120. class doublelist {
  121. private :
  122. int length;
  123. element *start,*current,*finalist;
  124. public :
  125. doublelist() { length=0; start=NULL;current=NULL;finalist=NULL;}
  126. int addFIFO(int thisvalue)
  127. {
  128. if(length == 0)
  129. {
  130. element * nou = new element;
  131. nou->info = thisvalue;
  132. finalist = nou;
  133. finalist->next = NULL;
  134. finalist->prev = NULL;
  135. start = current = finalist;
  136. length++;
  137. return 0;
  138. }
  139. element * nou = new element;
  140. nou->info = thisvalue;
  141. start->prev = nou;
  142. start->prev->next = start;
  143. start = start->prev;
  144. start->prev = NULL;
  145. length++;
  146. }
  147. int delFIFO()
  148. {
  149. if(length == 0)
  150. {
  151. return 0;
  152. }
  153. else if(length == 1)
  154. {
  155. start->next = NULL;
  156. start->prev = NULL;
  157. start->info = NULL;
  158. start = current = finalist = NULL;
  159. length--;
  160. return 0;
  161. }
  162. start->info = NULL;
  163. start = start->next;
  164. //start->prev->next = NULL;
  165. start->prev = NULL;
  166. length--;
  167. }
  168. int printLIST(bool direction)
  169. {
  170. if(length == 0)
  171. {
  172. return 0;
  173. }
  174. if(direction)
  175. {
  176. current = start;
  177. while(current != finalist->next)
  178. {
  179. cout<<current->info<<" ";
  180. current = current->next;
  181. }
  182. return 0;
  183. }
  184. current = finalist;
  185. while(current != start->prev)
  186. {
  187. cout<<current->info<<" ";
  188. current = current->prev;
  189. }
  190.  
  191. }
  192. protected :
  193. };
  194.  
  195. doublelist somelist;
  196.  
  197. int main()
  198. {
  199. for(int i=1;i<=10;i++)
  200. {
  201. somelist.addFIFO(i);
  202. somelist.printLIST(1);
  203. cout<<endl;
  204. }
  205. for(int i = 1; i <= 10; i++)
  206. {
  207. somelist.delFIFO();
  208. somelist.printLIST(1);
  209. cout<<endl;
  210. }
  211. for(int i=1;i<=10;i++)
  212. {
  213. somelist.addFIFO(i);
  214. somelist.printLIST(0);
  215. cout<<endl;
  216. }
  217. for(int i = 1; i <= 10; i++)
  218. {
  219. somelist.delFIFO();
  220. somelist.printLIST(0);
  221. cout<<endl;
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement