Created
January 28, 2015 02:51
-
-
Save MichaelEstes/e9b8d3338a3e732c8741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
const int array_length = 2048; | |
struct Q{ | |
int first; | |
int last; | |
unsigned char data[array_length]; | |
Q() : data{}, first(0), last(0){} | |
}; | |
Q * create_queue(); | |
void destroy_queue(Q * q); | |
void enqueue_byte(Q * q, unsigned char b); | |
unsigned char dequeue_byte(Q * q); | |
void defragment_array(Q * q); | |
void on_illegal_operation(); | |
void on_out_of_memory(); | |
Q * create_queue(){ | |
Q * newQ = new Q; | |
if (!newQ){ | |
on_out_of_memory(); | |
} | |
return newQ; | |
} | |
void destroy_queue(Q * q){ | |
delete[] q; | |
q = 0; | |
} | |
void enqueue_byte(Q * q, unsigned char b){ | |
if (q->last == array_length && q->first == 0){ | |
cout << "Queue is full"; | |
}else if (q->last == array_length && q->first){ | |
defragment_array(q); | |
}else{ | |
q->data[q->last] = b; | |
q->last++; | |
} | |
} | |
unsigned char dequeue_byte(Q * q){ | |
if (q->last){ | |
q->first++; | |
return q->data[q->first - 1]; | |
}else{ | |
on_illegal_operation(); | |
return 0; | |
} | |
} | |
void defragment_array(Q * q){ | |
int front_index = 0; | |
for (int i = q->first; i <= q->last; i++, front_index++){ | |
q->data[front_index] = q->data[i]; | |
} | |
q->last -= q->first; | |
q->first = 0; | |
} | |
void on_illegal_operation(){ | |
cerr << "Illegal Opperation" << endl; | |
} | |
void on_out_of_memory(){ | |
cerr << "Out of memory" << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment