Advertisement
rchandler

6am

Feb 19th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. #include "IntList.h"
  6.  
  7. IntList::IntList(){
  8.     head = nullptr;
  9.     tail = nullptr;
  10. }
  11.  
  12. IntList::IntList(const IntList &cpy){
  13.     head = nullptr;
  14.     tail = nullptr;
  15.     IntNode* curr;
  16.     if(cpy.head != nullptr){
  17.         for(curr = cpy.head; curr->next != nullptr; curr = curr->next){
  18.             push_back(curr->data);
  19.         }
  20.         push_back(curr->data);
  21.     }
  22. }
  23.  
  24. IntList::~IntList(){
  25.     if(head != nullptr){
  26.         IntNode *curr;
  27.         for(curr = head; curr->next != nullptr; curr = (curr->next)){
  28.             IntNode* temp = curr;
  29.             delete temp;
  30.         }
  31.     }
  32. }
  33.  
  34. void IntList::push_front(int num){
  35.     IntNode* temp = new IntNode(num);
  36.     temp->next = head;
  37.     if(head != nullptr)
  38.         temp->next = head;
  39.     else
  40.         tail = temp;
  41.    
  42.     head = temp;
  43. }
  44.  
  45. void IntList::push_back(int value){
  46.     IntNode* temp = new IntNode(value);
  47.     if(tail != nullptr)
  48.         tail->next = temp;
  49.     else
  50.         head = temp;      
  51.     tail = temp;
  52. }
  53.    
  54. void IntList::pop_front(){
  55.     if((head != tail) && tail != nullptr){
  56.         IntNode* temp = head;
  57.         delete temp;
  58.         head = head->next;
  59.     }
  60.     else{
  61.         tail = nullptr;
  62.         head = tail;
  63.     }
  64. }
  65.  
  66. bool IntList::empty() const{
  67.     return head == nullptr && tail == nullptr;
  68. }
  69.  
  70. const int & IntList::front() const{
  71.     //if(head != nullptr)
  72.         return head->data;
  73. }
  74.  
  75. const int & IntList::back() const{
  76.     //if(tail != nullptr)
  77.         return tail->data;
  78. }
  79.    
  80. void IntList::selection_sort(){
  81.     if(head != nullptr){
  82.         IntNode *curr;
  83.         IntNode *curr2;
  84.         IntNode *minLoc;
  85.         for(curr = head; curr->next != nullptr; curr = (curr->next)){
  86.             int min = curr->data;
  87.             minLoc = curr;
  88.             for(curr2 = curr; curr2->next != nullptr; curr2 = (curr2->next)){
  89.                 if(curr2->data < min){
  90.                     minLoc = curr2;
  91.                     min = curr2->data;
  92.                 }
  93.             }
  94.             if(curr2->data < min){
  95.                 minLoc = curr2;
  96.                 min = curr2->data;
  97.             }
  98.             minLoc->data = curr->data;
  99.             curr->data = min;
  100.         }
  101. //         int min = curr->data;
  102. //         minLoc = curr;
  103. //         for(curr2 = curr; curr2->next != nullptr; curr2 = (curr2->next)){
  104. //             if(curr2->data < min){
  105. //                 minLoc = curr2;
  106. //                 min = *curr->data;
  107. //             }
  108. //         }
  109. //         if(curr2->data < min){
  110. //             minLoc = curr2;
  111. //             min = curr->data;
  112. //         }
  113. //         minLoc->data = curr->data;
  114. //         curr->data = min;
  115. //     }
  116. }
  117. }
  118.  
  119. void IntList::insert_ordered(int value){
  120.     if(head != nullptr){
  121.         IntNode *curr;
  122.         bool flag = true;
  123.         for(curr = head; curr->next != nullptr; curr = (curr->next)){
  124.             if((value > curr->data && value < (curr->next)->data) || value == curr->data){
  125.                 IntNode* temp = new IntNode(value);
  126.                 temp->next = curr->next;
  127.                 curr->next = temp;
  128.                 flag = false;
  129.                 break;
  130.             }
  131. //             if(value == curr->data){
  132. //                 IntNode* temp = new IntNode(value);
  133. //                 temp->next = curr->next;
  134. //                 curr->next = temp;
  135. //                 flag = false;
  136. //                 break;
  137. //             }
  138.         }
  139.         if(flag){
  140.             if(value >= curr->data)
  141.                 push_back(value);
  142.             else
  143.                 push_front(value);
  144.         }
  145.     }
  146.     else
  147.         push_front(value);
  148. }
  149. void IntList::remove_duplicates(){
  150.     IntNode *curr;
  151.     IntNode *curr2;
  152.     if(head != nullptr){
  153.         for(curr = head; curr != 0; curr = curr -> next) {
  154.             IntNode *curr3;
  155.             for(curr2 = curr, curr3 = curr2 -> next; curr3 != 0; curr3 = curr2 -> next) {
  156.                 if(curr3 -> data == curr -> data) {
  157.                     curr2 -> next = curr3 -> next;
  158.                     if(curr3 == tail)
  159.                         tail = curr2;
  160.                     delete curr3;
  161.                 }
  162.                 else
  163.                     curr2 = curr2 -> next;
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169. void IntList::clear(){
  170.     while(head != nullptr){
  171.         pop_front();
  172.     }
  173. }
  174.  
  175. IntList & IntList::operator=(const IntList &rhs){
  176.     if(this == &rhs){
  177.         return *this;
  178.     }
  179.     head = nullptr;
  180.     tail = nullptr;
  181.     IntNode* curr;
  182.     if(rhs.head !=nullptr){
  183.         for(curr = rhs.head; curr->next != nullptr; curr = curr->next){
  184.             push_back(curr->data);
  185.         }
  186.         push_back(curr->data);
  187.     }
  188.     return *this;
  189. }
  190.  
  191. ostream & operator<<(ostream &out, const IntList &list){
  192.     if(list.head != nullptr){
  193.         IntNode *curr;
  194.         for(curr = list.head; curr->next != nullptr; curr = curr->next){
  195.             out << curr->data << " ";
  196.         }
  197.         out << curr->data;
  198.     }
  199.     return out;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement