栈ADT(有测试例程)

#include <cstdio>
#include <cstdlib>
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

struct Node{
	int ele;
	PtrToNode Next;
};

bool IsEmpty( Stack S )
{
	return S->Next == NULL;
}

void Pop( Stack S );
void MakeEmpty( Stack S )
{
	if( S->Next == NULL ) 
		return;                           ///
	else
		while( !IsEmpty( S ) )
			Pop( S );
}

Stack CreatStack( )
{
	Stack S;
	S =(Node*) malloc( sizeof( struct Node ) );
	if( S == NULL )
		printf("Out of space !!!");
	S->Next = NULL;
	MakeEmpty( S );
	return S;
}

void Push( int X, Stack S )
{
	PtrToNode TmpCell;
	TmpCell = ( Node* ) malloc( sizeof( struct Node ) );
	if( TmpCell == NULL )
		printf("Out of Space !!!\n");
	else
	{
		TmpCell->ele = X;
		TmpCell->Next = S->Next;
		S->Next = TmpCell;
	}
}

int Top( Stack S )
{
	return S->Next->ele;
}

void Pop( Stack S )
{
	PtrToNode FirstCell;
	FirstCell = S->Next;
	S->Next = S->Next->Next;
	free( FirstCell );
}

int Size( Stack S )
{
	PtrToNode P = S->Next;
	int num = 0;
	while( P != NULL)
	{
		num++;
		P = P->Next;
	}
	return num;
}
	
int main()
{
	Stack S = CreatStack( );
	Push( 1, S );
	Push( 2, S );
	printf("%d \n",Top( S ));
	Pop( S );
	printf("%d \n",Top( S ));
	Pop( S );
	return 0;
}
	
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值