1 /*-------------------------------------------------------------------------- 2 Copyright (c) 2010-2011, 2013, The Linux Foundation. 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 The Linux Foundation 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 void *element; 39 struct Node *next; 40 } Node; 41 42 struct Queue { 43 Node *head; 44 Node *tail; 45 int current_size; 46 }; 47 48 Queue *alloc_queue() 49 { 50 Queue *q = (Queue *) malloc(sizeof(Queue)); 51 52 if (q) { 53 q->head = q->tail = NULL; 54 q->current_size = 0; 55 } 56 57 return q; 58 } 59 60 void free_queue(Queue *q) 61 { 62 while (q->current_size) { 63 pop(q); 64 } 65 } 66 67 void free_queue_and_qelement(Queue *q) 68 { 69 while (q->current_size) { 70 void *element = pop(q); 71 72 if (element) 73 free(element); 74 } 75 } 76 77 int push(Queue *q, void * element) 78 { 79 Node *new_node = (Node *) malloc(sizeof(Node)); 80 81 if (new_node == NULL) 82 return -1; 83 84 new_node->element = element; 85 new_node->next = NULL; 86 87 if (q->current_size == 0) { 88 q->head = new_node; 89 } else { 90 q->tail->next = new_node; 91 } 92 93 q->tail = new_node; 94 q->current_size++; 95 96 return 0; 97 } 98 99 void *pop(Queue *q) 100 { 101 Node *temp; 102 void *element; 103 104 if (q->current_size == 0) 105 return NULL; 106 107 temp = q->head; 108 element = temp->element; 109 110 if (q->current_size == 1) { 111 q->head = q->tail = NULL; 112 } else { 113 q->head = q->head->next; 114 } 115 116 free(temp); 117 q->current_size--; 118 return element; 119 } 120 121