Last active
December 2, 2021 00:02
-
-
Save ArnonEilat/4471278 to your computer and use it in GitHub Desktop.
Simple C implementation of queue.
Usage example included.
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 <stdio.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
/* a link in the queue, holds the info and point to the next Node*/ | |
typedef struct { | |
int info; | |
} DATA; | |
typedef struct Node_t { | |
DATA data; | |
struct Node_t *prev; | |
} NODE; | |
/* the HEAD of the Queue, hold the amount of node's that are in the queue*/ | |
typedef struct Queue { | |
NODE *head; | |
NODE *tail; | |
int size; | |
int limit; | |
} Queue; | |
Queue *ConstructQueue(int limit); | |
void DestructQueue(Queue *queue); | |
int Enqueue(Queue *pQueue, NODE *item); | |
NODE *Dequeue(Queue *pQueue); | |
int isEmpty(Queue* pQueue); | |
Queue *ConstructQueue(int limit) { | |
Queue *queue = (Queue*) malloc(sizeof (Queue)); | |
if (queue == NULL) { | |
return NULL; | |
} | |
if (limit <= 0) { | |
limit = 65535; | |
} | |
queue->limit = limit; | |
queue->size = 0; | |
queue->head = NULL; | |
queue->tail = NULL; | |
return queue; | |
} | |
void DestructQueue(Queue *queue) { | |
NODE *pN; | |
while (!isEmpty(queue)) { | |
pN = Dequeue(queue); | |
free(pN); | |
} | |
free(queue); | |
} | |
int Enqueue(Queue *pQueue, NODE *item) { | |
/* Bad parameter */ | |
if ((pQueue == NULL) || (item == NULL)) { | |
return FALSE; | |
} | |
// if(pQueue->limit != 0) | |
if (pQueue->size >= pQueue->limit) { | |
return FALSE; | |
} | |
/*the queue is empty*/ | |
item->prev = NULL; | |
if (pQueue->size == 0) { | |
pQueue->head = item; | |
pQueue->tail = item; | |
} else { | |
/*adding item to the end of the queue*/ | |
pQueue->tail->prev = item; | |
pQueue->tail = item; | |
} | |
pQueue->size++; | |
return TRUE; | |
} | |
NODE * Dequeue(Queue *pQueue) { | |
/*the queue is empty or bad param*/ | |
NODE *item; | |
if (isEmpty(pQueue)) | |
return NULL; | |
item = pQueue->head; | |
pQueue->head = (pQueue->head)->prev; | |
pQueue->size--; | |
return item; | |
} | |
int isEmpty(Queue* pQueue) { | |
if (pQueue == NULL) { | |
return FALSE; | |
} | |
if (pQueue->size == 0) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
int main() { | |
int i; | |
Queue *pQ = ConstructQueue(7); | |
NODE *pN; | |
for (i = 0; i < 9; i++) { | |
pN = (NODE*) malloc(sizeof (NODE)); | |
pN->data.info = 100 + i; | |
Enqueue(pQ, pN); | |
} | |
while (!isEmpty(pQ)) { | |
pN = Dequeue(pQ); | |
printf("\nDequeued: %d", pN->data); | |
free(pN); | |
} | |
DestructQueue(pQ); | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inside the main function, the queue limit is set to 7, but you are attempting to add 9 nodes into it.
The exceeded nodes are not freed, hence results in the memory leak as @Martinfx mentioned.