Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

队列

$
0
0

队列:

队列是先进先出,栈是先进后出;

队列的一般操作如下:

   创建空的队列

   判断队列是否为空

   判断队列是否为满

   进队操作(向队列尾部添加元素)

   出对操作(从队列中弹出元素)

   释放队列

 

/*

 *queue.cpp

 *

 * Createdon:2013-9-24

 *     Author:mikechen

 */

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#defineSUCC  1

#defineFAIL  0

typedefstructQUEUE{

        void**ppData;//存放数据的指针数组

        unsignedintuMaxCount;       //队列中的最大数据量

        unsignedintuHead;   //队列的头部指针

        unsignedintuTail;   //队列的尾部指针

}QUEUE;

QUEUE*QueueCreate(unsignedintuMaxCount)

{

        QUEUE*pQueue;

        if(uMaxCount==0)

               returnNULL;

        pQueue=(QUEUE*)malloc(sizeof(QUEUE));

        if(pQueue==NULL)

               returnNULL;

        pQueue->ppData=(void**)malloc(sizeof(void*)*uMaxCount);

        if(pQueue->ppData==NULL)

        {

               free(pQueue);

               returnNULL;

        }

        pQueue->uHead=0;

        pQueue->uTail=0;

        pQueue->uMaxCount=uMaxCount;

        returnpQueue;

}

voidQueueDestroy(QUEUE*pQueue)

{

        if(pQueue!=NULL)

        {

               free(pQueue->ppData);

               free(pQueue);

        }

}

unsignedintQueueInsertTail(QUEUE*pQueue,void*pData)

{

        unsignedintuTailNext;

        if(pQueue->uTail==pQueue->uMaxCount-1)

        {

               uTailNext=0;

        }

        else

        {

               uTailNext=pQueue->uTail+1;

        }

        if(pQueue->uTail!=pQueue->uHead)  //队列未满

        {

               pQueue->ppData[pQueue->uTail]=pData;

               pQueue->uTail=uTailNext;

        }

        else   //队列已满,将空间扩大一倍

        {

               void**ppData=(void**)malloc(pQueue->uMaxCount*2*sizeof(void*));

               if(ppData==NULL)

                       returnFAIL;

               inti;

               if(pQueue->uHead>pQueue->uTail)

               {

                       for(i=pQueue->uHead;i<pQueue->uMaxCount;i++)

                       {

                               ppData[i]=pQueue->ppData[i];

                       }

                       for(i=0;i<pQueue->uTail;i++)

                       {

                               ppData[i+pQueue->uMaxCount]=pQueue->ppData[i];

                       }

                       pQueue->uTail+=pQueue->uMaxCount;

               }

               else

               {

                       for(i=pQueue->uHead;i<pQueue->uTail;i++)

                       {

                               ppData[i]=pQueue->ppData[i];

                       }

               }

               ppData[pQueue->uTail]=pData;

               pQueue->uTail+=1;

               pQueue->uMaxCount*=2;

               free(pQueue->ppData);

               pQueue->ppData=ppData;

        }

        returnSUCC;

}

void*QueuePopHead(QUEUE*pQueue)

{

        unsignedintuHead;

        uHead=pQueue->uHead;

        if(uHead!=pQueue->uTail)   //头部和尾部没有重合,表示队列不为空

        {

               if(uHead==pQueue->uMaxCount-1)

                       pQueue->uHead=0;

               else

                       pQueue->uHead+=1;

               returnpQueue->ppData[uHead];

        }

        returnNULL;

}

unsignedintQueueIsEmpty(QUEUE*pQueue)

{

        return(pQueue->uHead!=pQueue->uTail)?SUCC:FAIL;

}

voidmain()

{

        char*Data[]={"helloword!","TestMyStack"};

        char*GetData[64];

        QUEUE*pQueue;

        unsignedintuMaxCount=8;

        pQueue=QueueCreate(uMaxCount);

        if(pQueue==NULL)

        {

               printf("Createqueuefail\n");

               return;

        }

        bzero(GetData,sizeof(GetData));

        QueueInsertTail(pQueue,Data[0]);

        QueueInsertTail(pQueue,Data[1]);

        GetData[0]=QueuePopHead(pQueue);

        GetData[1]=QueuePopHead(pQueue);

        printf("QueueGetData[0]:%s\n",GetData[0]);

        printf("QueueGetData[1]:%s\n",GetData[1]);

}

运行结果:

 

作者:chenliang0224 发表于2013-9-24 15:03:45 原文链接
阅读:31 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>