最近复习了C语言的相关知识,实现了数据结构中的静态队列。
下面是Main.c文件:
#include "StaticQueue.h"
#include <stdio.h>
int main()
{
StaticQueue* queue = StaticQueueCreate();
if(queue != NULL)
{
int i, len = -1, data = -1;
for(i = 0; i < StaticQueueCapacity(queue); i++)
{
StaticQueueEnqueue(queue, i);
}
printf("插入状态:%d\n\n", StaticQueueEnqueue(queue, 100));
len = StaticQueueLength(queue);
printf("len = %d\n\n", len);
for(i = 0; i < len / 2; i++)
{
StaticQueueDequeue(queue, NULL);
}
len = StaticQueueLength(queue);
printf("len = %d\n\n", len);
printf("打印队列中元素:\n");
for(i = 0; i < len; i++)
{
int data;
StaticQueueDequeue(queue, &data);
printf("queue[%02d] = %-2d\n", i, data);
}
printf("删除状态:%d\n\n", StaticQueueDequeue(queue, NULL));
printf("获取数据元素状态:%d\n\n", StaticQueueFront(queue, &data));
queue = StaticQueueDestroy(queue);
len = StaticQueueLength(queue);
printf("len = %d\n\n", len);
}
return 0;
}
下面是StaticQueue.h文件:
#ifndef __STATICQUEUE_H__
#define __STATICQUEUE_H__
#define deBug() printf("File = %s\nLine = %d\n", __FILE__, __LINE__)
typedef int DataType;
#define __QUEUE_MAX_SIZE 100
#include <stdio.h>
#include <stdlib.h>
#ifndef __cplusplus
typedef int Bool;
#define true 1
#define false 0
#else
typedef bool Bool;
#endif
#define MALLOC(type, size) (type*)malloc(sizeof(type) *size)
#define FREE(p) (free(p), p = NULL)
#define STRUCT(type) typedef struct __struct##type type;\
struct __struct##type
STRUCT(StaticQueue)
{
DataType data[__QUEUE_MAX_SIZE];
int len;
int front;
int tail;
};
StaticQueue* StaticQueueCreate(void);
StaticQueue* StaticQueueDestroy(StaticQueue *queue);
int StaticQueueLength(StaticQueue *queue);
int StaticQueueCapacity(StaticQueue *queue);
Bool StaticQueueEnqueue(StaticQueue *queue, DataType data);
Bool StaticQueueDequeue(StaticQueue *queue, DataType *data);
Bool StaticQueueFront(StaticQueue *queue, DataType *data);
#endif
下面是StaticQueue.c文件:
#include "StaticQueue.h"
StaticQueue* StaticQueueCreate(void) //O(1)
{
StaticQueue *ret = MALLOC(StaticQueue, 1);
if(ret)
{
ret->len = 0;
ret->front = 0;
ret->tail = 0;
}
return ret;
}
StaticQueue* StaticQueueDestroy(StaticQueue *queue) //O(1)
{
return FREE(queue);
}
int StaticQueueLength(StaticQueue *queue) //O(1)
{
return (queue != NULL) ? queue->len : -1;
}
int StaticQueueCapacity(StaticQueue *queue) //O(1)
{
return (queue != NULL) ? __QUEUE_MAX_SIZE : -1;
}
Bool StaticQueueEnqueue(StaticQueue *queue, DataType data) //O(1)
{
Bool ret = (queue != NULL) && (queue->len < __QUEUE_MAX_SIZE);
if(ret)
{
queue->data[queue->tail] = data;
queue->tail = (queue->tail + 1) % __QUEUE_MAX_SIZE;
queue->len++;
}
return ret;
}
Bool StaticQueueDequeue(StaticQueue *queue, DataType *data) //O(1)
{
Bool ret = (queue != NULL) && (queue->len > 0);
if(ret)
{
if(data != NULL) *data = queue->data[queue->front];
queue->front = (queue->front + 1) % __QUEUE_MAX_SIZE;
queue->len--;
}
return ret;
}
Bool StaticQueueFront(StaticQueue *queue, DataType *data) //O(1)
{
return (((queue != NULL) && (data != NULL) && (queue->len > 0)) ? (*data = queue->data[queue->front], true) : false);
}
下面是运行结果: