表示
typedef struct { data_t data[N]; int front; /* front always points to the "cycle-head", * i.e. point to the previous postion of queue front */ int rear; /* rear point to the rear postion element of the queue */ }sequeue_t;
实现
sequeue_t *CreateEmptySequeue(void) { sequeue_t *queue; queue = (sequeue_t *)malloc(sizeof(sequeue_t)); if (NULL != queue) { queue->front = queue->rear = 0; return queue; } else { return NULL; } } int ClearSequeue(sequeue_t *queue) { if (NULL != queue) { queue->front = queue->rear = 0; return 0; } else { return -1; } } int DestroySequeue(sequeue_t *queue) { if (NULL != queue) { free(queue); return 0; } else { return -1; } } int EmptySequeue(sequeue_t *queue) { if (NULL != queue) { return (queue->front == queue->rear ? 1 : 0); } else { return -1; } } int FullSequeue(sequeue_t *queue) { if (NULL != queue) { return ((queue->rear + 1) % N == queue->front ? 1 : 0); } else { return -1; } } int EnQueue(sequeue_t *queue, data_t x) { if (NULL == queue) return -1; if (FullSequeue(queue)) return -1; queue->rear = (queue->rear + 1)%N; queue->data[queue->rear] = x; return 0; } int DeQueue(sequeue_t *queue, data_t *x) { if (NULL == queue) return -1; if (EmptySequeue(queue)) return -1; if (NULL != x) { queue->front = (queue->front + 1) % N; *x = queue->data[queue->front]; } return 0; }
测试代码
void iterate_queue(sequeue_t * queue) { int i; data_t a[N]; if (!queue) return; for (i = 0; i < N; i++) { a[i] = ‘_‘; } i = queue->front; a[i] = ‘x‘; while (i != queue->rear) { i++; i = i % N; a[i]= queue->data[i]; } printf("queue = (f(%d),r(%d)) {", queue->front, queue->rear); for (i = 0; i < N; i++) { printf("%c ", a[i]); } printf("\b}\n"); } int main() { int i; data_t data; sequeue_t *queue; queue = CreateEmptySequeue(); printf("First round!-----------------------------------------------\n"); for (i = 0; i < 20; i++) { if ( !FullSequeue(queue) ) { printf("Enter %c: ", ‘1‘ + i); EnQueue(queue, ‘1‘ + i); iterate_queue(queue); } else { printf("queue is full\n"); break; } } while ( !EmptySequeue(queue) ){ DeQueue(queue, &data); printf("Out %c: ", data); iterate_queue(queue); } printf("Second round!----------------------------------------------\n"); for (i = 0; i < 20; i++) { if ( !FullSequeue(queue) ) { printf("Enter %c: ", ‘1‘ + i); EnQueue(queue, ‘1‘ + i); iterate_queue(queue); } else { printf("queue is full\n"); break; } } while ( !EmptySequeue(queue) ){ DeQueue(queue, &data); printf("Out %c: ", data); iterate_queue(queue); } DestroySequeue(queue); return 0; }
结果
First round!----------------------------------------------- Enter 1: queue = (f(0),r(1)) {x 1 _ _ _ _ _ _ _ _} Enter 2: queue = (f(0),r(2)) {x 1 2 _ _ _ _ _ _ _} Enter 3: queue = (f(0),r(3)) {x 1 2 3 _ _ _ _ _ _} Enter 4: queue = (f(0),r(4)) {x 1 2 3 4 _ _ _ _ _} Enter 5: queue = (f(0),r(5)) {x 1 2 3 4 5 _ _ _ _} Enter 6: queue = (f(0),r(6)) {x 1 2 3 4 5 6 _ _ _} Enter 7: queue = (f(0),r(7)) {x 1 2 3 4 5 6 7 _ _} Enter 8: queue = (f(0),r(8)) {x 1 2 3 4 5 6 7 8 _} Enter 9: queue = (f(0),r(9)) {x 1 2 3 4 5 6 7 8 9} queue is full Out 1: queue = (f(1),r(9)) {_ x 2 3 4 5 6 7 8 9} Out 2: queue = (f(2),r(9)) {_ _ x 3 4 5 6 7 8 9} Out 3: queue = (f(3),r(9)) {_ _ _ x 4 5 6 7 8 9} Out 4: queue = (f(4),r(9)) {_ _ _ _ x 5 6 7 8 9} Out 5: queue = (f(5),r(9)) {_ _ _ _ _ x 6 7 8 9} Out 6: queue = (f(6),r(9)) {_ _ _ _ _ _ x 7 8 9} Out 7: queue = (f(7),r(9)) {_ _ _ _ _ _ _ x 8 9} Out 8: queue = (f(8),r(9)) {_ _ _ _ _ _ _ _ x 9} Out 9: queue = (f(9),r(9)) {_ _ _ _ _ _ _ _ _ x} Second round!---------------------------------------------- Enter 1: queue = (f(9),r(0)) {1 _ _ _ _ _ _ _ _ x} Enter 2: queue = (f(9),r(1)) {1 2 _ _ _ _ _ _ _ x} Enter 3: queue = (f(9),r(2)) {1 2 3 _ _ _ _ _ _ x} Enter 4: queue = (f(9),r(3)) {1 2 3 4 _ _ _ _ _ x} Enter 5: queue = (f(9),r(4)) {1 2 3 4 5 _ _ _ _ x} Enter 6: queue = (f(9),r(5)) {1 2 3 4 5 6 _ _ _ x} Enter 7: queue = (f(9),r(6)) {1 2 3 4 5 6 7 _ _ x} Enter 8: queue = (f(9),r(7)) {1 2 3 4 5 6 7 8 _ x} Enter 9: queue = (f(9),r(8)) {1 2 3 4 5 6 7 8 9 x} queue is full Out 1: queue = (f(0),r(8)) {x 2 3 4 5 6 7 8 9 _} Out 2: queue = (f(1),r(8)) {_ x 3 4 5 6 7 8 9 _} Out 3: queue = (f(2),r(8)) {_ _ x 4 5 6 7 8 9 _} Out 4: queue = (f(3),r(8)) {_ _ _ x 5 6 7 8 9 _} Out 5: queue = (f(4),r(8)) {_ _ _ _ x 6 7 8 9 _} Out 6: queue = (f(5),r(8)) {_ _ _ _ _ x 7 8 9 _} Out 7: queue = (f(6),r(8)) {_ _ _ _ _ _ x 8 9 _} Out 8: queue = (f(7),r(8)) {_ _ _ _ _ _ _ x 9 _} Out 9: queue = (f(8),r(8)) {_ _ _ _ _ _ _ _ x _}
时间: 2024-09-29 22:08:50