Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #include "IntList.h"
- IntList::IntList(){
- head = nullptr;
- tail = nullptr;
- }
- IntList::IntList(const IntList &cpy){
- head = nullptr;
- tail = nullptr;
- IntNode* curr;
- if(cpy.head != nullptr){
- for(curr = cpy.head; curr->next != nullptr; curr = curr->next){
- push_back(curr->data);
- }
- push_back(curr->data);
- }
- }
- IntList::~IntList(){
- if(head != nullptr){
- IntNode *curr;
- for(curr = head; curr->next != nullptr; curr = (curr->next)){
- IntNode* temp = curr;
- delete temp;
- }
- }
- }
- void IntList::push_front(int num){
- IntNode* temp = new IntNode(num);
- temp->next = head;
- if(head != nullptr)
- temp->next = head;
- else
- tail = temp;
- head = temp;
- }
- void IntList::push_back(int value){
- IntNode* temp = new IntNode(value);
- if(tail != nullptr)
- tail->next = temp;
- else
- head = temp;
- tail = temp;
- }
- void IntList::pop_front(){
- if((head != tail) && tail != nullptr){
- IntNode* temp = head;
- delete temp;
- head = head->next;
- }
- else{
- tail = nullptr;
- head = tail;
- }
- }
- bool IntList::empty() const{
- return head == nullptr && tail == nullptr;
- }
- const int & IntList::front() const{
- //if(head != nullptr)
- return head->data;
- }
- const int & IntList::back() const{
- //if(tail != nullptr)
- return tail->data;
- }
- void IntList::selection_sort(){
- if(head != nullptr){
- IntNode *curr;
- IntNode *curr2;
- IntNode *minLoc;
- for(curr = head; curr->next != nullptr; curr = (curr->next)){
- int min = curr->data;
- minLoc = curr;
- for(curr2 = curr; curr2->next != nullptr; curr2 = (curr2->next)){
- if(curr2->data < min){
- minLoc = curr2;
- min = curr2->data;
- }
- }
- if(curr2->data < min){
- minLoc = curr2;
- min = curr2->data;
- }
- minLoc->data = curr->data;
- curr->data = min;
- }
- // int min = curr->data;
- // minLoc = curr;
- // for(curr2 = curr; curr2->next != nullptr; curr2 = (curr2->next)){
- // if(curr2->data < min){
- // minLoc = curr2;
- // min = *curr->data;
- // }
- // }
- // if(curr2->data < min){
- // minLoc = curr2;
- // min = curr->data;
- // }
- // minLoc->data = curr->data;
- // curr->data = min;
- // }
- }
- }
- void IntList::insert_ordered(int value){
- if(head != nullptr){
- IntNode *curr;
- bool flag = true;
- for(curr = head; curr->next != nullptr; curr = (curr->next)){
- if((value > curr->data && value < (curr->next)->data) || value == curr->data){
- IntNode* temp = new IntNode(value);
- temp->next = curr->next;
- curr->next = temp;
- flag = false;
- break;
- }
- // if(value == curr->data){
- // IntNode* temp = new IntNode(value);
- // temp->next = curr->next;
- // curr->next = temp;
- // flag = false;
- // break;
- // }
- }
- if(flag){
- if(value >= curr->data)
- push_back(value);
- else
- push_front(value);
- }
- }
- else
- push_front(value);
- }
- void IntList::remove_duplicates(){
- IntNode *curr;
- IntNode *curr2;
- if(head != nullptr){
- for(curr = head; curr != 0; curr = curr -> next) {
- IntNode *curr3;
- for(curr2 = curr, curr3 = curr2 -> next; curr3 != 0; curr3 = curr2 -> next) {
- if(curr3 -> data == curr -> data) {
- curr2 -> next = curr3 -> next;
- if(curr3 == tail)
- tail = curr2;
- delete curr3;
- }
- else
- curr2 = curr2 -> next;
- }
- }
- }
- }
- void IntList::clear(){
- while(head != nullptr){
- pop_front();
- }
- }
- IntList & IntList::operator=(const IntList &rhs){
- if(this == &rhs){
- return *this;
- }
- head = nullptr;
- tail = nullptr;
- IntNode* curr;
- if(rhs.head !=nullptr){
- for(curr = rhs.head; curr->next != nullptr; curr = curr->next){
- push_back(curr->data);
- }
- push_back(curr->data);
- }
- return *this;
- }
- ostream & operator<<(ostream &out, const IntList &list){
- if(list.head != nullptr){
- IntNode *curr;
- for(curr = list.head; curr->next != nullptr; curr = curr->next){
- out << curr->data << " ";
- }
- out << curr->data;
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement