macrofish

dopííči:/

Jun 28th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Data
  5. {
  6. private:
  7. int m_items;
  8. struct Node
  9. {
  10. int data;
  11. Node *next;
  12. Node *prev;
  13. };
  14. Node *head;
  15. Node *tail;
  16. Node *curr;
  17. public:
  18. Data();
  19. ~Data();
  20. bool isFull();
  21. bool isEmpty();
  22. void put_tail(int item);
  23. void put_head(int item);
  24. void stack();
  25. void queue();
  26. int count();
  27. void report();
  28. };
  29.  
  30. Data::Data()
  31. {
  32. tail = NULL;
  33. head = NULL;
  34. m_items = 0;
  35. }
  36.  
  37. Data::~Data()
  38. {
  39. Node *tmp;
  40. while (head!=NULL)
  41. {
  42. tmp=head;
  43. head=head->next;
  44. delete tmp;
  45. }
  46. tail = NULL;
  47. }
  48.  
  49. void Data::report()
  50. {
  51. cout << endl;
  52. if (m_items==0)
  53. {
  54. cout << "--structure is empty--" << endl;
  55. }
  56. else
  57. {
  58. cout << "items in structure are: " << endl;
  59. cout << "--------head--------" << endl;
  60. int i=0;
  61. for(Node *p=head;(i<m_items);p=p->next,i++)
  62. {
  63. cout << i+1 << ": " << p->data << endl;
  64. }
  65. cout << "--------tail-------" << endl;
  66. cout << endl;
  67.  
  68. }
  69. }
  70.  
  71. bool Data::isEmpty()
  72. {
  73. return (m_items == 0);
  74. }
  75.  
  76.  
  77. int Data::count()
  78. {
  79. return m_items;
  80. }
  81.  
  82. void Data::put_tail(int item)
  83. {
  84. cout << "putting item to the tail: " << item << endl;
  85. Node *add=new Node;
  86. add->data=item;
  87. add->next=NULL;
  88. add->prev=NULL;
  89.  
  90. m_items++;
  91.  
  92. if (head == NULL)
  93. {
  94. head = add;
  95. tail=add;
  96. }
  97. else
  98. {
  99. curr=tail;
  100. add->prev=curr;
  101. tail->next=add;
  102. }
  103. tail= add;
  104. curr=add;
  105. }
  106.  
  107. void Data::put_head(int item)
  108. {
  109. cout << "putting item to the head: " << item << endl;
  110. Node *add=new Node;
  111. add->data=item;
  112. add->next=NULL;
  113. add->prev=NULL;
  114.  
  115. m_items++;
  116.  
  117. if (head == NULL)
  118. {
  119. head=add;
  120. tail=add;
  121. }
  122. else
  123. {
  124. curr=head;
  125. add->next=curr;
  126. head->prev=add;
  127. }
  128. head=add;
  129. curr=add;
  130. }
  131.  
  132.  
  133. void Data::queue()
  134. {
  135. if (isEmpty())
  136. {
  137. cout << "structure is empty"<<endl;
  138. }
  139. else
  140. {
  141. Node *tmp;
  142. tmp=head;
  143. m_items--;
  144.  
  145. head->prev=NULL;
  146. head->next->prev=NULL;
  147. head->next=NULL;
  148. head=tmp->next;
  149.  
  150.  
  151. cout << "removing item from head: ";
  152. cout << tmp->data<<endl;
  153.  
  154. delete tmp;
  155.  
  156.  
  157. if (m_items==0)
  158. {
  159. head=NULL;
  160. tail=NULL;
  161. }
  162. }
  163. }
  164.  
  165. void Data::stack()
  166. {
  167. if (isEmpty())
  168. {
  169. cout << "structure is empty" <<endl;
  170. }
  171.  
  172.  
  173. else
  174. {
  175. Node *tmp;
  176. tmp=tail;
  177.  
  178.  
  179. tail->next=NULL;
  180. tail->prev->next=NULL;
  181. tail=tmp->prev;
  182.  
  183. cout << "removing item from tail: ";
  184. cout <<tmp->data<< endl;
  185. m_items--;
  186. delete tmp;
  187.  
  188. if (m_items==0)
  189. {
  190. head=NULL;
  191. tail=NULL;
  192. }
  193. }
  194. }
  195.  
  196.  
  197. int main()
  198. {
  199. Data data;
  200.  
  201. data.put_tail(1);
  202. data.put_tail(2);
  203. data.put_tail(3);
  204. data.put_tail(4);
  205. data.put_tail(5);
  206. data.report();
  207.  
  208. cout << endl;
  209.  
  210. data.queue();
  211. data.report();
  212. data.queue();
  213. data.report();
  214. data.queue();
  215. data.report();
  216. data.queue();
  217. data.report();
  218. data.queue();
  219. data.report();
  220.  
  221. cout << endl;
  222.  
  223. /*
  224.  
  225. data.put_head(1);
  226. data.report();
  227. data.put_head(2);
  228. data.report();
  229. data.put_head(3);
  230. data.report();
  231. data.put_head(4);
  232. data.report();
  233. data.put_head(5);
  234. data.report();
  235.  
  236. cout << endl;
  237. data.stack();
  238. data.report();
  239. data.stack();
  240. data.report();
  241. data.stack();
  242. data.report();
  243. data.stack();
  244. data.report();
  245. data.stack();
  246. data.report();
  247.  
  248.  
  249. cout<< endl;
  250.  
  251. data.put_head(1);
  252. data.report();
  253. data.put_head(2);
  254. data.report();
  255. data.put_tail(3);
  256. data.report();
  257. data.stack();
  258. data.report();
  259. */
  260. return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment