I use my school's SSH gcc compiler. I edit the code on JGRASP then compile on the SSH server. (Reason: That is how professor grades it)
Header:
Code:
#ifndef DEQUE_H
#define DEQUE_H
//Used as a true or false statement...
typedef int bool;
typedef struct deque* Deque; //creating a handle for struct queue
// Creates a new array that has a max defined size based on input of client
Deque createDeque(int max_size);
//Inserts a value in the front of the queue
int insertLeft(Deque q, char input);
//Inserts a value to the rear of the queue
int insertRight(Deque q, char input);
//Removes a value from the head of the queue/ the front of it
char removeLeft(Deque q, char input);
//Removes a value from the tail of the queue/ the rear of the queue
char removeRight(Deque q, char input);
/* Returns whether a queue is empty
* @param q the queue to check
* @return whether the queue is empty
*/
bool isEmpty(Deque q);
/* Returns whether a queue is full
* * @param q the queue to check
* * @return whether the queue is full
* */
bool isFull(Deque q);
void freeDeque(Deque q);
#endif
Implementation:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "deque.h"
struct deque {
int head;
int tail;
int size;
char *items; //Purpose as an array
};
Deque createDeque(int max_size)
{//Will take in a maximum size and create a new deque. Again, you will want to create your struct and your array using dynamic allocation.
Deque q = malloc(sizeof *q);
q->items = malloc(sizeof(char) * max_size);
q->head = 0;// Both should start at zero
q->tail = 0;
q->size = max_size;
return q;
}
int insertLeft(Deque q, char input)
{
//Will accept your deque and insert the character on the left side of the deque.
if(isFull(q)) // check to see if it is full
{
printf("Reached the max size of queue.");
exit(1);
}
else
{
q->items[q->head++%q->size] = input;
}
return input;
}
int insertRight(Deque q, char input)
{
// Will accept your deque and insert the character on the right side of the deque.
if(isFull(q)) // check to see if it is full
{
printf("Reached the max size of queue.");
exit(1);
}
else
{
q->items[q->tail++%q->size] = input;
}
return input;
}
char removeLeft(Deque q, char input)
{
// Will accept your deque and remove the character on the left side of the deque.
if(isEmpty(q))
{
printf("Queue is empty.");
exit(1);
}
else
{
q->items[q->head+1%q->size] = input;
}
return input;
}
char removeRight(Deque q, char input)
{
//Will accept your deque and remove the character on the right side of the deque.
if(isEmpty(q))// We need to check if it is empty rather than if it is full...
{
printf("Queue is empty.");
exit(1);
}
else
{
q->items[q->tail+q->size-1%q->size] = input;
}
return input;
}
bool isEmpty(Deque q)
{
return q->tail == q->head;
}
bool isFull(Deque q)
{
return q->tail - q->head == q->size; // Easiest way to check if full
}
void freeDeque(Deque q)
{//Will free the memory used by your deque.
free(q->items);
free(q);
}
Client:
Code:
/**
* Client of Deque.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "deque.h"
int main()
{
char c,d;
int choice = 0;
int *xi;
int x; // Choice is what you choose for switch statement and x is the number to be set as max of the queue
printf("Please enter the number to be set as max of the queue");
scanf("%d", &x);
Deque q = newDeque(x);
do {
printf("Please select one of the options \n");
printf("1: Enter a character to the front\n");
printf("2: Enter a character to the head\n");
printf("3: Remove a character from the left\n");
printf("4: Remove a character from the right\n");
printf("5: Exit");
switch (choice)
{
case '1':
printf("Please enter the character you want to add to the front");
scanf(" %c", &c);
insertLeft(q, c);
break;
case '2':
printf("Please enter the character you want to add to the rear");
scanf(" %c", &c);
insertRight(q, c);
break;
case '3':
printf("Removing character from the left");
scanf(" %c", &d);
removeLeft(q, d);
printf("Character removed is: %c\n", d);
printf("Answer so far: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
break;
case '4':
printf("Removing character from the right");
scanf(" %c", &d);
removeRight(q, d);
printf("Character removed is %c\n", d);
printf("Answer so far is: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
break;
case '5':
return 0;
break;
}
}
while ( choice != 5 );
return 0;
}