顺序队列(sequence queue)

/* Author: ACb0y Date: 2010-6-14 Description: sequence queue */ #include <stdio.h> #define MAXSIZE 100 typedef int elemtype; typedef struct { elemtype queue[MAXSIZE]; int front; int rear; }sequeue; /* FUNCTION sequeue_init DESCRIPTION initialize the sequence queue PARAMETERS p_s: the pointer of sequence queue RETURNS void */ void sequeue_init(sequeue * p_s){ p_s->front = 0; p_s->rear = 0; } /* FUNCTION sequeue_push DESCRIPTION push the element into the sequeue PARAMETERS p_s: the pointer of sequeue value: the element prepare to push RETURNS flag: the flag whether push operation successful or not */ int sequeue_push(sequeue * p_s, elemtype value) { int flag; if ((p_s->rear + 1) % MAXSIZE == p_s->front) { flag = 0; } else { p_s->rear = (p_s->rear + 1) % MAXSIZE; p_s->queue[p_s->rear] = value; flag = 1; } return flag; } /* FUNCTION sequeue_pop DESCRIPTION pop the element from the sequence queue's front PARAMETERS p_s: the pointer of sequence queue RETURNS flag: the flag whether pop operation successful or not */ int sequeue_pop(sequeue * p_s) { int flag; if (p_s->rear == p_s->front) { flag = 0; } else { p_s->front = (p_s->front + 1) % MAXSIZE; flag = 1; } return flag; } /* FUNCTION sequeue_top DESCRIPTION get the top element of the sequence queue PARAMETERS p_s: the pointer of sequence queue RETURNS the top element of sequence queue */ elemtype sequeue_top(sequeue * p_s) { return p_s->queue[(p_s->front + 1) % MAXSIZE]; } /* FUNCTION sequeue_empty DESCRIPTION judge the sequence queue if empty PARAMETERS p_s: the pointer of sequence queue RETURNS 1: sequence queue is empty 0: sequence queue isn't empty */ int sequeue_empty(sequeue * p_s) { if (p_s->front == p_s->rear) { return 1; } else { return 0; } } int main() { sequeue s; sequeue_init(&s); int i; for (i = 0; i < 10; i++) { sequeue_push(&s, i + 1); } while (!sequeue_empty(&s)) { printf("%d ", sequeue_top(&s)); sequeue_pop(&s); } printf("/n"); return 0; }

你可能感兴趣的:(顺序队列(sequence queue))