-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Standard and Circular Queue (C++) (#116)
- Loading branch information
1 parent
f4a2c1e
commit ca157d7
Showing
10 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "CircularQueue.hpp" | ||
|
||
/* | ||
* SimpleQueue class constructor | ||
* @params length of the queue | ||
*/ | ||
template<class T> | ||
CircularQueue<T>::CircularQueue(int size){ | ||
this->head_ptr = (T*) calloc(size,sizeof(T)); | ||
this->head_index = -1; | ||
this->tail = -1; | ||
this->elements = 0; | ||
this->size = size; | ||
} | ||
|
||
|
||
/* | ||
* Insert a new element in tail of the queue or throw an exception if queue is full. | ||
* Complexity : O(1) | ||
* @param element | ||
* @throw QueueOverflowException | ||
*/ | ||
template<class T> | ||
void CircularQueue<T>::enqueue(T element){ | ||
if(this->isFull()){ | ||
throw this->overflow; | ||
} | ||
this->elements++; | ||
if(this->head_index == -1 && this->tail == -1){ | ||
this->head_index = (this->head_index + 1 ) % this->size; | ||
} | ||
this->tail = (this->tail + 1 ) % this->size; | ||
this->head_ptr[this->tail] = element; | ||
} | ||
|
||
/* | ||
* Remove head element of the queue or throw an exception if queue is empty | ||
* Complexity : O(1) | ||
* @return element | ||
* @throw QueueUnderflowException | ||
*/ | ||
template<class T> | ||
T* CircularQueue<T>::dequeue(){ | ||
if(this->isEmpty()){ | ||
throw this->underflow; | ||
} | ||
this->elements--; | ||
T* aux = &this->head_ptr[this->head_index]; | ||
this->head_index = (this->head_index + 1) % size; | ||
return aux; | ||
} | ||
|
||
|
||
/* | ||
* Return element in the head of the queue without remove, or NULL if queue is empty. | ||
* Complexity: O(1) | ||
* @return element | ||
*/ | ||
template<class T> | ||
T* CircularQueue<T>::head(){ | ||
if(this->isEmpty()){ | ||
return NULL; | ||
}else{ | ||
return &this->head_ptr[this->head_index]; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Return true if queue is empty or false,otherwise. | ||
* Complexity : O(1) | ||
* @return bool | ||
*/ | ||
template<class T> | ||
bool CircularQueue<T>::isEmpty(){ | ||
return this->elements == 0; | ||
} | ||
|
||
|
||
/* | ||
* Return true if queue is full or false,otherwise. | ||
* Complexity : O(1) | ||
* @return bool | ||
*/ | ||
template<class T> | ||
bool CircularQueue<T>::isFull(){ | ||
return this->elements == this->size; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef _CIRCULARQUEUE_H_ | ||
#define _CIRCULARQUEUE_H_ | ||
/* | ||
*@author Ionésio Junior | ||
*/ | ||
|
||
#include "resources/Queue.hpp" | ||
|
||
/* | ||
* Queue in circular implementation | ||
*/ | ||
template<class T> | ||
class CircularQueue : public Queue<T>{ | ||
private: | ||
T *head_ptr; | ||
int tail; | ||
int head_index; | ||
int size; | ||
int elements; | ||
public: | ||
CircularQueue(int size); | ||
bool isEmpty() override; | ||
bool isFull() override; | ||
void enqueue(T element) override; | ||
T* dequeue() override; | ||
T* head() override; | ||
}; | ||
|
||
#endif |
3 changes: 3 additions & 0 deletions
3
Data Structures/Queue/C++/CircularQueue/CircularQueueIntImpl.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "CircularQueue.cpp" | ||
|
||
template class CircularQueue<int>; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "SimpleQueue.hpp" | ||
|
||
/* | ||
* SimpleQueue class constructor | ||
* @params length of the queue | ||
*/ | ||
template<typename T> | ||
SimpleQueue<T>::SimpleQueue(int size){ | ||
this->head_ptr = (T*) calloc(size,sizeof(T)); | ||
this->tail = this->head_ptr - 1; | ||
this->size = size; | ||
} | ||
|
||
|
||
/* | ||
* Return true if queue is empty or false,otherwise. | ||
* Complexity : O(1) | ||
* @return bool | ||
*/ | ||
template<typename T> | ||
bool SimpleQueue<T>::isEmpty(){ | ||
return (this->tail < this->head_ptr); | ||
} | ||
|
||
|
||
/* | ||
* Return true if queue is full or false,otherwise. | ||
* Complexity : O(1) | ||
* @return bool | ||
*/ | ||
template <typename T> | ||
bool SimpleQueue<T>::isFull(){ | ||
return (this->tail == (this->head_ptr + this->size - 1)); | ||
} | ||
|
||
|
||
/* | ||
* Insert a new element in tail of the queue or throw an exception if queue is full. | ||
* Complexity : O(1) | ||
* @param element | ||
* @throw QueueOverflowException | ||
*/ | ||
template <typename T> | ||
void SimpleQueue<T>::enqueue(T element){ | ||
if(!this->isFull()){ | ||
this->tail++; | ||
*this->tail = element; | ||
}else{ | ||
throw this->overflow; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Remove head element of the queue or throw an exception if queue is empty | ||
* Complexity : O(n) | ||
* @return element | ||
* @throw QueueUnderflowException | ||
*/ | ||
template <typename T> | ||
T* SimpleQueue<T>::dequeue(){ | ||
if(!this->isEmpty()){ | ||
T removedElement = *(this->head_ptr); | ||
this->shiftLeft(); | ||
this->tail--; | ||
this->tail[1] = removedElement; | ||
return &this->tail[1]; | ||
}else{ | ||
throw this->underflow; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* When an element is removed, this method move each element to the previous position. | ||
*/ | ||
template<typename T> | ||
void SimpleQueue<T>::shiftLeft(){ | ||
for(int *i = this->head_ptr; i < this->tail;i++){ | ||
*i = *(i + 1); | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Return element in the head of the queue without remove, or NULL if queue is empty. | ||
* Complexity : O(1) | ||
* @return element | ||
*/ | ||
template<typename T> | ||
T* SimpleQueue<T>::head(){ | ||
if(!this->isEmpty()){ | ||
return this->head_ptr; | ||
}else{ | ||
return NULL; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _SIMPLEQUEUE_H_ | ||
#define _SIMPLEQUEUE_H_ | ||
|
||
/* | ||
*@author Ionésio Junior | ||
*/ | ||
#include "resources/Queue.hpp" | ||
|
||
//Queue.hpp | ||
/* | ||
* Simple Queue Implementation | ||
*/ | ||
|
||
template<typename T> | ||
class SimpleQueue : public Queue<T>{ | ||
private: | ||
T *head_ptr; | ||
T *tail; | ||
int size; | ||
void shiftLeft(); | ||
public: | ||
SimpleQueue(int size); | ||
bool isEmpty() override; | ||
bool isFull() override; | ||
void enqueue(T element) override; | ||
T* dequeue() override; | ||
T* head() override; | ||
}; | ||
|
||
#endif |
3 changes: 3 additions & 0 deletions
3
Data Structures/Queue/C++/StandardQueue/SimpleQueueIntImpl.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "SimpleQueue.cpp" | ||
|
||
template class SimpleQueue<int>; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
LIB = -L/usr/lib/x86_64-linux-gnu/ | ||
LIBS = -lboost_unit_test_framework | ||
INCLUDE = -I../. | ||
|
||
all: Test | ||
|
||
../StandardQueue/SimpleQueue.o: ../StandardQueue/SimpleQueueIntImpl.cpp ../StandardQueue/SimpleQueue.hpp ../StandardQueue/SimpleQueue.cpp | ||
g++ -c $< -o $@ $(INCLUDE) | ||
|
||
../CircularQueue/CircularQueue.o: ../CircularQueue/CircularQueueIntImpl.cpp ../CircularQueue/CircularQueue.cpp ../CircularQueue/CircularQueue.hpp | ||
g++ -c $< -o $@ $(INCLUDE) | ||
|
||
Test: ../CircularQueue/CircularQueue.o ../StandardQueue/SimpleQueue.o | ||
g++ $^ test.cpp -o test $(LIB) $(LIBS) $(INCLUDE) -std=c++14 | ||
|
||
clean: | ||
rm ../StandardQueue/SimpleQueue.o | ||
rm ../CircularQueue/CircularQueue.o | ||
rm test |
Oops, something went wrong.