Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Add yourself in this file if you are a contributor for Hacktoberfest 2018

- Radu Stochitoiu
- Antonio Macovei
51 changes: 51 additions & 0 deletions CPP/data-structures/Deque.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Deque.h
Copyright Antonio Macovei
*/

#ifndef DEQUE_H_
#define DEQUE_H_

#include "DoublyLinkedList.h"

template<typename T>
class Deque {
private:
DoublyLinkedList<T> deque;

public:
void push_front(T data) {
deque.add_first(data);
}

void push_back(T data) {
deque.add_last(data);
}

void pop_front() {
deque.remove_first();
}

void pop_back() {
deque.remove_last();
}

T front() {
if (!deque.is_empty())
return deque.get_head()->data;
}

T back() {
if (!deque.is_empty())
return deque.get_tail()->data;
}

int size() {
return deque.size();
}

bool empty() {
return deque.is_empty();
}
};
#endif // DEQUE_H_
169 changes: 169 additions & 0 deletions CPP/data-structures/DoublyLinkedList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
DoublyLinkedList.h
Copyright Antonio Macovei
*/

#ifndef DOUBLYLINKEDLIST_H_
#define DOUBLYLINKEDLIST_H_

template <typename T>
struct Node {
T data;
Node<T> *next;
Node<T> *prev;

explicit Node(T data) {
this->data = data;
this->next = nullptr;
this->prev = nullptr;
}
};

template <typename T>
class DoublyLinkedList {
private:
Node<T> *head;
Node<T> *tail;
int numElements;

// Returns the pos'th node in the list.
Node<T> *goToPos(int pos) {
int count = 0;
Node<T> *it = head;

while (count != pos) {
it = it->next;
count++;
}
return it;
}

public:
// Constructor
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
numElements = 0;
}

// Destructor
~DoublyLinkedList() {
while (head != nullptr) {
Node<T> *temp = head;
head = head->next;
delete temp;
}
}

/**
* Adds a new node at the beginning of the list.
*
* @param data Data to be added at the end of the list.
*/
void add_first(T data) {
if (head == nullptr) {
head = new Node<T>(data);
head->next = nullptr;
head->prev = nullptr;
tail = head;
} else {
Node<T> *new_node;
new_node = new Node<T>(data);
new_node->next = head;
new_node->prev = nullptr;
head->prev = new_node;
head = new_node;
}
numElements++;
}

/**
* Adds a new node at the end of the list.
*
* @param data Data to be added at the end of the list.
*/
void add_last(T data) {
if (head == nullptr) {
head = new Node<T>(data);
head->next = nullptr;
head->prev = nullptr;
tail = head;
} else {
Node<T> *new_node;
new_node = new Node<T>(data);
new_node->next = nullptr;
new_node->prev = tail;
tail->next = new_node;
tail = new_node;
}
numElements++;
}

/**
* Removes the last node of the list.
*/
void remove_last() {
if (numElements == 0) return;
if (numElements == 1) {
delete tail;
tail = nullptr;
head = nullptr;
numElements = 0;
} else {
Node<T> *new_tail = tail->prev;
tail->prev->next = nullptr;
delete tail;
tail = new_tail;
numElements--;
}
}

/**
* Removes the first node of the list.
*/
void remove_first() {
if (numElements == 0) return;
if (numElements == 1) {
delete head;
tail = nullptr;
head = nullptr;
numElements = 0;
} else {
Node<T> *new_head = head->next;
head->next->prev = nullptr;
delete head;
head = new_head;
numElements--;
}
}

/**
* Check if the list contains any elements.
*
* @return True if the list contains no elements, False otherwise.
*/
bool is_empty() {
if (numElements == 0)
return true;
return false;
}

/**
* Get the number of nodes in the list.
*
* @return The number of nodes stored in the list.
*/
int size() {
return numElements;
}

// Getters & Setters
Node<T> *get_head() {
return head;
}

Node<T> *get_tail() {
return tail;
}
};
#endif // DOUBLYLINKEDLIST_H_
70 changes: 70 additions & 0 deletions CPP/data-structures/Hashtable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef HASHTABLE_H_
#define HASHTABLE_H_
#include <list>

#define TRESHOLD 0.75

template <typename Tkey, typename Tvalue> struct elem_info {
Tkey key;
Tvalue value;
};

template <typename Tkey, typename Tvalue> class Hashtable {
private:
std::list<struct elem_info<Tkey, Tvalue> > * H; // pointer to buckets
int HMAX; // number of buckets
unsigned int (*hash)(Tkey); // pointer to hash function
unsigned int size; // number of elements in hashtable

public:
Hashtable(int hmax, unsigned int (*h)(Tkey)) {
HMAX = hmax;
H = new std::list<struct elem_info<Tkey, Tvalue> >[hmax];
hash = h;
}

~Hashtable() {
delete[] H;
}

// Adds an element to the hashtable
void put(Tkey key, Tvalue value) {
unsigned int index = hash(key) % HMAX;
bool found = false;
for(auto it : H[index]) {
if(it.key == key) {
it.value = value;
found = true;
}
}
if(!found) {
elem_info<Tkey, Tvalue> new_elem;
new_elem.key = key;
new_elem.value = value;
H[index].push_back(new_elem);
}
}

// Gets an element from the hashtable
Tvalue get(Tkey key) {
unsigned int index = hash(key) % HMAX;
for(auto it : H[index]) {
if(it.key == key) {
return it.value;
}
}
return Tvalue();
}

// Gets the hashtable size.
int get_size() {
return size;
}

// Gets the hastable capacity.
int get_capacity() {
return HMAX;
}
};

#endif // HASHTABLE_H__
Loading