Java教程

循环队列的实现

本文主要是介绍循环队列的实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100

struct queue{
    int front;
    int rear;
    int *p;
};

void init(struct queue* s )
{
    s->front=0;
    s->rear=0;
    s->p=(int*)malloc(sizeof(int*)*MAXSIZE);
}

void enqueue(struct queue* s,int e)
{
    if((s->rear+1)%MAXSIZE==s->front)
    {
        puts("queue full\n");
        exit;
    }
    s->p[s->rear]=e;
    s->rear++;
}

int dequeue(struct queue* s)
{
    int e;
    if(s->rear==s->front)
    {
        puts("queue empty\n");
        exit;
    }
    e=s->p[s->front];
    s->front++;
    return e;
}

void print_queue(struct queue* s)
{
    int i,j;
    i=s->front;j=s->rear;
    if(s->rear==s->front)
    {
        puts("queue empty can't print\n");
        exit;
    }
    if(s->rear>s->front)
        for(i;i<j;i++)
            printf("%d ",s->p[i]);
    printf("\n");
}

int main()
{
     struct queue tom;
    init(&tom);
    printf("tom.age=%d,tome.high=%d,tom->p=%p\n",tom.front,tom.rear,tom.p);
    enqueue(&tom,5);
    enqueue(&tom,10);
    enqueue(&tom,11);
    print_queue(&tom);
    dequeue(&tom);
    print_queue(&tom);
    return 0;
}

输出结果

tom.age=0,tome.high=0,tom->p=0x138f260
5 10 11
10 11

这篇关于循环队列的实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!