您的位置:首页 > 理论基础 > 数据结构算法

数据结构与算法学习笔记——队列

2013-10-03 20:28 871 查看
/********************************************************
* Copyright(C):
* Filename: queue.c
* Author:
* Version:
* Date:
* Description:
********************************************************/

#include <malloc.h>
#include <stdio.h>

#include "queue.h"

//----------------------------------
// Enqueue()
//
//
//
void
Enqueue(Queue *Q, ElementType X){

Q->size++;
if ( ++(Q->rear) == Q->capacity ){
Q->rear = 0;
}
Q->array[ Q->rear ] = X;
}

//----------------------------------
// Dequeue()
//
//
//
ElementType
Dequeue(Queue *Q){

Q->size--;
if ( ++(Q->front) == Q->capacity ){
Q->front = 0;
}
return Q->array[ Q->front ];
}

//----------------------------------
// DisposeQueue()
//
//
//
void
DisposeQueue(Queue * Q){

Q->size = 0;
Q->rear = -1;
Q->front = -1;
free(Q->array);
}

//----------------------------------
// InitQueue
//
//
//
int
InitQueue(Queue *Q, int capacity){

if ( capacity < 1 ){
return -1;
}

Q->size = 0;
Q->rear = -1;
Q->front = -1;

if ( (Q->array=(ElementType*)malloc(sizeof(ElementType)*capacity)) != NULL ){
Q->capacity = capacity;

return 0;
}

return -1;
}

//----------------------------------
// IsEmptyQueue()
//
//
//
int
IsEmptyQueue(Queue *Q){
return Q->size <= 0;
}

//----------------------------------
// IsFullQueue()
//
//
//
int
IsFullQueue(Queue *Q){
return Q->size == Q->capacity;
}

//----------------------------------
// MakeEmptyQueue()
//
//
//
void
MakeEmptyQeue(Queue *Q){
Q->size = 0;
Q->front = 1;
Q->rear = 0;
}


/********************************************************
* Copyright(C):
* Filename: queue.h
* Author:
* Version:
* Date:
* Description:
********************************************************/
#ifndef QUEUE_H
#define QUEUE_H

#define ElementType int

typedef struct QueueRecord
{
int front;
int rear;
int size;
int capacity;
ElementType *array;
}Queue;

ElementType Dequeue(Queue *Q);
void DisposeQueue(Queue *Q);
void Enqueue(Queue* Q, ElementType X);
void MakeEmptyQueue(Queue *Q);
int InitQueue(Queue *Q, int capacity);
int IsEmptyQueue(Queue *Q);
int IsFullQueue(Queue *Q);

#endif  // QUEUE_H
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: