Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C++ Compiler.
  4. Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <utility>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. struct Node
  16. {
  17. Node* prev;
  18. Node* next;
  19. double value;
  20. Node();
  21. Node(double& n)
  22. {
  23. value=n;
  24. prev=nullptr;
  25. next=nullptr;
  26. };
  27. Node(const Node& n)
  28. {
  29. value=n.value;
  30. next=n.next;
  31. prev=n.prev;
  32. };
  33. // prilikom čišćenja ispisujemo adresu
  34. ~Node()
  35. {
  36. cout<<"Delete node at: "<<this<<endl;
  37. };
  38.  
  39. //swap premješta sadržaj između dva čvora
  40. void swap(Node& n)
  41. {
  42. double x=n.value;
  43. n.value=value;
  44. value=x;
  45.  
  46.  
  47. };
  48. // ispisujemo adresu, adresu prethodnog, adresu sljedećeg te vrijednost
  49. void print() const
  50. {
  51. cout<<"Node at: "<<this<<", "<<"prev at: "<<this->prev<<", "<<"next at: "<<this->next<<", "<<"value: "<<value<<endl;
  52. };
  53. };
  54.  
  55. class CDLL {
  56. protected:
  57. Node* head{};
  58. Node* tail{};
  59. public:
  60. CDLL(){head=nullptr; tail=nullptr;};
  61. CDLL(const CDLL& c)
  62. {
  63. CDLL l;
  64. Node* temp=c.head;
  65. while(temp!=c.tail)
  66. {
  67.  
  68. if(head==nullptr and tail==nullptr)
  69. {
  70. Node *n= new Node(temp->value);
  71. n->next=head;
  72. n->prev=tail;
  73. head=n;
  74. tail=n;
  75.  
  76. }
  77. else
  78. {
  79. Node *n=new Node(temp->value);
  80. head->prev=n;
  81. tail->next=n;
  82. n->prev=tail;
  83. n->next=head;
  84. tail=n;
  85.  
  86.  
  87. }
  88.  
  89.  
  90.  
  91. temp=temp->next;
  92. }
  93.  
  94. if(head==nullptr and tail==nullptr)
  95. {
  96. Node *n= new Node(temp->value);
  97. n->next=head;
  98. n->prev=tail;
  99. head=n;
  100. tail=n;
  101.  
  102. }
  103. else
  104. {
  105. Node *n=new Node(temp->value);
  106. head->prev=n;
  107. tail->next=n;
  108. n->prev=tail;
  109. n->next=head;
  110. tail=n;
  111.  
  112.  
  113. }
  114. };
  115. ~CDLL()
  116. {
  117. Node* temp=head;
  118. while(temp!=tail)
  119. {
  120. temp=temp->next;
  121. Node* p=head;
  122. head=head->next;
  123. tail->next=head;
  124. delete p;
  125. }
  126. };
  127. bool empty() const
  128. {
  129. if(head==tail){return true;}
  130. else{return false;}
  131. };
  132. // postavi vrijednost na početak
  133. void prepend(double value)
  134. {
  135. if(head==nullptr and tail==nullptr)
  136. {
  137. Node *n= new Node(value);
  138. n->next=head;
  139. n->prev=tail;
  140. head=n;
  141. tail=n;
  142.  
  143. }
  144. else
  145. {
  146. Node *n=new Node(value);
  147. head->prev=n;
  148. tail->next=n;
  149. n->prev=tail;
  150. n->next=head;
  151. head=n;
  152.  
  153. }
  154. };
  155. // postavi vrijednost na kraj
  156. void append(double value)
  157. { if(head==nullptr and tail==nullptr)
  158. {
  159. Node *n= new Node(value);
  160. n->next=head;
  161. n->prev=tail;
  162. head=n;
  163. tail=n;
  164.  
  165. }
  166. else
  167. {
  168. Node *n=new Node(value);
  169. head->prev=n;
  170. tail->next=n;
  171. n->prev=tail;
  172. n->next=head;
  173. tail=n;
  174. };
  175. };
  176. // ukloni čvor s početka
  177. // i vrati njegovu vrijednost
  178. double removeFromHead()
  179. {
  180. double v=head->value;
  181. Node* p=head;
  182. head=head->next;
  183. tail->next=head;
  184. delete p;
  185. return v;
  186. };
  187. // ukloni čvor s kraja
  188. // i vrati njegovu vrijednost
  189. double removeFromTail()
  190. {
  191. double v=tail->value;
  192. Node* p=tail;
  193. tail=tail->prev;
  194. head->prev=tail;
  195. delete p;
  196.  
  197. };
  198. // ispisujemo adresu head-a, tail-a te sve čvorove
  199. void print() const
  200. {
  201. cout<<"Head at: "<<head<<", Tail at: "<<tail<<endl;
  202. Node* temp=head;
  203. while(temp!=tail)
  204. {
  205. //cout<<"Node at: "<<temp<<", "<<"prev at: "<<temp->prev<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
  206. temp->print();
  207. temp=temp->next;
  208. }
  209. temp->print();
  210. //cout<<"Node at: "<<temp<<", "<<"Node at: "<<temp->prev<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
  211. };
  212. void sort()
  213. {
  214. Node* temp=head;
  215. int i=0;
  216. while(temp!=tail)
  217. {
  218. i++;
  219. temp=temp->next;
  220. }
  221. i++;
  222.  
  223. for(int j=0;j<i;j++)
  224. {Node* temp1=head;
  225. while (temp1!=tail)
  226. {
  227. Node* temp2=temp1->next;
  228. if((temp1->value)<(temp2->value))
  229. {
  230. double v1=temp1->value;
  231. temp1->value=temp2->value;
  232. temp2->value=v1;
  233. }
  234. temp1=temp1->next;
  235. }
  236. }
  237.  
  238. };
  239. };
  240.  
  241.  
  242.  
  243.  
  244.  
  245. int main()
  246. {
  247.  
  248.  
  249.  
  250. CDLL* l1=new CDLL();
  251. l1->append(59.9);
  252. l1->append(10.0);
  253. l1->append(98.44);
  254. l1->append(16.7);
  255. l1->append(20.269);
  256. l1->append(1.5);
  257. l1->print();
  258.  
  259. CDLL* l2=new CDLL(*l1);
  260. l2->sort();
  261. l2->print();
  262. delete l1;
  263. delete l2;
  264.  
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement