指针实现:

操作运行时间均为[latex]O(1)[/latex]

缺点是对malloc和free的调用昂贵

#include <cstdio>
#include <malloc.h>
#include <cstdlib>

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);
ElementType Pop(Stack S);

struct Node
{
    ElementType Element;
    PtrToNode Next;
};

int IsEmpty(Stack S)
{
    return S->Next == NULL;
} 

Stack CreateStack(void)
{
    Stack S = Stack(malloc(sizeof(struct Node)));
    if(S==NULL)
    {
        printf("Out of space
");
    }
    S->Next = NULL;
    MakeEmpty(S);
    return S;
}

void MakeEmpty(Stack S)
{
    if(S==NULL)
    {
        printf("Must use CreateStack first
");
    }
    while(!IsEmpty(S))
    {
        Pop(S);
    }
}

void Push(ElementType X,Stack S)
{
    PtrToNode TmpCell = PtrToNode(malloc(sizeof(struct Node)));
    if(TmpCell == NULL)
    {
        printf("Out of space
");
    }
    TmpCell->Element = X;
    TmpCell->Next = S->Next;
    S->Next = TmpCell;
}

ElementType Top(Stack S)
{
    if(!IsEmpty(S))
    {
        return S->Next->Element; 
    }
}

ElementType Pop(Stack S)
{
    ElementType E;
    PtrToNode FirstCell;
    if(!IsEmpty(S))
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        E = FirstCell -> Element;
        free(FirstCell);
        return E;
    }
}

int main()
{
    Stack S = CreateStack();
    for(int i = 1;i<5;++i)
    {
        Push(i,S);
    }
    while(!IsEmpty(S))
    {
        printf("%d
",Pop(S));
    }
    return 0;
}

数组实现:

#include <cstdio>
#include <malloc.h>
#include <cstdlib>

struct StackRecord;
typedef int ElementType;
typedef struct StackRecord *Stack;

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);
 
#define EmptyTOS -1
#define MinStackSize 5

struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};


int IsEmpty(Stack S)
{
    return S->TopOfStack == -1;    
}

void MakeEmpty(Stack S)
{
    S->TopOfStack = -1;
}

int IsFull(Stack S)
{
    return S->TopOfStack == (S->Capacity-1);
}

Stack CreateStack (int MaxElements)
{
    Stack S;
    if(MaxElements < MinStackSize) { printf("Stack Size is too small
"); } S = Stack(malloc(sizeof(struct StackRecord))); if(S==NULL) { printf("Out of Space
"); } S->Array = (ElementType*)malloc(sizeof(ElementType)*MaxElements);
    if(S->Array == NULL)
    {
        printf("Out of Space
");
    }
    S->Capacity = MaxElements;
    MakeEmpty(S);
    return S;
}

void DisposeStack(Stack S)
{
    if(S!=NULL)
    {
        free(S->Array);
        free(S);
    }
}

void Push(ElementType X,Stack S)
{
        if(!IsFull(S))
        {
            S->Array[++(S->TopOfStack)] = X;
        }
} 

ElementType Top(Stack S)
{
    if(!IsEmpty(S))
    {
        return S->Array[S->TopOfStack];
    }
}

void Pop(Stack S)
{
    if(!IsEmpty(S))
    {
        S->TopOfStack--;
    }
}

ElementType TopAndPop(Stack S)
{
    if(!IsEmpty(S))
    {
        ElementType X = S->Array[(S->TopOfStack)--];
    }
}

int main()
{
    Stack S = CreateStack(7);
    for(int i=0;i<7;++i)
    {
        Push(i,S); 
    }
    while(!IsEmpty(S))
    {
        printf("%d
",TopAndPop(S));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kachunyippp/p/10256254.html