queue.h
Code:
// struct declaration
typedef struct queue {
char ch[MAXCHARBUF]; /* char contain in queue */
int front; /* front index of queue */
int rear; /* rear index of queue */
int count; /* current numbers of element in queue */
int max; /* actually use length */
} Queue;
// function prototypes
int init_queue(Queue *q, int max_chars); make a new empty queue, filling in *q
int enqueue(Queue *q,char ch); enqueue char ch on q, or return -1 if can't
int dequeue(Queue *q); dequeue a char and return it, or return -1 if can't
int queuecount(Queue *q); return # chars currently in queue q
queue.c
Code:
//function definitions
#include "queue.h"
int init_queue(Queue *q, int max_chars)
{
// perform stuff to initialize queue
return 0;
}
int enqueue(Queue *q, char ch)
{
// perform stuff to enqueue
return 0;
}
char dequeue(Queue *q)
{
// perform stuff to dequeue
return 0;
}
int queuecount(Queue *q)
{
// return count in queue
return 0;
}
Note I changed dequeue to return the type in your queue, char. But, in implementing your queue, this note from sgi stl docs are sort of interesting (kinda, you only have chars in your queue)
Originally Posted by
http://www.sgi.com/tech/stl/queue.html
[3] One might wonder why pop() returns void, instead of value_type. That is, why must one use front() and pop() to examine and remove the element at the front of the queue, instead of combining the two in a single member function? In fact, there is a good reason for this design. If pop() returned the front element, it would have to return by value rather than by reference: return by reference would create a dangling pointer. Return by value, however, is inefficient: it involves at least one redundant copy constructor call. Since it is impossible for pop() to return a value in such a way as to be both efficient and correct, it is more sensible for it to return no value at all and to require clients to use front() to inspect the value at the front of the queue.
To use you queue in some other code
somefile.c
Code:
// new functions
#include "queue.h"
#include "somefile.h"
int ttywrite()
{
Queue q;
int num, i;
init_queue(&q);
enqueue(&q, 'a');
enqueue(&q, 'b');
enqueue(&q, 'c');
enqueue(&q, 'd');
num = queuecount(&q);
for(i = 0; i < num; ++i)
{
printf("%c", dequeue(&q));
}
// prints 'abcd'
return 0;
}