分配限定范围内的不重复且可回收标识 ID 的类栈算法 C 语言实现

mirrorStack.h

/* MirrorStack Define Head file */

#ifndef MIRRORSTACK_H
#define MIRRORSTACK_H

#include <stdbool.h>

#define MAX_SIZE 10

typedef struct {
	int length;							/* The length of the stack */
	int top;							/* The head pointer of the stack */
	bool isUsed[MAX_SIZE];				/* Mark the num if was allocated */
	int value[MAX_SIZE];				/* The num to allocate */
} MirrorStack;

void InitMirrorStack(MirrorStack* mirrorStack);
bool AllocateMirrorId(MirrorStack* mirrorStack, int* mirrorId);
bool RecycleMirrorId(MirrorStack* mirrorStack, int mirrorId);

#endif // !MIRRORSTACK_H

mirrorStack.c

/* mirrorStack achieve source file */

#include "mirrorStack.h"
#include <stdio.h>

/* Initialize the MirrorStack */
void InitMirrorStack(MirrorStack* mirrorStack)
{
	if (mirrorStack == NULL) {
		printf("Error: NULL Pointer\n");
	}

	mirrorStack->length = MAX_SIZE;
	mirrorStack->top = MAX_SIZE - 1;
	for (int i = 0; i < MAX_SIZE; ++i) {
		mirrorStack->value[i] = MAX_SIZE - 1 - i;
		mirrorStack->isUsed[i] = false;
	}
}

/* The range the num can be allocated is 0 ~ MAX_SIZE - 1 */
bool AllocateMirrorId(MirrorStack* mirrorStack, int* mirrorId)
{
	if (mirrorStack == NULL || mirrorId == NULL) {
		printf("Error: NULL Pointer\n");
	}

	if (mirrorStack->length == 0) {
		printf("Error: Allocate mirrorId failed!\n");
		return false;
	}

	*mirrorId = mirrorStack->value[mirrorStack->top];
	mirrorStack->isUsed[mirrorStack->top] = true;
	if (--mirrorStack->length == 0) {
		return true;
	}

	for (int i = MAX_SIZE - 1; i >= 0; --i) {
		if (!mirrorStack->isUsed[i]) {
			mirrorStack->top = i;
			break;
		}
	}

	return true;
}

/* Recycle the valid mirrorId */
bool RecycleMirrorId(MirrorStack* mirrorStack, int mirrorId)
{
	if (mirrorStack == NULL) {
		printf("Error: NULL Pointer\n");
	}

	int currLocation = MAX_SIZE - 1 - mirrorId;
	if (mirrorId < 0 || mirrorId >= MAX_SIZE || !mirrorStack->isUsed[currLocation]) {
		printf("Error: Recycle mirrorId failed!\n");
		return false;
	}
	
	mirrorStack->isUsed[currLocation] = false;
	mirrorStack->top = (mirrorStack->top < currLocation) ? currLocation : mirrorStack->top;
	++mirrorStack->length;

	return true;
}

main.c

/* Entry program source file */

#include "mirrorStack.h"
#include <stdio.h>

int main(void)
{
	MirrorStack mirrorStack = { 0 };
	InitMirrorStack(&mirrorStack);
	int count = 0;
	int mirrorId = -1;
	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	printf("Recycle mirrorId 1\n");
	RecycleMirrorId(&mirrorStack, 1);

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	printf("Recycle mirrorId 2\n");
	RecycleMirrorId(&mirrorStack, 2);

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	for (int i = 0; i < 6; ++i) {
		if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
			printf("mirrorId %d = %d\n", count++, mirrorId);
		}
	}

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	RecycleMirrorId(&mirrorStack, 4);
	RecycleMirrorId(&mirrorStack, 6);

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	if (AllocateMirrorId(&mirrorStack, &mirrorId)) {
		printf("mirrorId %d = %d\n", count++, mirrorId);
	}

	RecycleMirrorId(&mirrorStack, 6);
	RecycleMirrorId(&mirrorStack, 6);
	RecycleMirrorId(&mirrorStack, -1);

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值