C 实现ArrayQueue队列

本文介绍了一种基于泛型ArrayList实现的队列结构,包括元素的入队、出队、容量控制及移除指定位置元素等核心功能,并通过topIndex来标识当前队列元素的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     在自增数组的基础上实现的,依赖C 实现泛型ArrayList数组。完成以下功能:

  • 元素的入队出队
  • 容量控制
  • 移除指定位置元素

      
typedef struct
{
	ArrayList arrayList[1];

	/**
	 * ArrayQueue top element index
	 */
	int topIndex;
}
ArrayQueue;



typedef struct
{
	ArrayQueue* (*Create)            (int typeSize);
	void        (*Init)              (int typeSize, ArrayQueue* outArrayQueue);
	ArrayQueue* (*CreateWithCapacity)(int typeSize, int capacity);
	void        (*InitWithCapacity)  (int typeSize, int capacity, ArrayQueue* outArrayQueue);
	void        (*Release)           (ArrayQueue*   arrayQueue);

	/**
	 * Push element from elementPtr into ArrayQueue
	 *
	 * elementPtr point to element
	 * return elementPtr in ArrayQueue
	 */
	void*       (*Push)              (ArrayQueue* arrayQueue, void* elementPtr);

	/**
	 * Pop element from ArrayQueue
	 * return top elementPtr in ArrayQueue, if no element return defaultValuePtr
	 */
	void*       (*Pop)               (ArrayQueue* arrayQueue, void* defaultValuePtr);

	/**
	 * Remove element at index range in [topIndex, ArrayQueue size - 1]
	 */
	void        (*RemoveAt)          (ArrayQueue* arrayQueue, int index);
}
_AArrayQueue_;

extern _AArrayQueue_ AArrayQueue[1];


/** The type is the ArrayQueue value type */
#define ArrayQueue(valueType) ArrayQueue

static void* Push(ArrayQueue* arrayQueue, void* elementPtr)
{
	if (arrayQueue->topIndex > arrayQueue->arrayList->array->length - arrayQueue->arrayList->size)
	{
		AArrayList->RemoveRange(arrayQueue->arrayList, 0, arrayQueue->topIndex - 1);
		arrayQueue->topIndex = 0;
	}

	return AArrayList->Add(arrayQueue->arrayList, elementPtr);
}

static void* Pop(ArrayQueue* arrayQueue, void* defaultValuePtr)
{
	ArrayList* arrayList = arrayQueue->arrayList;

	if (arrayQueue->topIndex > arrayList->size - 1)
	{
		return defaultValuePtr;
	}

	return (char*) arrayList->array->data + arrayList->typeSize * (arrayQueue->topIndex++);
}

static void RemoveAt(ArrayQueue* arrayQueue, int index)
{
	ArrayList* arrayList = arrayQueue->arrayList;

	ALog_A
	(
		index >= arrayQueue->topIndex && index < arrayList->size,
		"popIndex index = %d, out of range [%d, %d]",
		index, arrayQueue->topIndex, arrayList->size - 1
	);

	AArrayList->Remove(arrayList, index);
}

static void Release(ArrayQueue* arrayQueue)
{
	arrayQueue->topIndex = 0;
	AArrayList->Release(arrayQueue->arrayList);
}

static void InitWithCapacity(int typeSize, int capacity, ArrayQueue* outArrayQueue)
{
	if (capacity == 0)
	{
		AArrayList->Init(typeSize, outArrayQueue->arrayList);
	}
	else
	{
		AArrayList->InitWithCapacity(typeSize, capacity, outArrayQueue->arrayList);
	}

	outArrayQueue->topIndex = 0;
}

static ArrayQueue* CreateWithCapacity(int typeSize, int capacity)
{
	ArrayQueue* arrayQueue = (ArrayQueue*) malloc(sizeof(ArrayQueue));
	InitWithCapacity(typeSize, capacity, arrayQueue);

	return arrayQueue;
}

static void Init(int typeSize, ArrayQueue* outArrayQueue)
{
	InitWithCapacity(typeSize, 0, outArrayQueue);
}

static ArrayQueue* Create(int typeSize)
{
	return CreateWithCapacity(typeSize, 0);
}


_AArrayQueue_ AArrayQueue[1] =
{
	Create,
	Init,
	CreateWithCapacity,
	InitWithCapacity,
	Release,

	Push,
	Pop,
	RemoveAt,
};

    有topIndex来标示当前队列元素的位置。push元素的时候,如果队列空位个数大于剩余容量就会触发一次元素移动,用来缓解触发容量的自增。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值