简易版C语言队列

.h文件

 

#ifndef Queue_h

#define Queue_h

 

typedef struct Queue *Queue;

 

typedef int State;

 

struct Queue {

    int *data;

    int len;

    int *head;

    int *tai;

};

#include <stdio.h>

Queue initQueue(int len);

State outQueue(Queue queue);

State inQueue(Queue queue,int value);

void printQueue(Queue queue);

#endif /* Queue_h */

.c文件

 

#include "Queue.h"

#include <mm_malloc.h>

 

Queue initQueue(int len){

    Queue queue=malloc(sizeof(Queue));

    queue->data=malloc(sizeof(int)*(len +1));

    queue->len=len;

    queue->head=queue->data;

    queue->tai=queue->data;

    return queue;

}

State inQueue(Queue queue,int value){

    if (&queue->data[queue->len+1]<=queue->tai+1) {

        return -1;//队列已满

    }else{

        

        *queue->tai=value;

        queue->tai++;

 

        printQueue(queue);

 

        return 0;//入队列成功

    }

}

State outQueue(Queue queue){

    if (queue->head+1>queue->tai) {

        queue->head--;

        return -1;//队列为空

    }else{

        queue->head++;

        printQueue(queue);

        return 0;//出队列成功

    }

}

void printQueue(Queue queue){

    int *q=queue->head;

    for (; q<queue->tai; q++) {

        printf("%d ",*q);

    }

    printf("\n");

}

 

posted @ 2018-04-13 11:46  杨悦··  阅读(124)  评论(0编辑  收藏  举报