Chinaunix首页 | 论坛 | 博客
  • 博客访问: 926931
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: C/C++

2010-03-17 00:24:36

#include
#define maxsize 10
typedef struct
{
    int elem[maxsize];
    int front,rear;
}queue;
void init_queue(queue *cp)
{
    cp->front=0;
    cp->rear=0;
}
void en_queue(queue *cp,int x)
{
    if((cp->rear+1)%maxsize==cp->front)
        printf("The quequ is full!!");
    else
    {
        cp->rear=(cp->rear+1)%maxsize;
        cp->elem[cp->rear]=x;
    }
}
int dl_queue(queue *cp)
{
    if(cp->front==cp->rear)
        {printf("The queue is empty!!");
        return -1;
         }
    else
    {
        cp->front=(cp->front+1)%maxsize;
        return(cp->elem[cp->front]);
     }
}
void print(queue *cp)
{
    int i;
    for(i=cp->front+1;i<=cp->rear;i++)
    {
        printf("[%d]",cp->elem[i]);
    }
}
void main()
{
    int x,y;
    int select;
    queue *cp=NULL;
    init_queue(cp);
    do
    {
        printf("\n(1) Input a data");
        printf("\n(2) Output a  data");
        printf("\n(3) Exit");
        printf("\nPlease select one:");
        scanf("%d",&select);
        switch(select)
        {
            case 1:printf("\nPlease input the data:");
                   scanf("%d",&x);
                   en_queue(cp,x);
                   printf("\nThe queue is: ");
                   print(cp);
                   break;
            case 2:y=dl_queue(cp);
                   printf("\nThe queue is: ");
                   print(cp);
                   printf("\nThe putout data is %d",y);
                   break;
            case 3:break;
        }
    }
    while(select<3);
    getch();
}

 
阅读(438) | 评论(0) | 转发(0) |
0

上一篇:C 二叉树基本操作

下一篇:C队列线性存储

给主人留下些什么吧!~~