首页
社区
课程
招聘
看到论坛一个帖子说把012345678倒序输出,我用栈实现[C语言]
发表于: 2007-5-19 23:36 7613

看到论坛一个帖子说把012345678倒序输出,我用栈实现[C语言]

2007-5-19 23:36
7613
看到论坛一个帖子说把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()

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 209
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
for (ulong_Count = 0;
    ulong_Count < 9;
    ulong_Count++)
这是循环语句吧
另外,原题似乎没有固定原始值的大小。
2007-5-21 01:10
0
雪    币: 209
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
应该是:没有固定原始值的位数
2007-5-21 01:12
0
雪    币: 255
活跃值: (207)
能力值: ( LV9,RANK:250 )
在线值:
发帖
回帖
粉丝
4
for 里面 也会产生判断。
你看看asm代码,里面很多je jne ja js……
2007-5-21 11:28
0
雪    币: 8149
活跃值: (1875)
能力值: ( LV8,RANK:122 )
在线值:
发帖
回帖
粉丝
5
用的不是真栈哈
2007-5-25 11:03
0
雪    币: 209
活跃值: (19)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
是算法,用栈结构而已
2007-5-25 16:00
0
游客
登录 | 注册 方可回帖
返回
//