Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include "intLinkedList.h"
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. Element::Element(){
  6. next=nullptr;
  7. }
  8. Element::Element(int num): value{num}{
  9. next=nullptr;
  10. }
  11. Element::Element(const Element & el){
  12. next=el.next;
  13. value=el.value;
  14. }
  15. Element::~Element(){
  16.  
  17. }
  18.  
  19. IntLinkedList::IntLinkedList():head{nullptr},tail{nullptr}{
  20. strcpy(name,"LX");
  21. }
  22. IntLinkedList::IntLinkedList(const char *nazwa):head{nullptr},tail{nullptr}{
  23. strcpy(name,nazwa);
  24. }
  25.  
  26. IntLinkedList::~IntLinkedList(){
  27. Element *ptr = head;
  28. while(ptr!=tail){
  29. ptr=ptr->next;
  30. delete head;
  31. head=ptr;
  32. }
  33. delete tail;
  34. head=nullptr;
  35. tail=nullptr;
  36. if(isEmpty()){
  37. std::cout << "Destruktor "<< name <<": Lista pusta" << std::endl;
  38. }
  39. }
  40.  
  41.  
  42. bool IntLinkedList::isEmpty(){
  43. if (head==nullptr){
  44. return true;
  45. }
  46. else{
  47. return false;
  48. }
  49. }
  50. bool IntLinkedList::contains(int num){
  51. if(!isEmpty()){
  52. Element *ptr = head;
  53. while((ptr!=nullptr)){
  54. if(ptr->value==num){
  55. return true;
  56. }
  57. ptr=ptr->next;
  58. }
  59. return false;
  60. }
  61. else
  62. return false;
  63. }
  64.  
  65. int IntLinkedList::size(){
  66. if (isEmpty()){
  67. return 0;
  68. }
  69. else{
  70. Element * ptr = head;
  71. int counter = 0;
  72. while(ptr!=tail){
  73. counter++;
  74. ptr=ptr->next;
  75. }
  76. counter++;
  77. return counter;
  78. }
  79. }
  80. void print(IntLinkedList &lista){
  81.  
  82.  
  83. }
  84.  
  85. void IntLinkedList::print(){
  86. if(!isEmpty()){
  87. std::cout << "[";
  88. int size = this->size();
  89. Element *ptr = head;
  90. for(int i=0;i<size-1;i++){
  91. std::cout << ptr->value << ", ";
  92. ptr=ptr->next;
  93. }
  94. std::cout << tail->value << "]" << std::endl;
  95. }
  96.  
  97.  
  98. }
  99. void IntLinkedList::append(Element &el){
  100. if(isEmpty()){
  101. prepend(el);
  102. }
  103. else{
  104. tail->next=&el;
  105. tail=&el;
  106. }
  107.  
  108. }
  109.  
  110. void IntLinkedList::append(IntLinkedList &lista){}
  111.  
  112.  
  113. void IntLinkedList::prepend(Element &el){
  114. if (isEmpty()){
  115. head=&el;
  116. tail=&el;
  117. }
  118. else{
  119. el.next=head;
  120. head=&el;
  121. }
  122.  
  123. }
  124.  
  125. void IntLinkedList::prepend(int num){
  126. if (isEmpty()){
  127. Element * newElement = new Element(num);
  128. head=newElement;
  129. tail=newElement;
  130. }
  131. else{
  132.  
  133. Element * newElement = new Element(num);
  134. newElement->next=head;
  135. head=newElement;
  136. }
  137.  
  138. }
  139.  
  140. void IntLinkedList::addSorted(IntLinkedList &lista){
  141.  
  142. }
  143. void IntLinkedList::addSorted(Element &el, bool uniq){
  144. if(uniq==true){
  145. if(contains(el.value)){}
  146. else{
  147. if(isEmpty() || el.value<head->value){
  148. prepend(el);
  149. }
  150. else{
  151. if(el.value>tail->value){
  152. tail->next=&el;
  153. tail=&el;
  154. }
  155. else{
  156. Element *ptr=head;
  157. while(ptr->value<el.value){
  158. ptr=ptr->next;
  159. }
  160.  
  161. el.next=ptr->next;
  162. ptr->next=&el;
  163. }
  164. }
  165. }
  166. }
  167. else{
  168. if(isEmpty() || el.value<head->value){
  169. prepend(el);
  170. }
  171. else{
  172. if(el.value>tail->value){
  173. tail->next=&el;
  174. tail=&el;
  175. }
  176. else{
  177. Element *ptr=head;
  178. while(ptr->value<el.value){
  179. ptr=ptr->next;
  180. }
  181.  
  182. el.next=ptr->next;
  183. ptr->next=&el;
  184. }
  185. }
  186.  
  187.  
  188. }
  189.  
  190.  
  191.  
  192. }
  193. void IntLinkedList::addSorted(int num,bool uniq){
  194. if(uniq==true){
  195. if(contains(num)){}
  196. else{
  197. if(isEmpty() || num<head->value){
  198. prepend(num);
  199. }
  200. else{
  201. if(num>tail->value){
  202. Element *el = new Element(num);
  203. tail->next=el;
  204. tail=el;
  205. }
  206. else{
  207. Element *ptr=head;
  208. while(ptr->value<num){
  209. ptr=ptr->next;
  210. }
  211. Element *el = new Element(num);
  212. el->next=ptr->next;
  213. ptr->next=el;
  214. }
  215. }
  216. }
  217. }
  218. else{
  219. if(isEmpty() || num<head->value){
  220. prepend(num);
  221. }
  222. else{
  223. if(num>tail->value){
  224. Element *el = new Element(num);
  225. tail->next=el;
  226. tail=el;
  227. }
  228. else{
  229. Element *ptr=head;
  230. while(ptr->value<num){
  231. ptr=ptr->next;
  232. }
  233. Element *el = new Element(num);
  234. el->next=ptr->next;
  235. ptr->next=el;
  236. }
  237. }
  238.  
  239.  
  240. }
  241.  
  242. }
  243.  
  244. void IntLinkedList::removeFirst(){
  245. if(!isEmpty()){
  246. Element *ptr = new Element(*(head->next));
  247. delete head;
  248. head=ptr;
  249.  
  250. }
  251.  
  252. }
  253. void IntLinkedList::removeLast(){
  254. if(!isEmpty()){
  255. if(size()==1){
  256. delete tail;
  257. tail=nullptr;
  258. head=nullptr;
  259. }
  260. else{
  261. Element *ptr=head;
  262. for(int i=0;i<size()-2;i++){
  263. ptr=ptr->next;
  264. }
  265. delete tail;
  266. tail = ptr;
  267. tail->next=nullptr;
  268. }
  269. }
  270. }
  271. void IntLinkedList::removeValue(int num){
  272. if(!isEmpty() && contains(num)){
  273. Element *ptr = head;
  274. Element *before = head;
  275. while(ptr->value!=num){
  276. before=ptr;
  277. ptr=ptr->next;
  278. }
  279. if (ptr==tail){
  280. removeLast();
  281. }
  282. else if (ptr==head){
  283. ptr=ptr->next;
  284. delete head;
  285. head=ptr;
  286. }
  287. else{
  288. before->next=ptr->next;
  289. delete ptr;
  290. }
  291. }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement