1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, 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 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED 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 
37 typedef struct Node
38 {
39   void *element;
40   struct Node *next;
41 } Node;
42 
43 struct Queue
44 {
45   Node *head;
46   Node *tail;
47   int  current_size;
48 };
49 
alloc_queue()50 Queue *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 
free_queue(Queue * q)61 void free_queue(Queue *q)
62 {
63   while (q->current_size)
64   {
65     pop(q);
66   }
67 }
68 
free_queue_and_qelement(Queue * q)69 void 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 
push(Queue * q,void * element)79 int 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 
pop(Queue * q)104 void *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