can I improve/fix my implemention? I only implemented - Creatre/IsEmpty/Enqueue/Dequeue. Code: #include <stdlib.h> /*a link in the queue, holds the info and point to the next Node*/ typedef struct Node_t{ int count; Node_t *next; } Node; /*the HEAD of the Queue, hold the amount of node's that are in the queue*/ typedef struct Queue{ Node *head; Node *tail; unsigned int size; } Queue; void Create(){ Queue *queue = (Queue*)malloc(sizeof(Queue)); if (queue == NULL) return; queue->size = 0; queue->head = NULL; queue->tail = NULL; } bool Enqueue(Queue *pQueue, Node *item) { /*bad param*/ if ((pQueue == NULL) ||(item == NULL)) return false; /*the queue is empty*/ if (pQueue->size == 0) { pQueue->head = item; } /*adding item to the end of the queue*/ pQueue->tail->next = item; pQueue->size++; return true; } bool freeNode(Queue *pQueue) { Node *tmp = (pQueue->head)->next; free(pQueue->head); pQueue->head = tmp; return true; } bool Dequeue(Queue *pQueue, Node **item) { /*the queue is empty or bad param*/ if ((pQueue->size == 0) || (pQueue == NULL) ||(*item == NULL)) return false; *item = pQueue->head; if (freeNode(pQueue) == true) { pQueue->size--; return true; } else return false; } bool isEmpty(Queue* pQueue) { if (pQueue == NULL) return false; if (pQueue->size == 0) return true; else return false; }
i have a problem with the function "Dequeue". I CAN'T seem to extract the information from the node I remove from the queue. I added a simple example - function main() - to help you understand what I mean. Code: #include <stdlib.h> #include <stdio.h> /*a link in the queue, holds the info and point to the next Node*/ typedef struct Node_t{ int info; Node_t *next; } Node; /*the HEAD of the Queue, hold the amount of node's that are in the queue*/ typedef struct Queue_t{ Node *head; Node *tail; unsigned int size; } Queue; Queue* Create(){ Queue *queue = (Queue*)malloc(sizeof(Queue)); if (queue == NULL) return NULL; queue->size = 0; queue->head = NULL; queue->tail = NULL; return queue; } bool Enqueue(Queue *pQueue, Node *item) { /*bad param*/ if ((pQueue == NULL) ||(item == NULL)) return false; /*adding item to the end of the queue*/ if (pQueue->size != 0) { (pQueue->tail)->next = item; pQueue->tail = item; } else { /*the queue is empty*/ pQueue->head = pQueue->tail = item; }; pQueue->size++; return true; } bool freeNode(Queue *pQueue) { Node *tmp = (pQueue->head)->next; free(pQueue->head); pQueue->head = tmp; return true; } bool Dequeue(Queue *pQueue, Node **item) { /*the queue is empty or bad param*/ if ((pQueue->size == 0) || (pQueue == NULL) ||(*item == NULL)) return false; *item = pQueue->head; if (freeNode(pQueue)) { pQueue->size--; return true; } else return false; } bool isEmpty(Queue* pQueue) { if (pQueue == NULL) return false;/*bad param*/ if (pQueue->size == 0) return true; else return false; } int main() { bool b; Queue *q; /*creating the QUEUE of the queue*/ q = Create(); /*checking if queue is empty*/ if (isEmpty(q)) {printf ("Queue is Empty\n"); } else {printf ("Queue is not empty\n");} /*creating Node1 and insert it to the queue*/ Node *item1 = (Node*)malloc(sizeof(Node)); item1->info = 1; item1->next = NULL; Enqueue(q, item1); /*creating Node2 and insert it to the queue*/ Node *item2 = (Node*)malloc(sizeof(Node)); item2->info = 2; item2->next = NULL; Enqueue(q, item2); /*removing the 1 node from the queue*/ Node *tmp = (Node*)malloc(sizeof(Node)); Dequeue(q, &tmp); /*not working right*/ /*checking if queue is empty*/ if (isEmpty(q)) {printf ("Queue is Empty\n"); } else {printf ("Queue is not empty\n");} printf("info - %d\n", &tmp->info); system ("pause"); return 0; }