summaryrefslogtreecommitdiff
path: root/algorithms/fifo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/fifo.cpp')
-rw-r--r--algorithms/fifo.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/algorithms/fifo.cpp b/algorithms/fifo.cpp
new file mode 100644
index 0000000..a9d2536
--- /dev/null
+++ b/algorithms/fifo.cpp
@@ -0,0 +1,59 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+
+struct Queue {
+ Queue *next;
+ int data;
+};
+
+Queue*
+queue_new(int data)
+{
+ Queue *q = new Queue;
+
+ q->data = data;
+ q->next = NULL;
+
+ return q;
+}
+
+void
+queue_put(Queue* q, int data)
+{
+ Queue* p = queue_new(data);
+ Queue* cur = q;
+
+ while(cur->next) {
+ cur = cur->next;
+ }
+ cur->next = p;
+}
+
+Queue*
+queue_pop(Queue* q, int& data)
+{
+ data = q->data;
+ return q->next;
+}
+
+int main(void)
+{
+ Queue *q = queue_new(1);
+ queue_put(q, 2);
+ queue_put(q, 3);
+ cout << q->data << " " << q->next->data << " " << q->next->next->data << endl;
+
+ int data;
+ q = queue_pop(q, data);
+ cout << data << endl;
+ q = queue_pop(q, data);
+ cout << data << endl;
+ q = queue_pop(q, data);
+ cout << data << endl;
+
+ return 0;
+}
+