#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;
}
栈ADT(有测试例程)
最新推荐文章于 2022-01-24 16:38:07 发布