棧ADT

紅心棧的鏈表實現(缺點:對malloc和free的調用的開銷昂貴)

燈泡棧ADT鏈表實現的類型聲明數組

#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;
};

燈泡測試棧是否爲空棧測試

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

燈泡建立一個空棧spa

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 );
}

燈泡Push進棧指針

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;
    }
}
燈泡返回棧頂元素
ElementType
Top( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Next->Element;
    Error( "Empty stack" );
    return 0;    /* Return value used to avoid warning */
}

燈泡Pop出棧code

void
Pop( Stack S )
{
    PtrToNode    FirstCell;
    
    if( IsEmpty( S ) )
        Error( "Empty stack" );
    else
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        free( FirstCell );
    }
}

 

紅心棧的數組實現(避免使用指針而且多是更流行的解決方案,這種策略的惟一潛在問題是咱們須要提早聲明一個數組的大小。不過,通常來講,這並非一個問題,由於在典型的應用程序中,即便有至關多的棧操做,在任一時刻棧元素的實際個數從不會太大。聲明一個數組足夠大而不至於浪費太多的空間一般並無什麼困難。若是不能作到這一點,那麼節省的作法是使用鏈表來實現。)

燈泡棧的數組實現的類型聲明blog

#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;
};

燈泡棧的建立ci

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;
}

燈泡釋放棧it

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

燈泡檢測一個棧是否爲空io

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

燈泡建立一個空棧class

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;
}

燈泡返回棧頂元素

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

燈泡從棧彈出元素

void
Pop( Stack S )
{
    if( IsEmpty( S ) )
        Error( "Empty stack" );
    esle
        S->TopOfStack--;
}

燈泡返回棧頂元素並將其從棧彈出

ElementType
TopAndPop( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Array[ S->TopOfStack-- ];
    Error( "Empty satck" );
    return 0;    /* return value used to avoid warning */
}
相關文章
相關標籤/搜索