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;
}