Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include "IntList.h"
- IntList::IntList() {
- headNode = NULL;
- tailNode = NULL;
- }
- IntList::~IntList() {
- IntNode *temp = 0;
- while (headNode != NULL) {
- temp = headNode->next;
- delete headNode;
- headNode = temp;
- }
- }
- void IntList::display() const {
- IntNode *temp = 0;
- temp = headNode;
- while (temp != NULL) {
- cout << " " << temp->data;
- temp = temp->next;
- }
- return;
- }
- void IntList::pop_front() {
- IntNode *temp = 0;
- temp = headNode;
- headNode = headNode->next;
- delete temp;
- if (empty()) {
- headNode = tailNode = 0;
- }
- return;
- }
- bool IntList::empty() const {
- if (headNode == NULL) {
- return true;
- } else {
- return false;
- }
- }
- void IntList::push_front(int value) {
- IntNode *temp = new IntNode(value); //default constructor for node struct
- //temp is a pointer that points to newly constructed node
- temp->data = value;
- temp->next = headNode; //next points to headNode
- headNode = temp; //temp becomes headNode
- return;
- }
- IntList::IntList(const IntList& cpy) {
- headNode = tailNode = 0;
- if (!cpy.empty()) {
- IntNode *n = new IntNode(0);
- *n = *(cpy.headNode); //????
- while (n != NULL) {
- push_back(n->data);
- n = n->next;
- }
- }
- return;
- }
- IntList & IntList::operator=(const IntList &rhs) {
- if (rhs.empty()) {
- headNode = tailNode = 0;
- return *this;
- } else if(rhs.empty()) {
- headNode = 0;
- tailNode = 0;
- }
- else if (this != &rhs) {
- clear(); //replaces
- IntNode *n = 0;
- n = rhs.headNode;
- while (n != NULL) {
- push_back(n->data);
- n = n->next;
- }
- }
- return *this;
- }
- void IntList::push_back(int value) {
- IntNode *newTail = new IntNode(value);
- if (headNode == NULL) {
- headNode = newTail;
- tailNode = newTail;
- } else {
- tailNode->next = newTail;
- tailNode = newTail;
- }
- return;
- }
- void IntList::selection_sort() {
- if (empty()) {
- return;
- } else {
- IntNode *min = 0;
- IntNode *first = 0;
- IntNode *second = 0;
- for (first = headNode; first->next != NULL; first = first->next) {
- min = first; //min points to whatever first is pointing to
- for (second = first->next; second != NULL; second = second->next) {
- if (second->data < min->data) {
- min = second;
- }
- }
- int temp = 0;
- temp = first->data;
- first->data = min->data;
- min->data = temp;
- }
- }
- return;
- }
- void IntList::insert_ordered(int value) {
- /*IntNode *newNode = new IntNode(value);
- newNode->data = value;
- IntNode *n = 0;
- IntNode *temp = 0;
- //compare newNode's data with list's data
- for (n = headNode; n->next != NULL; n = n->next) {
- if ((n->data < value) && ((n->next)->data > value)) {
- temp = n->next;
- n->next = newNode;
- newNode->next = temp;
- break;
- }
- }
- return;*/
- push_back(value);
- selection_sort();
- return;
- }
- void IntList::remove_duplicates() { //selection sort algorithm required?
- if (empty()) {
- return;
- } else {
- IntNode *first = 0;
- IntNode *second = 0;
- IntNode *previous = 0;
- for (first = headNode; first!= NULL; first = first->next) {
- previous = first;
- for (second = first->next; second != NULL; second = previous->next) {
- if (second->data == first->data) {
- if (second == tailNode) {
- delete second;
- tailNode = previous;
- tailNode->next = NULL;
- } else {
- previous->next = second->next;
- delete second;
- }
- } else {
- previous = previous->next;
- }
- }
- }
- }
- }
- ostream & operator<<(ostream &out, const IntList &rhs) {
- if (rhs.empty()) {
- return out;
- } else {
- IntNode *n = new IntNode(0);
- n = rhs.headNode;
- out << n->data;
- while (n->next != NULL) {
- n = n->next;
- out << " " << n->data;
- }
- }
- return out;
- }
- void IntList::clear() {
- while (!empty()) {
- pop_front();
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement