Hi, for an assignment I have to do, we have to made a Stack and Queue, and read data from a file and add to queue and push onto the stack. Well, I made an array implementation of a stack that works fine, but the array implementation of the Queue is really giving me problems. I keep getting, especially surrounding my remove subroutine, a "error: conflicting types for remove" and "previous declaration of 'remove' was here" errors.
Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include "queueADT.h"
#define QUEUE_SIZE 100
struct queue_type {
int contents[QUEUE_SIZE];
int cursor;
int size;
};
static void terminate(char *message)
{
printf("%s\n", message);
exit(EXIT_FAILURE);
}
Queue create()
{
Queue q;
q = malloc(sizeof(struct queue_type));
if(q == NULL)
terminate("Error in create: Queue could not be created.");
q->cursor = 0;
q->size = 0; //Size is zero because no elements have been added yet
return q;
}
void destroy(Queue q)
{
free(q);
}
bool is_empty(Queue q)
{
return q->size == 0;
}
bool is_full(Queue q)
{
return q->size == QUEUE_SIZE;
}
void add(Queue q, int i) //Adds an element to the end of the queue
{
int last = get_last(q); //finds last occupied spot in the queue
while(q->cursor<=QUEUE_SIZE) //Makes sure you dont go out of bounds
q->cursor++;
q->contents[q->cursor]=i;
q->size++;
}
int remove(Queue q) //Removes the first element from the queue
{
get_first(q); //MAKE SURE IT MOVES THE CURSOR!!!!!
int removed = q->contents[0]; //Saves the item to return it
q->cursor++; //Prepares to move the element
while(q->cursor<q->size) //Should shift each element one to the left without
{ //going out of bounds
q->contents[q->cursor] = q->contents[q->cursor-1];
q->cursor++;
}
q->size--;
return removed;
}
void get_first(Queue q) //Moves the cursor to zero since you already know
{ //where the first item is
q->cursor=0;
}
int get_next(Queue q) //returns index of the next item in the queue and moves the cursor there
{
q->cursor++;
return q->cursor;
}
int get_last(Queue q) //finds the last element, returns its index, and moves the cursor there
{
q->cursor = q->size-1;
return q->cursor;
}
int main()
{
Queue q1;
q1 = create();
/* add(q1, 10);
add(q1, 20);
remove(q1); */
return 0;
}
I dont know how to tag my header properly, the code tags arent working... so I guess Ill just paste it in bold letters... sorry
/*queueADT.h*/
#ifndef QUEUEADT_H
#define QUEUEADT_H
#include <stdbool.h>
typedef struct queue_type *Queue;
Queue create();
void destroy(Queue q);
bool is_empty(Queue q);
bool is_full(Queue q);
void add(Queue q, int i);
int remove(Queue q);
void get_first(Queue q);
int get_next(Queue q);
int get_last(Queue q);
#endif