/*
* stack.cpp
*/
#include
#include "fatal.h"
#include "stack.h"
#ifndef _STACK_ARRAY_
/* --------------------Linked List Implementation of Stacks------------------- */
struct Node
{
ElementType Element;
PtrToNode Next;
};
/*
* Test whether a stack is empty.
*/
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
/*
* Create a header node.
* Then, set the next pointer to NULL.
*/
Stack CreateStack()
{
Stack S;
S = (Stack)malloc(sizeof(struct Node));
if (S == NULL)
{
FatalError("Out of memory!");
}
S->Next = NULL;
MakeEmpty(S);
return S;
}
void MakeEmpty(Stack S)
{
if (S == NULL)
{
FatalError("Must use CreateStack first");
}
else
{
while (!IsEmpty(S))
{
Pop(S);
}
}
}
void DisposeStack(Stack S)
{
MakeEmpty(S);
free(S);
}
/*
* The Push is implemented as an insertion into the front of a linked list,
* where the front of the list serves as the top of the stack.
*/
void Push(ElementType X, Stack S)
{
PtrToNode TmpCell;
TmpCell = (PtrToNode)malloc(sizeof(struct Node));
if (TmpCell == NULL)
{
FatalError("Out of memory!");
}
else
{
TmpCell->Element = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
/*
* Retrieve the element in the first position of the list.
*/
ElementType Top(Stack S)
{
if (!IsEmpty(S))
{
return S->Next->Element;
}
else
{
Error("Empty stack");
return -1; /* Return value used to avoid warning */
}
}
/*
* Delete a cell from the front of the list.
*/
void Pop(Stack S)
{
PtrToNode FirstCell;
if (IsEmpty(S))
{
FatalError("Empty stack");
}
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free(FirstCell);
}
}
#else /* #ifndef _STACK_ARRAY_ */
/* --------------------Array Implementation of Stacks------------------- */
#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
/*
* Test whether stack is empty.
*/
int IsEmpty(Stack S)
{
return S->TopOfStack == EmptyTOS;
}
/*
* Test whether stack is full.
*/
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity - 1;
}
/*
* Create stack.
*/
Stack CreateStack(int MaxElements)
{
Stack S;
if (MaxElements < MinStackSize)
{
FatalError("Stack size is too small!");
}
S = (Stack)malloc(sizeof(struct StackRecord));
if (S == NULL)
{
FatalError("Out of memory!");
}
S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements);
if (S->Array == NULL)
{
FatalError("Out of memory!");
}
S->Capacity = MaxElements;
MakeEmpty(S);
return S;
}
/*
* Make stack empty.
*/
void MakeEmpty(Stack S)
{
S->TopOfStack = EmptyTOS;
}
/*
* Free the stack structure.
*/
void DisposeStack(Stack S)
{
if (S != NULL)
{
free(S->Array);
free(S);
}
}
/*
* A Push on a full stack will overflow the array bounds and cause a crash.
*/
void Push(ElementType X, Stack S)
{
if (IsFull(S))
{
FatalError("Full stack!");
}
else
{
S->Array[++S->TopOfStack] = X;
}
}
/*
* Return top of stack.
*/
ElementType Top(Stack S)
{
if (IsEmpty(S))
{
FatalError("Empty stack!");
return 0; /* Return value used to avoid warning */
}
else
{
return S->Array[S->TopOfStack];
}
}
/*
* A Pop on an empty stack will underflow the array bounds and cause a crash.
*/
void Pop(Stack S)
{
if (IsEmpty(S))
{
FatalError("Empty stack!");
}
else
{
--S->TopOfStack;
}
}
ElementType TopAndPop(Stack S)
{
if (IsEmpty(S))
{
FatalError("Empty stack!");
return 0; /* Return value used to avoid warning */
}
else
{
return S->Array[S->TopOfStack--];
}
}
#endif /* #ifndef _STACK_ARRAY_ */
/*
* teststack.cpp
*/
#include
#include "stack.h"
int main()
{
Stack S = NULL;
int i;
#ifndef _STACK_ARRAY_
S = CreateStack();
#else
S = CreateStack(12);
#endif
for (i = 0; i < 10; ++i)
{
Push(i, S);
}
while (!IsEmpty(S))
{
printf("%d\n", Top(S));
Pop(S);
}
DisposeStack(S);
return 0;
}