栈的数组和链表实现

5 篇文章 0 订阅
1 篇文章 0 订阅

栈的数组实现:

stack.h


#ifndef _Stack_h
#define ElementType int
struct StackRecord;
typedef struct StackRecord *Stack;

int IsEmpty( Stack S );
int IsFull( Stack S );
Stack CreatStack( int MaxElements );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push(ElementType X, Stack S);
ElementType Top( Stack S );
void Pos( Stack S );
ElementType TopAndPos( Stack S );
#endif

stack.c

#include "stack.h"
#include <stdio.h>
#include <malloc.h>

#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};

Stack
CreatStack( int MaxElements )
{
    Stack S;
    
    if( MaxElements < MinStackSize )
        return NULL;
    S = (Stack)malloc( sizeof(struct StackRecord ) );
    if( S == NULL )
        return NULL;

    S->Array = (ElementType *)malloc( sizeof(ElementType) * MaxElements );
    if( S->Array == NULL )
        return NULL;
    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;
}

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

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

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

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

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

ElementType 
TopAndPop( Stack S )
{
    if( IsEmpty( S ) )
        return -1;
    else
    {
        ElementType Tmp = S->Array[S->TopOfStack];
        S->TopOfStack--;
        return Tmp;
    }
}

int 
main()
{
    Stack S;
    S = CreatStack(10);
    printf("S is empty : %d", IsEmpty( S ));
    printf("\n");

    int i;
    for(i = 0; i < 6; i++)
        Push(i, S);
    while( !IsEmpty( S ) )
    {
        printf( " %d ", TopAndPop( S ) );
    }
    printf("\n");
    return 0;

}

栈的链表实现

stack.h

#ifndef _Stack_H
#define ElementType int
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty( Stack S );
Stack CreatStack( void );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push( ElementType x, Stack S );
ElementType Top( Stack S );
void Pop( Stack S );

#endif

stack.c

#include "stack.h"
#include <stdio.h>
#include <malloc.h>

struct Node
{
    ElementType Element;
    PtrToNode Next;
};

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

Stack
CreatStack( void )
{
    Stack S;
    S = malloc( sizeof(struct Node) );
    if( S == NULL )
        return ;
    S->Next = NULL;
    MakeEmpty( S );
    return S;
}

void
MakeEmpty( Stack S )
{
    if( S == NULL )
        return ;
    else
        while( !IsEmpty(S) )
            Pop( S );
}

void 
Push( ElementType X, Stack S )
{
    PtrToNode TmpCell;

    TmpCell = malloc( sizeof(struct Node) );
    if( TmpCell == NULL )
        return ;
    else
    {
        TmpCell->Element = X;
        TmpCell->Next = S->Next;
        S->Next = TmpCell;
    }
}

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

void 
Pop( Stack S )
{
    PtrToNode FirstCell;

    if( IsEmpty( S ) )
        return ;
    else
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        free(FirstCell);
    }
}

int main()
{
    Stack S;
    S = CreatStack();

    int i;
    for( i = 1; i < 6; i++ )
        Push(i, S);

    for( i = 0; i < 5; i++)
    {
        printf("%d\n", Top(S));
        Pop(S);
    }
    return 0;
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值