#ifndef __STACK_H
#define __STACK_H
/*array stack*/
namespace dskit
{
struct StackRecord;
typedef struct StackRecord* Stack;
#ifndef NULL
#define NULL (0)
#endif
typedef char ElementType;
typedef unsigned size_t;
bool IsEmpty(Stack s);
bool IsFull(Stack s);
Stack CreateStack(size_t MaxElements);
void DisposeStack(Stack s);
bool Push(ElementType x, Stack s);
ElementType Pop(Stack s);
ElementType Top(Stack s);
#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType* Array;
};
}
#endif
#include "stack.h"
#include
#include
bool dskit::IsEmpty(Stack s)
{
return s->TopOfStack == EmptyTOS;
}
bool dskit::IsFull(Stack s)
{
return s->TopOfStack == s->Capacity;
}
dskit::Stack dskit::CreateStack(size_t MaxElements)
{
if(MaxElements < MinStackSize)
MaxElements = MinStackSize;
Stack S;
S = (struct StackRecord*)malloc(sizeof(struct StackRecord));
if(NULL == S)
return NULL;
S->Array = (ElementType*) malloc(sizeof(ElementType) * MaxElements );
if(NULL == S->Array)
{
free(S);
return NULL;
}
S->Capacity = MaxElements;
S->TopOfStack = EmptyTOS;
return S;
}
void dskit::DisposeStack(Stack s)
{
if(NULL != s)
{
free(s->Array);
free(s);
}
}
bool dskit::Push(ElementType x, Stack s)
{
if(IsFull(s))
return false;
else
{
printf("%c was pushed./n", x);
return s->Array[++s->TopOfStack] = x;
}
}
dskit::ElementType dskit::Pop(Stack s)
{
if(!IsEmpty(s))
return s->Array[s->TopOfStack--];
else
{
printf("Pop error/n");
exit(-1);
}
}
dskit::ElementType dskit::Top(Stack s)
{
if(!IsEmpty(s))
return s->Array[s->TopOfStack];
else
{
printf("Top error/n");
exit(-1);
}
}
/*
int main(int argc, char* argv[])
{
dskit::Stack s = dskit::CreateStack(50);
dskit::Push(12, s);
dskit::Push(13, s);
dskit::Push(14, s);
std::cout << dskit::Pop(s) << '/t' << std::endl;
std::cout << dskit::Pop(s) << '/t' << std::endl;
std::cout << dskit::Pop(s) << '/t' << std::endl;
dskit::DisposeStack(s);
}
*/