root/src/queue.cc

Revision 5, 0.7 KB (checked in by nick, 3 years ago)
Line 
1#include "apachetop.h"
2
3Queue::Queue(void)
4{
5        size = 0;
6        head = NULL;
7        tail = NULL;
8}
9Queue::~Queue(void)
10{
11        delete head;
12}
13
14void Queue::push(void *q)
15{
16        Node *temp;
17
18        temp = new Node;
19
20        temp->x = q;
21        temp->next = NULL;
22
23        if (size == 0)
24        {
25                head = temp;
26                tail = temp;
27        }
28        else
29        {
30                tail->next = temp;
31                tail = temp;
32        }
33        size++;
34}
35
36// remove and return first entry
37int Queue::pop(void **q)
38{
39        Node *temp;
40
41        if (!head)
42        {
43                *q = NULL;
44                return -1;
45        }
46
47        // return the first entry
48        *q = head->x;
49
50        if (head->next)
51                // we have another node to point at
52                temp = head->next;
53        else
54                // no more nodes
55                temp = NULL;
56
57        delete head;
58        head = temp;
59
60        size--;
61
62        return 0; // success
63}
64
65int Queue::entries(void)
66{
67        return size;
68}
Note: See TracBrowser for help on using the browser.