嵌入式C语言数据结构之堆栈

堆栈拥有先进后出的特性;

函数跳转时的现场保护和恢复和堆栈息息相关;

代码如下:

stack.h

#ifndef __STACK_H__
#define __STACK_H__

#include "stm32f10x.h"
#include <string.h>

#define ListStack      0x00
#define StaticStack    0x01

#define UpStack        0x00
#define DownStack      0x01

#define MaxStackLength 20
	
#define StackMode      ListStack
#define StackGrowMode  DownStack

#if StackMode == ListStack
  #include "list.h"
	#define ListStackMode 1
	typedef struct
	{
		ListNode * pHead;
		uint32_t length;
	}Str_Stack;
#endif

#if StackMode == StaticStack 
	#define StaticStackMode 1
	typedef struct 
	{
		uint32_t array[MaxStackLength];
		uint32_t Location;
		uint32_t length;
	}Str_Stack;

#endif

typedef unsigned int StackStates;

StackStates Stack_Init(Str_Stack *pStack);
StackStates Stack_Pop (Str_Stack *pStack , uint32_t *pBuffer);
StackStates Stack_Push(Str_Stack *pStack , const uint32_t value);


#endif

stack.c

#include "stack.h"

#ifdef ListStackMode
	ListNode MyStackList = {0};
#endif
	
/**
  * @name   Stack_Init.
  * @brief  Creat a stack.
  * @param  ListNode * pStack   : Head of the stack.
  * @retval 0                   : Succeed.
						1                   : Stack is NULL.
	* @author Jiao Wo Yi Sheng Xiao Ming Ge.
	* @Date   2018.6.8
  **/
StackStates Stack_Init(Str_Stack *pStack)
{
	if(pStack == NULL)
		return 1;
	#ifdef StaticStackMode
		pStack->length = 0;
		pStack->Location = 0;
		memset(pStack->array,0x00,sizeof(pStack->array));
	#endif
	#ifdef ListStackMode
		ListInit(&MyStackList,0);
		pStack->length = 0;
		pStack->pHead = &MyStackList;
	#endif
	return 0;
}


/**
  * @name   Stack_Push.
  * @brief  Push a value.
  * @param  ListNode * pStack     : Head of the stack.
						const uint32_t vlaue  : The value you push.
  * @retval 0                     : Succeed.
						1                     : Stack is NULL.
	* @author Jiao Wo Yi Sheng Xiao Ming Ge.
	* @Date   2018.6.8
  **/
StackStates Stack_Push(Str_Stack *pStack,const uint32_t value)
{
	if(pStack == NULL)
		return 1;
	#ifdef StaticStackMode
		#if StackGrowMode == DownStack
		if(pStack->Location >= MaxStackLength )
			return 2;
		pStack->array[pStack->Location++] = value;
		#endif
		
		#if StackGrowMode == UpStack
		if(pStack->Location)
			pStack->array[(MaxStackLength - pStack->Location)++] = value;		
		#endif
	#endif	
    
  #ifdef ListStackMode
		#if StackGrowMode == DownStack
    ListInsertNode(pStack->pHead,MaxListLength,value);
		#endif
		
		#if StackGrowMode == UpStack
    ListInsertNode(pStack->pHead,1,value);    
		#endif
  #endif
	pStack->length ++;
	return 0;
}
/**
  * @name   Stack_Pop.
  * @brief  Pop a value.
  * @param  ListNode * pStack   : Head of the stack.
						uint32_t * pBuffer  : The output buffer.
  * @retval 0                   : Succeed.
						1                   : Stack is NULL.
            2                   : The output buffer is NULL.
	* @author Jiao Wo Yi Sheng Xiao Ming Ge.
	* @Date   2018.6.8
  **/
StackStates Stack_Pop(Str_Stack *pStack ,uint32_t *pBuffer)
{
  if(pBuffer == NULL)
    return 2;
	if(pStack == NULL)
		return 1;
	#ifdef StaticStackMode
		#if StackGrowMode == DownStack
			*pBuffer = pStack->array[pStack->Location--];
		#endif
		
		#if StackGrowMode == UpStack
			*pBuffer = pStack->array[pStack->Location++];			
		#endif
	#endif
	
	#ifdef ListStackMode
		#if StackGrowMode == DownStack
			ListDeleteNode(pStack->pHead,pStack->length,pBuffer);	
		#endif
		
		#if StackGrowMode == UpStack
			ListDeleteNode(pStack->pHead,1,pBuffer);	 
		#endif		
	#endif
	
	pStack->length --;
	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/silence3039/article/details/80628208