Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 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: "<<&prev<<", "<<"next at: "<<&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. bool empty() const
  117. {
  118. if(head==tail){return true;}
  119. else{return false;}
  120. };
  121. // postavi vrijednost na početak
  122. void prepend(double value)
  123. {
  124. if(head==nullptr and tail==nullptr)
  125. {
  126. Node *n= new Node(value);
  127. n->next=head;
  128. n->prev=tail;
  129. head=n;
  130. tail=n;
  131.  
  132. }
  133. else
  134. {
  135. Node *n=new Node(value);
  136. head->prev=n;
  137. tail->next=n;
  138. n->prev=tail;
  139. n->next=head;
  140. head=n;
  141.  
  142. }
  143. };
  144. // postavi vrijednost na kraj
  145. void append(double value)
  146. { if(head==nullptr and tail==nullptr)
  147. {
  148. Node *n= new Node(value);
  149. n->next=head;
  150. n->prev=tail;
  151. head=n;
  152. tail=n;
  153.  
  154. }
  155. else
  156. {
  157. Node *n=new Node(value);
  158. head->prev=n;
  159. tail->next=n;
  160. n->prev=tail;
  161. n->next=head;
  162. tail=n;
  163. };
  164. };
  165. // ukloni čvor s početka
  166. // i vrati njegovu vrijednost
  167. double removeFromHead()
  168. {
  169. double v=head->value;
  170. Node* p=head;
  171. head=head->next;
  172. tail->next=head;
  173. delete p;
  174. return v;
  175. };
  176. // ukloni čvor s kraja
  177. // i vrati njegovu vrijednost
  178. double removeFromTail()
  179. {
  180. double v=tail->value;
  181. Node* p=tail;
  182. tail=tail->prev;
  183. head->prev=tail;
  184. delete p;
  185.  
  186. };
  187. // ispisujemo adresu head-a, tail-a te sve čvorove
  188. void print() const
  189. {
  190. Node* temp=head;
  191. while(temp!=tail)
  192. {
  193. cout<<"prev at: "<<temp->prev<<", "<<"current at: "<<temp<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
  194. temp=temp->next;
  195. }
  196. cout<<"prev at: "<<temp->prev<<", "<<"current at: "<<temp<<", next at: "<<temp->next<<", "<<"value: "<<temp->value<<endl;
  197. };
  198. void sort()
  199. {
  200. Node* temp=head;
  201. int i=0;
  202. while(temp!=tail)
  203. {
  204. i++;
  205. temp=temp->next;
  206. }
  207. i++;
  208.  
  209. for(int j=0;j<i;j++)
  210. {Node* temp1=head;
  211. while (temp1!=tail)
  212. {
  213. Node* temp2=temp1->next;
  214. if((temp1->value)<(temp2->value))
  215. {
  216. double v1=temp1->value;
  217. temp1->value=temp2->value;
  218. temp2->value=v1;
  219. }
  220. temp1=temp1->next;
  221. }
  222. }
  223.  
  224. };
  225. };
  226.  
  227.  
  228.  
  229.  
  230.  
  231. int main()
  232. {
  233.  
  234. CDLL l;
  235. //cout<<l.empty()<<endl;
  236. l.append(5);
  237. l.append(2);
  238. l.append(8);
  239. l.append(4);
  240. l.sort();
  241. l.removeFromHead();
  242. l.print();
  243. //cout<<l.empty()<<endl;
  244. CDLL l1(l);
  245. l1.print();
  246.  
  247. return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement