Created
April 14, 2018 17:52
-
-
Save bobby5892/d18dd5cf84f2462fc220f93726700f7c to your computer and use it in GitHub Desktop.
wrapping array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Dequeue.h" | |
| #include <string> | |
| #include <sstream> | |
| #include <iostream> | |
| #include <ostream> | |
| Dequeue::Dequeue() | |
| { | |
| // Create initial data | |
| this->init(100); | |
| this->tailIndex = 0; | |
| this->headIndex = -1; | |
| } | |
| Dequeue::Dequeue(int specialSize){ | |
| // Create Initial Data | |
| if(specialSize > 1){ | |
| this->init(specialSize); | |
| } | |
| else{ | |
| this->init(100); | |
| } | |
| this->tailIndex = 0; | |
| this->headIndex =-1; | |
| } | |
| void Dequeue::init(int howBig){ | |
| // Used to build the queue | |
| this->dataSize = howBig; | |
| this->data = new int[this->dataSize]; | |
| } | |
| int Dequeue::Count(){ | |
| return this->tailIndex - this->headIndex; | |
| } | |
| void Dequeue::insertRight(int entry){ | |
| if(this->headIndex == -1){ | |
| // Lets shift it all | |
| for(int i=this->tailIndex% this->dataSize-1;i>this->headIndex % this->dataSize;i--){ | |
| //Start at back and move forward | |
| //std::cout << " Moving data from " << i << " into " << i-1 << std::endl; | |
| this->data[i+1] = this->data[i]; | |
| } | |
| this->data[0] = entry; | |
| this->tailIndex++; | |
| } | |
| else{ | |
| this->data[this->headIndex-- % this->dataSize] = entry; | |
| } | |
| //std::cout << "stuff "; | |
| //this->displayArray(); | |
| } | |
| void Dequeue::insertLeft(int entry){ | |
| // check for room | |
| if(this->tailIndex > this->dataSize ){ | |
| this->resizeArray(this->dataSize*2); | |
| } | |
| // Save data | |
| //this->data[ this->tailIndex++] = entry; | |
| this->data[ this->tailIndex++ % this->dataSize ] = entry; | |
| } | |
| int Dequeue::removeLeft(){ | |
| if(this->isEmpty()){ | |
| throw std::range_error("Out of range"); | |
| } | |
| return this->data[--this->tailIndex % this->dataSize]; | |
| } | |
| int Dequeue::removeRight(){ | |
| if(this->isEmpty()){ | |
| throw std::range_error("Out of range"); | |
| } | |
| return this->data[++this->headIndex % this->dataSize]; | |
| } | |
| void Dequeue::resizeArray(int newSize){ | |
| int* oldData = this->data; | |
| int oldSize = this->dataSize; | |
| // Create new data | |
| this->init(newSize); | |
| this->dataSize = newSize; | |
| // move data over | |
| for(int i=0; i < oldSize;i++ ){ | |
| this->data[i] = oldData[i]; | |
| } | |
| // cleanup | |
| delete [] oldData; | |
| } | |
| void Dequeue::displayArray(){ | |
| std:: cout << " head: " << this->headIndex << std::endl; | |
| for(int i=0; i<this->dataSize;i++){ | |
| std::cout << "value:" << i << ":" << this->data[i] << std::endl; | |
| } | |
| std:: cout << " tail: " << this->tailIndex << std::endl; | |
| // Show the entire array | |
| } | |
| std::string Dequeue::displayQueue(){ | |
| // Show the part that has stuff in it | |
| if( this->isEmpty() == false){ | |
| std::stringstream output; | |
| // Start at the first head index and while the iterator is less than the tail - look at it | |
| for(int i=this->headIndex; i < this->tailIndex ; i++){ | |
| output << this->data[i] << ","; | |
| } | |
| std::string outputStr = output.str(); | |
| //Remove Trailing coma | |
| return outputStr.substr(0, outputStr.length() - 1); | |
| } | |
| return "Queue is empty"; | |
| } | |
| bool Dequeue::isEmpty(){ | |
| if((this->tailIndex-1) == this->headIndex){ | |
| return true; | |
| } | |
| return false; | |
| } | |
| Dequeue::~Dequeue() | |
| { | |
| //dtor | |
| // delete [] this->data; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment