#include #include #include "gqueue.h" typedef struct cell *cell; struct cell { void *element; cell prev; cell next; }; struct gqueue { cell sentinel; cell cursor; }; void gq_go_beginning(gqueue q) { q -> cursor = q -> sentinel; } void gq_go_end(gqueue q) { q -> cursor = q -> sentinel -> prev; } int gq_at_beginning(gqueue q) { return q -> cursor == q -> sentinel; } int gq_at_end(gqueue q) { return q -> cursor == q -> sentinel -> prev; } int gq_empty(gqueue q) { return q -> sentinel -> next == q -> sentinel; } void gq_go_forward(gqueue q) { assert(!gq_at_end(q)); q -> cursor = q -> cursor -> next; } void gq_go_backward(gqueue q) { assert(!gq_at_beginning(q)); q -> cursor = q -> cursor -> prev; } void * gq_get(gqueue q) { assert(!gq_at_end(q)); return q -> cursor -> next -> element; } void gq_insert(gqueue q, void *element) { cell temp = malloc(sizeof(struct cell)); cell current = q -> cursor; cell next = current -> next; temp -> element = element; temp -> next = next; temp -> prev = current; next -> prev = current -> next = temp; q -> cursor = temp; } void gq_delete(gqueue q) { cell temp; assert(!gq_at_end(q)); temp = q -> cursor -> next; temp -> next -> prev = temp -> prev; temp -> prev -> next = temp -> next; free(temp); } gqueue gq_create(void) { gqueue temp = malloc(sizeof(struct gqueue)); cell sentinel = malloc(sizeof(struct cell)); sentinel -> next = sentinel -> prev = sentinel; temp -> sentinel = temp -> cursor = sentinel; return temp; } void gq_destroy(gqueue q) { assert(gq_empty(q)); free(q -> sentinel); free(q); }