summaryrefslogtreecommitdiff
path: root/algorithms/fifo.cpp
blob: a9d25367ab9c6c9d01aa48e5db9885c470a8a2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
}