靜態佇列——基於C語言

2020-08-12 15:01:35

最近複習了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);
}

下面 下麪是執行結果:
在这里插入图片描述
在这里插入图片描述