diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-16 19:06:53 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-16 19:06:53 +0200 |
commit | 6a5407b96d09b506d4f0bfb5dec0d435408c017d (patch) | |
tree | a7315001474bc2075c73cc5150c5a19e48cc4167 /algorithms | |
parent | 5feae204ca981384ea4902ee8d5859f37cbfdca5 (diff) |
fifo
Diffstat (limited to 'algorithms')
-rw-r--r-- | algorithms/fifo.cpp | 59 |
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; +} + |