1/*--------------------------------------------------------------------------
2Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6    * Redistributions of source code must retain the above copyright
7      notice, this list of conditions and the following disclaimer.
8    * Redistributions in binary form must reproduce the above copyright
9      notice, this list of conditions and the following disclaimer in the
10      documentation and/or other materials provided with the distribution.
11    * Neither the name of Code Aurora nor
12      the names of its contributors may be used to endorse or promote
13      products derived from this software without specific prior written
14      permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27--------------------------------------------------------------------------*/
28/*
29	Queue with Linked list
30*/
31
32#include "queue.h"
33#include <stdio.h>
34#include <stdlib.h>
35
36
37typedef struct Node
38{
39  void *element;
40  struct Node *next;
41} Node;
42
43struct Queue
44{
45  Node *head;
46  Node *tail;
47  int  current_size;
48};
49
50Queue *alloc_queue()
51{
52  Queue *q = (Queue *) malloc(sizeof(Queue));
53  if (q)
54  {
55    q->head = q->tail = NULL;
56    q->current_size = 0;
57  }
58  return q;
59}
60
61void free_queue(Queue *q)
62{
63  while (q->current_size)
64  {
65    pop(q);
66  }
67}
68
69void free_queue_and_qelement(Queue *q)
70{
71  while (q->current_size)
72  {
73    void *element = pop(q);
74    if (element)
75      free(element);
76  }
77}
78
79int push(Queue *q, void * element)
80{
81  Node *new_node = (Node *) malloc(sizeof(Node));
82
83  if (new_node == NULL)
84    return -1;
85
86  new_node->element = element;
87  new_node->next = NULL;
88
89  if (q->current_size == 0)
90  {
91    q->head = new_node;
92  }
93  else
94  {
95    q->tail->next = new_node;
96  }
97
98  q->tail = new_node;
99  q->current_size++;
100
101  return 0;
102}
103
104void *pop(Queue *q)
105{
106  Node *temp;
107  void *element;
108
109  if (q->current_size == 0)
110    return NULL;
111
112  temp = q->head;
113  element = temp->element;
114
115  if (q->current_size == 1)
116  {
117    q->head = q->tail = NULL;
118  }
119  else
120  {
121    q->head = q->head->next;
122  }
123
124  free(temp);
125  q->current_size--;
126  return element;
127}
128
129