栈的数组实现:
stack.h
#ifndef _Stack_h
#define ElementType int
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty( Stack S );
int IsFull( Stack S );
Stack CreatStack( int MaxElements );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push(ElementType X, Stack S);
ElementType Top( Stack S );
void Pos( Stack S );
ElementType TopAndPos( Stack S );
#endif
stack.c
#include "stack.h"
#include <stdio.h>
#include <malloc.h>
#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
Stack
CreatStack( int MaxElements )
{
Stack S;
if( MaxElements < MinStackSize )
return NULL;
S = (Stack)malloc( sizeof(struct StackRecord ) );
if( S == NULL )
return NULL;
S->Array = (ElementType *)malloc( sizeof(ElementType) * MaxElements );
if( S->Array == NULL )
return NULL;
S->Capacity = MaxElements;
MakeEmpty( S );
return S;
}
void
DisposeStack( Stack S )
{
if( S != NULL )
{
free(S->Array);
free(S);
}
}
int
IsEmpty( Stack S )
{
return S->TopOfStack == EmptyTOS;
}
int
IsFull( Stack S )
{
return S->TopOfStack == S->Capacity - 1;
}
void
MakeEmpty( Stack S )
{
S->TopOfStack = EmptyTOS;
}
void
Push( ElementType X, Stack S )
{
if( IsFull( S ) )
return;
else
S->Array[++S->TopOfStack] = X;
}
ElementType
Top( Stack S )
{
if( IsEmpty( S ) )
return -1;
else
return S->Array[S->TopOfStack];
}
void
Pop( Stack S )
{
if( IsEmpty( S ) )
return;
else
S->TopOfStack--;
}
ElementType
TopAndPop( Stack S )
{
if( IsEmpty( S ) )
return -1;
else
{
ElementType Tmp = S->Array[S->TopOfStack];
S->TopOfStack--;
return Tmp;
}
}
int
main()
{
Stack S;
S = CreatStack(10);
printf("S is empty : %d", IsEmpty( S ));
printf("\n");
int i;
for(i = 0; i < 6; i++)
Push(i, S);
while( !IsEmpty( S ) )
{
printf( " %d ", TopAndPop( S ) );
}
printf("\n");
return 0;
}
栈的链表实现
stack.h
#ifndef _Stack_H
#define ElementType int
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
int IsEmpty( Stack S );
Stack CreatStack( void );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push( ElementType x, Stack S );
ElementType Top( Stack S );
void Pop( Stack S );
#endif
stack.c
#include "stack.h"
#include <stdio.h>
#include <malloc.h>
struct Node
{
ElementType Element;
PtrToNode Next;
};
int
IsEmpty( Stack S )
{
return S->Next == NULL;
}
Stack
CreatStack( void )
{
Stack S;
S = malloc( sizeof(struct Node) );
if( S == NULL )
return ;
S->Next = NULL;
MakeEmpty( S );
return S;
}
void
MakeEmpty( Stack S )
{
if( S == NULL )
return ;
else
while( !IsEmpty(S) )
Pop( S );
}
void
Push( ElementType X, Stack S )
{
PtrToNode TmpCell;
TmpCell = malloc( sizeof(struct Node) );
if( TmpCell == NULL )
return ;
else
{
TmpCell->Element = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
ElementType
Top( Stack S )
{
if( !IsEmpty( S ) )
return S->Next->Element;
return 0;
}
void
Pop( Stack S )
{
PtrToNode FirstCell;
if( IsEmpty( S ) )
return ;
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free(FirstCell);
}
}
int main()
{
Stack S;
S = CreatStack();
int i;
for( i = 1; i < 6; i++ )
Push(i, S);
for( i = 0; i < 5; i++)
{
printf("%d\n", Top(S));
Pop(S);
}
return 0;
}