Code: Show/Hide #include "queue.h" int main() { Queue * q = queue_create(8); //queue of length 8 queue_push(q,"test"); queue_push(q,"test2"); queue_push(q,"test3"); queue_push(q,"test4"); queue_push(q,"test5"); queue_push(q,"test6"); queue_push(q,"test7"); queue_push(q,"test8"); queue_push(q,"test9"); queue_push(q,"test10"); char test[200]; queue_pop(q,test); for(int t;t<20;t++){ if(queue_pop(q,test)==0) { printf("popped: %s\n",test); } } queue_free(q); //free memory } |
Code: Show/Hide #ifndef QUEUE_H_INCLUDED #define QUEUE_H_INCLUDED typedef struct Queue Queue; extern Queue * queue_create(int /*queue size*/); extern int queue_push(Queue *, char * /*item to add to queue*/); extern int queue_pop(Queue *, char * /*holds retrieved item*/); extern int queue_size(Queue *); extern int queue_free(Queue *); #endif // QUEUE_H_INCLUDED |
Code: Show/Hide /* * Simple Queue * * Copyright (c) 2008, Doc Flabby * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * NOTES * * NOT THREAD SAFE */ //standard libs #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include "queue.h" struct Queue { char ** Content; unsigned int WriteCount; unsigned int ReadCount; int MaxElements; }; extern Queue * queue_create(int NumberOfElements) { Queue *queue = malloc(sizeof(Queue)); if(queue == NULL) { printf("malloc failed\n"); return NULL; } queue->Content = malloc(NumberOfElements * sizeof(char*)); memset(queue->Content,(int)NULL,NumberOfElements * sizeof(char*)); // zero array if(queue->Content == NULL) { printf("malloc failed\n"); return NULL; } queue->WriteCount = 0; queue->ReadCount = 0; queue->MaxElements = NumberOfElements; return queue; } extern int queue_push(Queue *queue, char * item) { if(queue == NULL) { printf("Invalid Queue Struct\n"); return -1; } // In the second level if(queue_size(queue) >= queue->MaxElements) { printf("Queue is full, discarded new value\n"); return 1; } //theresb space in the queue. int position = queue->WriteCount % queue->MaxElements; printf("Added %s Current Position:%i\r\n",item,position); queue->Content[position] = malloc(strlen(item) + 1); if(queue->Content[position] == NULL) { printf("malloc failed\n"); return -1; } strcpy(queue->Content[position],item); queue->WriteCount++; return 0; } extern int queue_pop(Queue *queue, char * item) { if(queue == NULL) { printf("Invalid Queue Struct"); return -1; } if(queue_size(queue) == 0) { printf("Queue is empty\n"); return 1; } int position = queue->ReadCount % queue->MaxElements; strcpy(item,queue->Content[position]); printf("Removed %s Current Position:%i\r\n",item,position); free(queue->Content[position]); // free memory queue->Content[position] = NULL; // assign null pointer to indicate no entry here queue->ReadCount++; //increment read count return 0; } extern int queue_size(Queue *queue) { if(queue == NULL) { printf("Invalid Queue Struct"); return -1; } //Number of items added to queue-number takne out = number left :> return queue->WriteCount - queue->ReadCount; } extern int queue_free(Queue *queue) { if(queue == NULL) { printf("Invalid Queue Struct"); return -1; } for(int i = 0;i<queue->MaxElements;i++) { if(queue->Content[i]!=NULL) { free(queue->Content[i]); } } free(queue->Content); free(queue); return 0; } |
Samapico wrote: |
Would be nice with a template so it could work with any types |