用户名: 密   码:
   飞诺网 加入收藏
飞诺网 软件编程 C C++ Java VB Delphi Foxpro 汇编语言 游戏开发 移动开发 软件工程师 软工与管理 VC shell编程 C#
C++系列教程 C++实例 C++技术文档 C++/C语言函数 Mangos

您当前的位置:飞诺网 >> c/c++ >> C++实例

静态数组队列(用不完全填满数组来区别满队列和空队列)

www.diybl.com    时间 : 2008-10-09  作者:佚名   编辑:本站 点击:   [ 评论 ]


#include <stdio.h>
#include <stdlib.h>
#ifndef QUEUE_SIZE
#define QUEUE_SIZE 12
#endif

typedef struct LinkQueue
{
        int front;
        int rear;
        int data[QUEUE_SIZE];
}LinkQueue;

void QueueInit(LinkQueue *Q)
{
        Q->front = 0;
        Q->rear = 1;
}

int QueueFull(LinkQueue *Q)
{
        return (Q->rear+1)%QUEUE_SIZE==Q->front;
}

int QueueEmpty(LinkQueue *Q)
{
        return (Q->front+1)%QUEUE_SIZE==Q->rear;
}

void EnQueue(LinkQueue *Q, int value)
{
        if(QueueFull(Q))
                printf("queue is full, your data will be covered!\n");
        Q->data[Q->rear] = value;
        Q->rear = (Q->rear+1)%QUEUE_SIZE;
}

void DeQueue(LinkQueue *Q)
{
        if(QueueEmpty(Q))
                printf("queue is empty!");
        Q->data[Q->front]=0;
        Q->front = (Q->front+1)%QUEUE_SIZE;
}

int QueueFront(LinkQueue *Q)
{
        if(!QueueEmpty(Q))
                return Q->data[Q->front];
}


void PrintLinkQueue(LinkQueue *Q)
{
        int i;
        for(i=Q->front; i<Q->rear; i++)
                printf("%4d",Q->data[i]);
        printf("\n");
}
int main(void)
{
        LinkQueue *Q=(LinkQueue *)malloc(sizeof(LinkQueue));
        int i;
        for(i=1; i<=20; i++)
                EnQueue(Q,i);
        PrintLinkQueue(Q);
        #if 0
        QueueInit(Q);
        EnQueue(Q,1);
        EnQueue(Q,2);
        EnQueue(Q,3);
        EnQueue(Q,4);
        EnQueue(Q,5);
        printf("打印这个队列:\n");
        PrintLinkQueue(Q);


        printf("插入二个元素6,7后的队列:\n");
        EnQueue(Q,6);
        EnQueue(Q,7);
        PrintLinkQueue(Q);

        printf("删除两个元素后的队列为:\n");
        DeQueue(Q);
        DeQueue(Q);
        PrintLinkQueue(Q);
        #endif

        return 0;


如果图片或页面不能正常显示请点击这里
C++实例推荐文章

文章评论