-
-
看到论坛一个帖子说把012345678倒序输出,我用栈实现[C语言]
-
发表于:
2007-5-19 23:36
7614
-
看到论坛一个帖子说把012345678倒序输出,我用栈实现[C语言]
看到论坛一个帖子说把012345678倒序输出,我用栈实现[C语言],顺便复习一下数据结构算法
// Test-stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <cstdlib>
#define STATCK_INIT_SIZE 100
typedef struct _STACK
{
PULONG pulong_Top ;
PULONG pulong_Base ;
int int_CurStackSize ;
}STRUCT_STACK, *PSTRUCT_STACK;
BOOL
Stack_Init(IN OUT PSTRUCT_STACK pstruct_param_Stack) ;
BOOL
Stack_GetTop(IN PSTRUCT_STACK pstruct_param_Stack,
OUT PULONG pulong_param_Object) ;
BOOL
Stack_Push(IN PSTRUCT_STACK pstruct_param_Stack,
IN PULONG pulong_param_Object) ;
BOOL
Stack_Pop(IN PSTRUCT_STACK pstruct_param_Stack,
IN OUT PULONG pulong_param_Object) ;
int _tmain(int argc, _TCHAR* argv[])
{
STRUCT_STACK struct_Stack ;
ULONG ulong_StackData = 0,
ulong_Count = 0 ;
memset(&struct_Stack,
0,
sizeof(STRUCT_STACK)) ;
// 初始化栈[填入0~8个数字]
Stack_Init(&struct_Stack) ;
for (ulong_Count = 0;
ulong_Count < 9;
ulong_Count++)
{
// 数据入栈
Stack_Push(&struct_Stack,
&ulong_Count) ;
}
// 获取栈顶数据[这里应该是8]
Stack_GetTop(&struct_Stack,
&ulong_StackData) ;
printf("Stack top: 0x%I32X \n", ulong_StackData) ;
// 从栈顶开始打印数据[876543210]
printf("Stack data: ") ;
for (ulong_Count = 0;
ulong_Count < 9;
ulong_Count++)
{
printf("0x%I32X ", *(struct_Stack.pulong_Top-ulong_Count-1)) ;
}
// 释放内存[这个不跟栈算法有关系了]
free(struct_Stack.pulong_Base) ;
getchar() ;
return 0;
}
BOOL
Stack_Init(IN OUT PSTRUCT_STACK pstruct_param_Stack)
{
pstruct_param_Stack->pulong_Base = (PULONG)malloc(STATCK_INIT_SIZE*sizeof(ULONG)) ;
memset(pstruct_param_Stack->pulong_Base,
0,
STATCK_INIT_SIZE*sizeof(ULONG)) ;
if (!pstruct_param_Stack->pulong_Base)
{
return FALSE ;
}
pstruct_param_Stack->pulong_Top = pstruct_param_Stack->pulong_Base ;
pstruct_param_Stack->int_CurStackSize = STATCK_INIT_SIZE ;
return TRUE ;
}//End Stack_Init
BOOL
Stack_GetTop(IN PSTRUCT_STACK pstruct_param_Stack,
OUT PULONG pulong_param_Object)
{
if (pstruct_param_Stack->pulong_Base ==
pstruct_param_Stack->pulong_Top)
{
return FALSE ;
}
*pulong_param_Object = *(pstruct_param_Stack->pulong_Top-1) ;
return TRUE ;
}// End Stack_GetTop()
BOOL
Stack_Push(IN PSTRUCT_STACK pstruct_param_Stack,
IN PULONG pulong_param_Object)
{
*(pstruct_param_Stack->pulong_Top++) = *pulong_param_Object ;
return TRUE ;
}// End Stack_Push()
BOOL
Stack_Pop(IN PSTRUCT_STACK pstruct_param_Stack,
IN OUT PULONG pulong_param_Object)
{
*pulong_param_Object = *(--pstruct_param_Stack->pulong_Top) ;
}// End Stack_Pop()
[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法