Created
April 23, 2017 21:50
-
-
Save imdanielsp/1897ceb67f38dba2f3bf0abaaa750837 to your computer and use it in GitHub Desktop.
Implementation of the Queue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "queue.h" | |
#include "../LinkedList/linked_list.h" | |
struct Queue { | |
pLinkedList buffer; | |
}; | |
pQueue makeQueue() { | |
pQueue newQueue = malloc(sizeof(Queue)); | |
newQueue->buffer = makeList(); | |
return newQueue; | |
} | |
size_t push(pQueue queue, T data) { | |
if (queue == NULL) return 0; // FAILED, returns 0. | |
appendToList(queue->buffer, data); | |
return queue->buffer->size; | |
} | |
T* pop(pQueue queue) { | |
if (queue == NULL) return NULL; // No Queue | |
pNode front = removeFront(queue->buffer); | |
if (front == NULL) return NULL; // Nothing in the queue | |
return front->data; | |
} | |
size_t queueSize(pQueue queue) { | |
return queue->buffer->size; | |
} | |
void destroyQueue(pQueue* pPqueue) { | |
if (pPqueue == NULL) return; // Nothing here. | |
pQueue queue = *pPqueue; | |
destroyList(&queue->buffer); | |
queue->buffer = NULL; | |
free(queue); | |
*pPqueue = NULL; | |
pPqueue = NULL; | |
queue = NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment