码迷,mamicode.com
首页 > 编程语言 > 详细

队列的数组实现

时间:2015-10-31 17:00:49      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

//    队列的数组实现


#include <cstdio>
#include <cstdlib>
#define maxsize 100
//#define _OJ_

typedef struct Lnode
{
    int top;
    int base;
    int *array;
} qnode, *queue;


void
creat_queue(queue q)
{
    q->array = (int *) malloc (sizeof(int) * maxsize);
    q->top = q->base = 0;
}

int
isempty(queue q)
{
    if(q->top - q->base == 0)
        return 1;
    else
        return 0;
}

void
Enqueue(queue q)
{
    int x;
    scanf("%d", &x);
    //printf("%d\n", x);
    q->array[q->top++] = x;
}


int
DEqueue(queue q)
{
    int e;
    e = q->array[q->base++];
    return e;
}

void
print(queue q)
{
    int i;
    for(i = q->base;i < q->top; i++) {
    printf("%d\n", q->array[i]);
     }
}





int main(int argc, char const *argv[]) {
#ifndef _OJ_  //ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif


    int i, n;
    queue q;
    creat_queue(q);
    printf("null == %d\n", isempty(q));
    scanf("%d", &n);

     while (n--) {
        printf("%d\n", n);
        Enqueue(q);
     }
     while (q->base != q->top) {
       printf("out == %d\n", DEqueue(q));
     }

//不知为什么此处条件不可以为n--

 





    return 0;
}

队列的数组实现

标签:

原文地址:http://www.cnblogs.com/airfand/p/4925732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!