Tests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include "DoubleLinkedList.hpp" int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; DoubleLinkedList * lineTwo = new DoubleLinkedList(ORANGE); lineTwo->insertStart((char*)"Window of World"); lineTwo->insertStart((char*)"Keyuan"); lineTwo->insertStart((char*)"Houhai"); lineTwo->insertLast((char*)"Xiang Mi"); lineTwo->insertLast((char*)"Futian"); lineTwo->insertLast((char*)"Grand Theatre"); lineTwo->insertLast((char*)"Hu Bei"); lineTwo->insertLast((char*)"Xin Xiu"); lineTwo->insertStart((char*)"Wan Xia"); lineTwo->insertStart((char*)"Sea World"); lineTwo->insertStart((char*)"Shek Kou Port"); lineTwo->insertStart((char*)"Chi Wan"); lineTwo->insertAfter((char*)"Sea World", (char*)"Shui Wan"); lineTwo->insertAfter((char*)"Shui Wan", (char*)"Dong Jiao Tou"); lineTwo->insertAfter((char*)"Wan Xia", (char*)"Dengliang"); lineTwo->insertAfter((char*)"Dengliang", (char*)"Hai Yue"); lineTwo->insertAfter((char*)"Keyuan", (char*)"Hong Shu Wan"); lineTwo->displayForward(); lineTwo->displayBackward(); delete lineTwo; return 0; } |
.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#ifndef DoubleLinkedList_hpp #define DoubleLinkedList_hpp #include <stdio.h> #include <iostream> enum Color { GREEN=1, ORANGE=2, LIGHT_BLUE=3, RED=4, PURPLE=5 }; struct Node { char * locationName; struct Node * previous; struct Node * next; //constructor Node(char * newLocationName, struct Node * newNext, struct Node * newPrevious) { this->locationName = newLocationName; this->next = newNext; this->previous = newPrevious; } void log() { std::cout << "[" << this->locationName << "]"; } }; // declares the struct type //a list if a metro line class DoubleLinkedList { private: struct Node * first; struct Node * last; Color metroLineColor; struct Node * find (char * nameOfStation); public: DoubleLinkedList(Color lineColor); ~DoubleLinkedList(); void insertStart(char * nameOfStation); void deletStart(); void insertLast(char * nameOfStation); void deleteLast(); void insertAfter(char * nameOfTargetStation, char * nameOfStationToInsert); void displayForward(); void displayBackward(); }; #endif /* DoubleLinkedList_hpp */ |
.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
// // DoubleLinkedList.cpp // DoublyLinkedList // // Created by Ricky Tsao on 12/23/16. // Copyright © 2016 Epam. All rights reserved. // #include "DoubleLinkedList.hpp" DoubleLinkedList::DoubleLinkedList(Color lineColor) { std::cout << "Constructor" << std::endl; this->metroLineColor = lineColor; this->first = NULL; this->last = NULL; } DoubleLinkedList::~DoubleLinkedList() { std::cout << "\n\n========= Deconstructing Metro Line ==========" << std::endl; for (struct Node * temp = this->first; temp != NULL;) { struct Node * toDelete = temp; temp = temp->next; //go forward once step std::cout << "Deleting station " << toDelete->locationName << std::endl; toDelete->previous = NULL; toDelete->next = NULL; toDelete->locationName = NULL; delete toDelete; } this->first = NULL; this->last = NULL; } void DoubleLinkedList::insertStart(char * nameOfStation) { if(this->first == NULL && this->last == NULL) { std::cout << "List is empty" << std:: endl; this->first = new (struct Node)(nameOfStation, NULL, NULL); this->last = this->first; } else { std::cout << "List has items, let's insert at the very first station:" << this->first->locationName << std:: endl; struct Node * temp = new (struct Node)(nameOfStation, this->first, NULL); this->first->previous = temp; this->first = temp; } std::cout << "done" << std::endl; } void DoubleLinkedList::insertLast(char * nameOfStation) { if(this->first == NULL && this->last == NULL) { std::cout << "List is empty" << std:: endl; this->first = new (struct Node)(nameOfStation, NULL, NULL); this->last = this->first; } else { std::cout << "List is empty" << std:: endl; // <--[]--NULL struct Node * temp = new (struct Node)(nameOfStation, NULL, this->last); this->last->next = temp; this->last = temp; } } struct Node * DoubleLinkedList::find (char * nameOfStation) { std::cout << " looking for station: " << nameOfStation << std::endl; for ( struct Node * temp = this->first; temp != NULL; temp = temp->next) { if(strcasecmp(temp->locationName, nameOfStation)==0) { std::cout << " found the station: " << nameOfStation << std::endl; return temp; } } std::cout << "station not found!" << std::endl; return NULL; } void DoubleLinkedList::insertAfter(char * nameOfTargetStation, char * nameOfStationToInsert) { struct Node * found = this->find(nameOfTargetStation); struct Node * temp = new (struct Node)(nameOfStationToInsert, NULL, NULL); if(found) { temp->next = found->next; temp->previous = found; found->next->previous = temp; found->next = temp; } else { std::cout << "can't insert. target station not found" << nameOfTargetStation << std::endl; } } void DoubleLinkedList::displayForward() { std::cout << "\n\n------- display all stations forward ---------" << std::endl; if (this->first == NULL) { std::cout << "EMPTY: No station(s)" << std::endl; return; } std::cout << "||"; for (struct Node * temp = this->first; temp != NULL; temp = temp->next) { temp->log(); } std::cout << "||"; } void DoubleLinkedList::displayBackward() { std::cout << "\n\n------- display all stations backwards ---------" << std::endl; std::cout << "||"; for (struct Node * temp = this->last; temp != NULL; temp = temp->previous) { temp->log(); } std::cout << "||"; } void DoubleLinkedList::deletStart() { if(this->first==NULL) { std::cout << "\n\nCan't deleteStart, list is empty" << std::endl; return; } struct Node * temp = this->first; this->first = this->first->next; //if we're on the last item, let's assign both properties to NULL if(this->first==NULL) this->last = NULL; temp->previous = NULL; temp->next = NULL; temp->locationName = NULL; delete temp; } void DoubleLinkedList::deleteLast() { if(this->last==NULL) { std::cout << "\n\nCan't deleteStart, list is empty" << std::endl; return; } struct Node * temp = this->last; this->last = this->last->previous; if(this->last==NULL) { this->first = NULL; } else { this->last->next = NULL; //repoint the last one } //delete the station temp->previous = NULL; temp->next = NULL; temp->locationName = NULL; delete temp; } |