Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "queueHeader.h"
- int main() {
- QUEUE *pointerToQ;
- queueManage(&pointerToQ, 1, 10);
- int i = 0;
- for(i = 0; i < 10; i++){
- addToQueue(pointerToQ, i);
- }
- printf("Pointer is pointing to %d", pointerToQ -> count);
- int *val = 0;
- removeFromQueue(pointerToQ, val);
- printf("The value of val is %d", val);
- }
- /*
- * line Manager
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "queueHeader.h"
- /*
- * create or delete a queue
- *
- * PARAMETERS: QUEUE **qptr space for, or pointer to, queue
- * int flag 1 for create, 0 for delete
- * int size max elements in queue
- */
- void queueManage(QUEUE **qptr, int flag, int size)
- {
- if (flag){
- /* allocate a new queue */
- *qptr = malloc(sizeof(QUEUE));
- (*qptr)->head = (*qptr)->count = 0;
- (*qptr)->que = malloc(size * sizeof(int));
- (*qptr)->size = size;
- }
- else{
- /* delete the current queue */
- (void) free((*qptr)->que);
- (void) free(*qptr);
- }
- }
- /*
- * add an element to an existing queue
- *
- * PARAMETERS: QUEUE *qptr pointer for queue involved
- * int n element to be appended
- */
- void addToQueue(QUEUE *qptr, int n)
- {
- /* add new element to tail of queue */
- qptr->que[(qptr->head + qptr->count) % qptr->size] = n;
- qptr->count++;
- }
- /*
- * take an element off the front of an existing queue
- *
- * PARAMETERS: QUEUE *qptr pointer for queue involved
- * int *n storage for the return element
- */
- void removeFromQueue(QUEUE *qptr, int *n)
- {
- /* return the element at the head of the queue */
- *n = qptr->que[qptr->head++];
- qptr->count--;
- qptr->head %= qptr->size;
- }
- /*
- * line Manager Header file
- */
- /*
- * the queue structure
- */
- typedef struct queue {
- int *que; /* the actual array of queue elements */
- int head; /* head index in que of the queue */
- int count; /* number of elements in queue */
- int size; /* max number of elements in queue */
- } QUEUE;
- /*
- * the library functions
- */
- void queueManage(QUEUE **, int, int); /* create or delete a queue */
- void addToQueue(QUEUE *, int); /* add to queue */
- void removeFromQueue(QUEUE *, int *); /* remove from queue */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement