stack link

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct tagStackNode
{
	int iValue;
	struct tagStackNode *pstNext;
}STACK_NODE_S;

static STACK_NODE_S *pstStack;

#define STACK_TOP() \
	pstStack;

#define STACK_SET_TOP(pstTop) \
{ \
	pstStack = pstTop; \
}

int STACK_Push(int iNum)
{
	int iRet = -1;
	STACK_NODE_S *pstTop;
	
	pstTop = (STACK_NODE_S *)malloc(sizeof(STACK_NODE_S));
	if (pstTop != NULL)
	{
		memset(pstTop, 0, sizeof(STACK_NODE_S));
		pstTop->iValue = iNum;
		
		pstTop->pstNext = STACK_TOP();
		STACK_SET_TOP(pstTop);
		
		iRet = 0;
	}	
	
	return iRet;
}

void STACK_Pop()
{
	STACK_NODE_S *pstCurTop = NULL;
	
	pstCurTop = STACK_TOP();
	
	assert(pstCurTop != NULL);
	
	STACK_SET_TOP(pstCurTop->pstNext);	

	free(pstCurTop);

	return;
}

int STACK_Top()
{
	STACK_NODE_S *pstTop;

	pstTop = STACK_TOP();

	assert(pstTop != NULL);

	return pstTop->iValue;
}

int STACK_IsEmpty()
{
	STACK_NODE_S *pstTop;
	pstTop = STACK_TOP();
	return pstTop == NULL;
}

int main()
{
	int iRet = -1;
	int i;
	STACK_NODE_S *pstTop;
	printf("Push:\r\n");
	for (i = 0; i < 5; i++)
	{
		iRet = rand();
		printf("%d ", iRet);
		STACK_Push(iRet);
	}	
	printf("\r\n");	

	printf("Display and Pop.\r\n");
	while (STACK_IsEmpty() == 0)
	{
		printf("%d ", STACK_Top());
		STACK_Pop();	
	}
	
	return iRet;
}
 

Result

Push:
1804289383 846930886 1681692777 1714636915 1957747793
Display and Pop.
1957747793 1714636915 1681692777 846930886 1804289383

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值