您的位置:首页 > 理论基础 > 数据结构算法

C源码@数据结构与算法->栈Stack

2015-08-11 16:14 661 查看
/*
* fatal.h
* Header file for print error message.
*/
#ifndef _FATAL_H
#define _FATAL_H

#include
#include

#define Error(str) FatalError(str)
#define FatalError(str) fprintf(stderr, "%s\n", str), exit(-1)

#endif /* _FATAL_H */

/*
* stack.h
*/
#ifndef _STACK_H
#define _STACK_H

#define _STACK_ARRAY_

#ifndef NULL
#define NULL (0)
#endif

typedef int ElementType;

#ifndef _STACK_ARRAY_
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
#else /* ifndef _STACK_ARRAY_ */
struct StackRecord;
typedef struct StackRecord *Stack;
#endif /* ifndef _STACK_ARRAY_ */

#ifdef __cplusplus
extern "C" {
#endif

int IsEmpty(Stack S);
#ifndef _STACK_ARRAY_
Stack CreateStack();
#else /* _STACK_ARRAY_ */
Stack CreateStack(int MaxElements);
int IsFull(Stack S);
ElementType TopAndPop(Stack S);
#endif /* _STACK_ARRAY_ */
void MakeEmpty(Stack S);
void DisposeStack(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

#ifdef __cplusplus
}
#endif

#endif /* _STACK_H */


/*
* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: