#include <stdio.h>
typedef enum {FALSE, TRUE} bool;
/* DECLARATION FOR STACK */
#define STACK_CAPACITY 24
typedef char stackElt;
typedef struct stack {
stackElt* elts;
int size;
} stack;
/* DECLARATION FOR QUEUE */
typedef stackElt queueElt;
typedef struct queue {
stack in, // stack to enqueue
out; // stack to dequeue
} queue;
/* FUNCTION PROTOTYPES AND DOCUMENTATION FOR STACK */
stack createStack(); // create a new empty stack
bool isEmptyStack(stack s); // check whether stack s is empty or not
bool isFullStack (stack s); // check whether stack s is full or not
int sizeOfStack (stack s);
stack push(stackElt e, stack s); // add element e on top of non-full stack
stack pop(stack s); // remove top element from non-empty stack
stackElt top(stack s); // get top element of non-empty stack
stackElt bottom(stack s); // get bottom element of non-empty stack
void showStackTopToBottom(stack s);
void showStackBottomToTop(stack s);
/* FUNCTION PROTOTYPES AND DOCUMENTATION FOR QUEUE */
queue createQueue(); // create a new empty queue
bool isEmptyQueue(queue q); // check whether queue q is empty or not
bool isFullQueue (queue q); // check whether queue q is full or not
int sizeOfQueue (queue q);
queue insert(queueElt e, queue q); // add element e to non-full queue q
queue serve(queue q); // remove front element from non-empty queue
queueElt front(queue q); // get front element of non-empty queue
queueElt rear(queue q); // get rear element of non-empty queue
void showQueueFrontToRear(queue q);
/* DEFINITION OF THE STACK FUNCTIONS */
Back to PR21
/* DEFINITION OF THE QUEUE FUNCTIONS */
Back to PR21
/* TEST PROGRAMMA */
int main(void) {
int i;
queue q = createQueue();
printf("An empty queue has been created.\n");
printf("The size of the queue: %d.\n\n", sizeOfQueue(q));
printf("Add characters \'a\', \'b\', ... to the queue.\n");
for (i = 0; i < 10; i++)
if (!isFullQueue(q))
q = insert('a' + i, q);
printf("The size of the queue: %d.\n", sizeOfQueue(q));
printf("The queue contains: "); showQueueFrontToRear(q);
printf("\nCharacters are removed from the queue.\n");
for (i = 0; i < 6; i++)
if (!isEmptyQueue(q))
q = serve(q);
printf("The size of the queue: %d.\n", sizeOfQueue(q));
printf("The queue contains: "); showQueueFrontToRear(q);
printf("\nAdd characters \'A\' ... to the queue.\n");
for (i = 0; i < 5; i++)
if (!isFullQueue(q))
q = insert('A' + i, q);
printf("The size of the queue: %d.\n", sizeOfQueue(q));
printf("The queue contains: "); showQueueFrontToRear(q);
printf("\nCharacters are removed until queue is empty.\n");
while (!isEmptyQueue(q))
q = serve(q);
printf("The size of the queue: %d.\n", sizeOfQueue(q));
if (isEmptyQueue(q))
printf("The queue is empty.\n");
else
printf("SHIT: the queue should be empty now.\n");
return(0);
}
/*
OUTPUT:
An empty queue has been created.
The size of the queue: 0.
Add characters 'a', 'b', ... to the queue.
The size of the queue: 10.
The queue contains: [[][ a b c d e f g h i j]]
Characters are removed from the queue.
The size of the queue: 4.
The queue contains: [[ g h i j][]]
Add characters 'A' ... to the queue.
The size of the queue: 9.
The queue contains: [[ g h i j][ A B C D E]]
Characters are removed until queue is empty.
The size of the queue: 0.
The queue is empty.
*/