这次更新的关于栈的内容在上次的基础上修补了一部分,上次的内容本身非常简单初级,并且没有考虑栈顶溢出的情况,这次补充。
其次补充了多线程栈的内容,需要去了解锁的知识,在处理数据时都需要加锁。
然后近期正在看一个大概3万行的项目代码,时间紧张,暂不更。
栈在检测到要溢出时加倍。代码如下。
头文件:
#ifndef __STACK_H__ #define __STACK_H__ #include "Global.h" #define CAPI_FAILED 0 #define CAPI_SUCCESS 1 typedef struct STACK_at { void** ppBase; UINT uTop; unsigned uStackSize; }STACK; STACK* Stack_Create(UINT uStackSize); void Stack_Destroy(STACK* pStack, DESTROYFUNC DestroyFunc); void* Stack_Pop(STACK* pStack); INT Stack_Push(STACK* pStack, void* pData); INT Stack_IsEmpty(STACK* pStack); #endif
c文件:
#include "stack.h" STACK* Stack_Create(UINT uStackSize) { STACK* pStack; if (uStackSize == 0) { return NULL; } pStack = (STACK*)malloc(sizeof(struct STACK_at)); if (pStack != NULL) { pStack->ppBase = (void**)malloc(uStackSize * sizeof(void*)); if (pStack->ppBase == NULL) { free(pStack); pStack = NULL; } else { pStack->ppBase[0] = NULL; pStack->uTop = 0; pStack->uStackSize = uStackSize; } } return pStack; } void Stack_Destroy(STACK* pStack, DESTROYFUNC DestroyFunc) { if (pStack != NULL) { if (pStack->ppBase != NULL) { if (DestroyFunc != NULL) { UINT i; for (i = 0; i < pStack->uTop; i++) { if (pStack->ppBase[i] != NULL) { (*DestroyFunc)(pStack->ppBase[i]); } } } free(pStack->ppBase); } free(pStack); pStack = NULL; } } void* Stack_Pop(STACK* pStack) { void* pData; if (pStack == NULL || pStack->uTop == 0) { return NULL; } pStack->uTop -= 1; pData = pStack->ppBase[pStack->uTop]; return pData; } INT Stack_IsEmpty(STACK* pStack) { if (pStack->uTop = 0) { return 1; } return 0; } INT Stack_Push(STACK* pStack, void* pData) { if (pStack == NULL) { return CAPI_FAILED; } if (pStack->uTop >= pStack->uStackSize - 1) { pStack->ppBase = (void**)realloc(pStack->ppBase, (pStack->uStackSize * 2) * sizeof(void*)); if (pStack->ppBase == NULL) { return CAPI_FAILED; } pStack->uStackSize *= 2; } pStack->ppBase[pStack->uTop] = pData; pStack->uTop += 1; return CAPI_SUCCESS; }
多线程栈
头文件如下:
#ifndef __MSTACK_H__ #define __MSTACK_H__ #include "stack.h" typedef struct MSTACK_st { STACK* pStack; FASTLOCK FastLock; }MSTACK; MSTACK* MStack_Create(UINT uStackSize); void MStack_Destroy(MSTACK* pMStack, DESTROYFUNC DestroyFunc); void* MStack_Pop(MSTACK* pMStack); INT MStack_Push(MSTACK* pMStack, void* pData); INT MStack_IsEmpty(MSTACK* pMStack); #endif
c文件如下
#include "MStack.h" MSTACK* MStack_Create(UINT uStackSize) { MSTACK* pMStack = (MSTACK*)malloc(sizeof(MSTACK)); if (pMStack != NULL) { pMStack->pStack = Stack_Create(uStackSize); if (pMStack->pStack != NULL) { /*锁*/ InitializeCriticalSection(&(pMStack->FastLock)); return pMStack; } free(pMStack); } return NULL; } void MStack_Destroy(MSTACK* pMStack, DESTROYFUNC DestroyFunc) { if (pMStack != NULL) { EnterCriticalSection(&(pMStack->FastLock)); Stack_Destroy(pMStack->pStack, DestroyFunc); DeleteCriticalSection(&(pMStack->FastLock)); free(pMStack); } } void* MStack_Pop(MSTACK* pMStack) { void* pData; EnterCriticalSection(&(pMStack->FastLock)); pData = Stack_Pop(pMStack->pStack); DeleteCriticalSection(&(pMStack->FastLock)); return pData; } INT MStack_Push(MSTACK* pMStack, void* pData) { INT nRet; EnterCriticalSection(&(pMStack->FastLock)); nRet = Stack_Push(pMStack->pStack, pData); DeleteCriticalSection(&(pMStack->FastLock)); return nRet; } INT MStack_IsEmpty(MSTACK* pMStack) { INT nRet; EnterCriticalSection(&(pMStack->FastLock)); nRet = Stack_IsEmpty(pMStack->pStack); DeleteCriticalSection(&(pMStack->FastLock)); return nRet; }