Last active
January 30, 2018 23:44
-
-
Save Icaro-Lima/2ca525b9d3d547b9c309aadaba83ed6b to your computer and use it in GitHub Desktop.
A simple C / C++ Queue implementation (in my case for CUDA).
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
#include "Node.h" | |
Node::Node(int v) { | |
value = v; | |
next = nullptr; | |
} |
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
#pragma once | |
class Node | |
{ | |
public: | |
Node(int v); | |
int value; | |
Node *next; | |
}; |
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
#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; | |
} |
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
#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