首页 > 其他 > 详细

栈ADT

时间:2014-03-19 03:48:47      阅读:533      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣栈的链表实现(缺点:对malloc和free的调用的开销昂贵)

bubuko.com,布布扣栈ADT链表实现的类型声明

bubuko.com,布布扣
#ifndef _Stack_h

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int         IsEmpty( Stack S );
Stack       CreateStack( void );
void        DisposeStack( Stack S );
void        MakeEmpty( Stack S );
void        Push( ElementTyep X, Stack S );
ElementType Top( Stack S );
void        Pop( Stack S );

#endif    /* _Stack_h */


/* Place in implementation file */
/* Stack implementation is a linked list with a header */
struct Node
{
    ElementType    Element;
    PtrToNode      Next;
};
bubuko.com,布布扣

bubuko.com,布布扣测试栈是否为空栈

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

bubuko.com,布布扣创建一个空栈

bubuko.com,布布扣
Stack
CreateStack( void )
{
    Stack S;

    S = malloc( sizeof( struct Node ) );
    if( S == NULL )
    {
        FatalError( "Out of space!\n" );
    }
    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 );
}
bubuko.com,布布扣

bubuko.com,布布扣Push进栈

bubuko.com,布布扣
void
Push( ElementType X, Stack S )
{
    PtrToNode    TmpCell;
    
    TmpCell = malloc( sizeof( struct Node ) );
    if( TmpCell == NULL )
        FatalError( "Out of sapce !" );
    else
    {
        TmpCell->Element = X;
        TmpCell->Next = S->Next;
        S->Next = TmpCell;
    }
}
bubuko.com,布布扣
bubuko.com,布布扣返回栈顶元素
bubuko.com,布布扣
ElementType
Top( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Next->Element;
    Error( "Empty stack" );
    return 0;    /* Return value used to avoid warning */
}
bubuko.com,布布扣

bubuko.com,布布扣Pop出栈

bubuko.com,布布扣
void
Pop( Stack S )
{
    PtrToNode    FirstCell;
    
    if( IsEmpty( S ) )
        Error( "Empty stack" );
    else
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        free( FirstCell );
    }
}
bubuko.com,布布扣

 

bubuko.com,布布扣栈的数组实现(避免使用指针并且可能是更流行的解决方案,这种策略的唯一潜在问题是我们需要提前声明一个数组的大小。不过,一般来说,这并不是一个问题,因为在典型的应用程序中,即使有相当多的栈操作,在任一时刻栈元素的实际个数从不会太大。声明一个数组足够大而不至于浪费太多的空间通常并没有什么困难。如果不能做到这一点,那么节省的做法是使用链表来实现。)

bubuko.com,布布扣栈的数组实现的类型声明

bubuko.com,布布扣
#ifndef _Stack_h

struct StackRecord
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 );

#endif    /* _Stack_h */


/* place in implementation file */
/* Stack implementaion is a dynamically allocated array */
#define    EmptyTOS        ( -1 )
#define    MinStackSize    (  5 )

struct StackRecord
{
    int             Capacity;
    int             TopOfStack;
    ElementType    *Array;
};
bubuko.com,布布扣

bubuko.com,布布扣栈的创建

bubuko.com,布布扣
Stack
CreateStack( int MaxElements )
{
    Stack S;

    if( MaxElements < MinStackSize )
        Error( "Stack size is too small" );
    S = malloc( sizeof( struct StackRecord ) );
    if( S == NULL )
        FatalError( "Out of space !" );

    S->Array = malloc( sizeof( ElementType ) * MaxElements );
    if( S->Array == NULL )
        FatalError( "Out of space !" );
    S->Capacity = MaxElements;
    MakeEmpty( S );

    return S;
}
bubuko.com,布布扣

bubuko.com,布布扣释放栈

bubuko.com,布布扣
void
DisposeStack( Stack S )
{
    if( S != NULL )
    {
        free( S->Array );
        free( S );
    }
}
bubuko.com,布布扣

bubuko.com,布布扣检测一个栈是否为空

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

bubuko.com,布布扣创建一个空栈

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

bubuko.com,布布扣进栈

bubuko.com,布布扣
void
Push( ElementType X, Stack S )
{
    if( IsFull( S ) )
        Error( "Full Stack" );
    else
        S->Array[ ++S->TopOfStack ] = X;
}
bubuko.com,布布扣

bubuko.com,布布扣返回栈顶元素

bubuko.com,布布扣
ElementType
Top( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Array[ S->TopOfStack ];
    Error( "Empty stack" );
    return 0;    /* return value used to avoid warning */
}
bubuko.com,布布扣

bubuko.com,布布扣从栈弹出元素

bubuko.com,布布扣
void
Pop( Stack S )
{
    if( IsEmpty( S ) )
        Error( "Empty stack" );
    esle
        S->TopOfStack--;
}
bubuko.com,布布扣

bubuko.com,布布扣返回栈顶元素并将其从栈弹出

bubuko.com,布布扣
ElementType
TopAndPop( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Array[ S->TopOfStack-- ];
    Error( "Empty satck" );
    return 0;    /* return value used to avoid warning */
}
bubuko.com,布布扣

栈ADT,布布扣,bubuko.com

栈ADT

原文:http://www.cnblogs.com/nufangrensheng/p/3608736.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!