Skip to content

Instantly share code, notes, and snippets.

@Icaro-Lima
Last active January 30, 2018 23:44
Show Gist options
  • Save Icaro-Lima/2ca525b9d3d547b9c309aadaba83ed6b to your computer and use it in GitHub Desktop.
Save Icaro-Lima/2ca525b9d3d547b9c309aadaba83ed6b to your computer and use it in GitHub Desktop.
A simple C / C++ Queue implementation (in my case for CUDA).
#include "Node.h"
Node::Node(int v) {
value = v;
next = nullptr;
}
#pragma once
class Node
{
public:
Node(int v);
int value;
Node *next;
};
#include "Queue.h"
#include "Node.h"
Queue::Queue() {
head = tail = nullptr;
}
bool Queue::empty() {
return head == nullptr;
}
void Queue::enqueue(int v) {
Node *node = new Node(v);
if (empty()) {
head = tail = node;
} else if (head == tail) {
head->next = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
int Queue::dequeue() {
int v = head->value;
if (head == tail) {
head = tail = nullptr;
} else {
head = head->next;
}
return v;
}
#pragma once
#include "Node.h"
class Queue
{
private:
Node *head;
Node *tail;
public:
Queue();
bool empty();
void enqueue(int v);
int Queue::dequeue();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment