Skip to content

Commit

Permalink
Implement Standard and Circular Queue (C++) (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
IonesioJunior authored and tstreamDOTh committed Oct 5, 2018
1 parent f4a2c1e commit ca157d7
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 0 deletions.
89 changes: 89 additions & 0 deletions Data Structures/Queue/C++/CircularQueue/CircularQueue.cpp
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;
}
29 changes: 29 additions & 0 deletions Data Structures/Queue/C++/CircularQueue/CircularQueue.hpp
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "CircularQueue.cpp"

template class CircularQueue<int>;
97 changes: 97 additions & 0 deletions Data Structures/Queue/C++/StandardQueue/SimpleQueue.cpp
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;
}
}
30 changes: 30 additions & 0 deletions Data Structures/Queue/C++/StandardQueue/SimpleQueue.hpp
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "SimpleQueue.cpp"

template class SimpleQueue<int>;
19 changes: 19 additions & 0 deletions Data Structures/Queue/C++/Test/Makefile
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
Loading

0 comments on commit ca157d7

Please sign in to comment.