fork download
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Node
  7. {
  8. public :
  9. int data;
  10. Node* next;
  11.  
  12. Node() { next = NULL; }
  13. Node(int d) { data = d; next = NULL; }
  14. };
  15.  
  16. class LinkList
  17. {
  18. private :
  19. Node* first;
  20. Node* last;
  21. int size;
  22.  
  23. public :
  24. LinkList() { first = last = NULL , size = 0; }
  25.  
  26.  
  27. // baraye tamame tavabe dar surati ke betunin kar khaste shode ro anjam bedin true bargardunin
  28. // dar gheyre in surat false bargardunid
  29.  
  30. bool AddFirst(int x)
  31. {
  32. // node jadid ba meghdare x ro be ebtedaye link ezafe konid
  33. Node* t = new Node(x);
  34.  
  35. if (first == NULL)
  36. first = last = t;
  37. else
  38. {
  39. t->next = first;
  40. first = t;
  41. }
  42. size++;
  43.  
  44. return true;
  45. }
  46.  
  47. bool AddLast(int x)
  48. {
  49. // ... enteha ...
  50. Node* t = new Node(x);
  51.  
  52. if (first == NULL)
  53. first = last = t;
  54. else
  55. {
  56. last->next = t;
  57. last = t;
  58. }
  59. size++;
  60.  
  61. return true;
  62. }
  63.  
  64. bool AddMiddle(int x , int pos)
  65. {
  66. // node jadid ro ruye andise pos garar bedin , yani :
  67. // -> pos==0 : be avvale list ezafe konid
  68. // -> pos==size : be entehaye list ezafe konid
  69. // -> pos>0 && pos<size : node jadid ro ghabl az node x'th garar bedin ta andisesh mosaviye x beshe
  70. // mesal : 7 -> 3 -> 6 -> 1
  71. // add(5,2)
  72. // 7 -> 3 -> 5 -> 6 -> 1
  73. // -> else : andis na motabare va bayad false bargardunid;
  74.  
  75. if (pos<0 || pos>size) return false;
  76.  
  77. if (pos==0) return AddFirst(x);
  78. if (pos==size) return AddLast(x);
  79.  
  80. Node* help = first;
  81. for (int i=0 ; i<pos-1 ; i++)
  82. help = help->next;
  83.  
  84. Node* t = new Node(x);
  85. t->next = help->next;
  86. help->next = t;
  87. size++;
  88.  
  89. return true;
  90. }
  91.  
  92.  
  93. // baraye delete ha
  94. // age emkane delete kardan vujud dashte bashe -> meghdare delete shode ro be komake x be karbar ettela bedid va true resturn konid
  95. // dar gheyre insurat false bargardunid
  96. // be onovane mesal be tabe zir nega konid
  97.  
  98. bool DeleteFirst(int& x)
  99. {
  100. // Hazfe ozve avval
  101. if (first == NULL) return false;
  102.  
  103. Node* t = first; // copy az first baraye delete kardan
  104. if (first == last) last = NULL;
  105. first = first->next;
  106.  
  107. x = t->data; // zakhireye meghdare dakhele in node
  108. delete t; // delete kardane hafezeye gerefte shode
  109. size--;
  110.  
  111. return true;
  112. }
  113.  
  114. bool DeleteLast(int& x)
  115. {
  116. // Hazve ozve akhar
  117.  
  118. if (first == NULL) return false;
  119.  
  120. x = last->data;
  121. if (first == last)
  122. {
  123. delete first;
  124. first = last = NULL;
  125. }
  126. else
  127. {
  128. Node* help = first;
  129. while ( help->next != last )
  130. help = help->next;
  131.  
  132. delete last;
  133. last = help;
  134. last->next = NULL;
  135. }
  136.  
  137. size--;
  138. return true;
  139. }
  140.  
  141. bool DeleteMiddle(int& x, int pos)
  142. {
  143. // Hazfe ozve pos'th , andisha az sefr shuru mishan yani [0 .. size-1]
  144. return true;
  145. }
  146.  
  147.  
  148. bool GetFirst(int& x) { return true; }
  149. bool GetLast(int& x) { return true; }
  150. bool GetMiddle(int&x , int pos) { return true; }
  151.  
  152. int Size() { return size; }
  153.  
  154. void Print()
  155. {
  156. cout<<"Size = "<<size<<" : ";
  157.  
  158. Node* help = first;
  159. while (help!=NULL)
  160. {
  161. cout<<help->data<<" ";
  162. help = help->next;
  163. }
  164. cout<<"\n";
  165. }
  166.  
  167. void Clean()
  168. {
  169. // azaye link list ro kamel pak konid
  170. }
  171.  
  172. void Reverse()
  173. {
  174. // link list ro makus konid
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. // ---------------------------------------------------------------- Ekhtiyari ha --------------------------------------------------------------
  182.  
  183. void Sort_n2() {}
  184. void Sort_nlogn() {}
  185. void Unique() {} // hazfe tekrari ha
  186. LinkList Merge(LinkList l) // tarkibe 2 link liste morattab shode
  187. {
  188. LinkList ans;
  189. // ...
  190. return ans;
  191. }
  192. };
  193.  
  194. int main()
  195. {
  196. int x;
  197. LinkList l;
  198.  
  199. l.AddFirst(3);
  200. l.AddFirst(5);
  201. l.AddFirst(7);
  202. l.AddLast(10);
  203. l.Print();
  204.  
  205. l.DeleteFirst(x);
  206. l.DeleteLast(x);
  207. l.DeleteFirst(x);
  208. l.Print();
  209.  
  210. l.DeleteFirst(x);
  211. l.DeleteFirst(x);
  212. l.DeleteFirst(x);
  213. l.DeleteFirst(x);
  214. l.Print();
  215.  
  216. l.AddFirst(8);
  217. l.AddLast(10);
  218. l.AddMiddle(11 , 1);
  219. l.AddMiddle(12 , 1);
  220. l.AddMiddle(13 , 3);
  221. l.Print();
  222.  
  223. return 0;
  224. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Size = 4 : 7 5 3 10 
Size = 1 : 3 
Size = 0 : 
Size = 5 : 8 12 11 13 10