顺序队列的实现
2016-03-11 23:04
661 查看
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define QUEUE_INIT_SIZE 5 typedef int Status; typedef int Elemtype; typedef struct QuqQueue{ Elemtype* base; int front; int rear; }SuqQueue; Status InitQueue(SuqQueue *sq){ sq->base = (Elemtype*)malloc(sizeof(Elemtype) * QUEUE_INIT_SIZE); if( !sq->base ) return ERROR; sq->front = sq->rear = 0; return OK; } Status InsertQueue(SuqQueue *sq,Elemtype value){ if( (sq->rear + 1) % QUEUE_INIT_SIZE == sq->front){ printf("OVERFLOW!\n"); return ERROR; } sq->base[sq->rear] = value; sq->rear = ( sq->rear + 1 ) % QUEUE_INIT_SIZE; return OK; } Status DeleteQueue(SuqQueue *sq,Elemtype* value){ if( sq->front == sq->rear ){ printf("EMPTY!\n"); return ERROR; } *value = sq->base[sq->front]; sq->front = ( sq->front + 1 ) % QUEUE_INIT_SIZE; return OK; } int main(){ SuqQueue s; InitQueue(&s); InsertQueue(&s,1); InsertQueue(&s,2); InsertQueue(&s,3); InsertQueue(&s,4); Elemtype r; DeleteQueue(&s,&r); printf("%d ", r); DeleteQueue(&s,&r); printf("%d ", r); DeleteQueue(&s,&r); printf("%d ", r); }
相关文章推荐