Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DO NOT MODIFY THIS STRUCTURE
- struct dequeue {
- int front;
- int back;
- int len;
- int maxlen;
- int *queue;
- };
- struct dequeue *dequeue_create(void) {
- struct dequeue *dq = malloc(sizeof(int));
- dq->front = 0; //indexes
- dq->back = 2;
- dq->len = 1;
- dq->maxlen = 3;
- dq->queue = malloc(sizeof(int));
- }
- bool dequeue_is_empty(const struct dequeue *q) {
- assert(q);
- return (q->len == 0);
- }
- int dequeue_front(const struct dequeue *q) {
- assert(q);
- return q->front;
- }
- int dequeue_back(const struct dequeue *q) {
- assert(q);
- return q->back;
- }
- int dequeue_remove_front(struct dequeue *q) {
- assert(q);
- ++q->front;
- if (
- }
- int dequeue_remove_back(struct dequeue *q) {
- return 0;
- }
- // resize_up(q) consumes a dequeue q and puts the elements in q in the center of the array. This function should only be called after a realloc has been called on the array to modify its size
- // effects: modifies q->queue and q->front
- void resize_up(struct dequeue *q) {
- assert(q);
- int start = (q->maxlen / 3);
- for (int i = 0; i < q->len; ++i) {
- q->queue[start + i] = q->queue[q->front + i];
- }
- q->front = start;
- q->back = start + (len - 1);
- }
- void dequeue_add_front(int item, struct dequeue *q) {
- assert(q);
- --q->front;
- q->queue[q->front] = item;
- ++q->len;
- if (q->front == 0) {
- q->maxlen = (q->len * 3); //triple current length
- q->queue = realloc(q->queue, q->maxlen * sizeof(int));
- resize_up(q);
- }
- }
- void dequeue_add_back(int item, struct dequeue *q){
- ++q->back;
- q->queue[q->back] = item;
- ++q->len;
- if (q->back == (q->maxlen - 1)) {
- q->maxlen = (q->len * 3); //triple current length
- q->queue = realloc(q->queue, q->maxlen * sizeof(int));
- resize_up(q);
- }
- }
- void dequeue_destroy(struct dequeue *q) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement