From 6a5407b96d09b506d4f0bfb5dec0d435408c017d Mon Sep 17 00:00:00 2001
From: Patrick Simianer
Date: Mon, 16 Jun 2014 19:06:53 +0200
Subject: fifo
---
algorithms/fifo.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+)
create mode 100644 algorithms/fifo.cpp
(limited to 'algorithms')
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
+
+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;
+}
+
--
cgit v1.2.3