链表实现
fatal.h
#include <stdio.h>
#include <stdlib.h>
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
Stack_h.h
#ifndef _Stack_h
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;
int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
struct Node
{
ElementType Element;
PtrToNode Next;
};
#endif // _Stack_h
main.c
#include "fatal.h"
#include "Stack_h.h"
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
Stack CreateStack(void)
{
Stack S;
S = (Stack)malloc(sizeof(struct Node));
if(S == NULL)
FatalError("Out of space!!!");
S->Next = NULL;
MakeEmpty(S);
return S;
}
void MakeEmpty(Stack S)
{
if(S == NULL)
Error("Must use CreateStack first");
else
while(!IsEmpty(S))
Pop(S);
}
void Push(ElementType X, Stack S)
{
PtrToNode TmpCell;
TmpCell = (PtrToNode) malloc(sizeof(struct Node));
if(TmpCell == NULL)
FatalError("Out of space!!!");
else
{
TmpCell->Element = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
void Pop(Stack S)
{
PtrToNode FirstCell;
if(IsEmpty(S))
Error("Empty stack");
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free(FirstCell);
}
}
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Next->Element;
Error("Empty stack");
return 0;
}
void traverse(Stack S)
{
if(IsEmpty(S)) return ;
PtrToNode p = S->Next;
while(p != NULL)
{
printf("%d ", p->Element);
p = p->Next;
}
printf("\n");
}
#include<stdio.h>
int main()
{
Stack S;
S = CreateStack();
Push(1, S);
Push(2, S);
Push(3, S);
traverse(S);
printf("%d\n", Top(S));
Pop(S);
Pop(S);
traverse(S);
printf("%d\n", Top(S));
return 0;
}
数组实现
fatal.h
#include <stdio.h>
#include <stdlib.h>
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
Stack_h.h
#ifndef _Stack_h
struct StackRecord;
typedef struct StackRecord *Stack;
typedef int ElementType;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopAndPop(Stack S);
#endif // _Stack_h
#define EmptyTOS (-1) // 空栈
#define MinStackSize (5)
struct StackRecord
{
int Capacity; // 容量
int TopOfStack;
ElementType *Array;
};
main.c
#include "fatal.h"
#include "Stack_h.h"
Stack CreateStack(int MaxElements)
{
Stack S;
if(MaxElements < MinStackSize)
Error("Stack size is too small");
S = (Stack)malloc(sizeof(struct StackRecord));
if(S == NULL)
FatalError("Out of space!!!");
S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements);
if(S->Array == NULL)
FatalError("Out of space!!!");
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;
}
// 空栈
void MakeEmpty(Stack S)
{
S->TopOfStack = EmptyTOS;
}
void Push(ElementType X, Stack S)
{
if(IsFull(S))
Error("Full stack");
else
S->Array[++S->TopOfStack] = X;
}
// 栈是否满
int IsFull(Stack S)
{
return S->Capacity - 1 == S->TopOfStack;
}
// 将栈顶弹出
void Pop(Stack S)
{
if(IsEmpty(S))
Error("空栈");
else
S->TopOfStack --;
}
// 返回栈顶元素
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack];
Error("Empty stack");
return 0;
}
// 给出栈顶元素并从栈中弹出
ElementType TopAndPop(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack--];
Error("空栈");
return 0;
}
void traverse(Stack S)
{
if(IsEmpty(S))
Error("空栈");
for(int i = 0; i <= S->TopOfStack; i++)
printf("%d ", S->Array[i]);
printf("\n");
}
#include<stdio.h>
int main()
{
Stack S;
S = CreateStack(6);
Push(1, S);
Push(2, S);
Push(3, S);
traverse(S);
Pop(S);
traverse(S);
printf("%d\n", Top(S));
printf("%d\n", TopAndPop(S));
traverse(S);
return 0;
}
参考:《数据结构与算法分析——C语言描述》