1. 程式人生 > >抽象資料型別——棧ADT(二)

抽象資料型別——棧ADT(二)

(一)棧的定義

是限制插入和刪除只能在一個位置上進行的表,該位置是表的末端,叫做棧的
對棧的基本操作有Push(進棧)和Pop(出棧),前者相當於插入,後者則是刪除最後插入的元素。最後插入的元素可以通過使用Top例程在執行Pop之前進行考查。
這裡寫圖片描述
棧的一般模型是,存在某個元素位於棧頂,而該元素是唯一可見的元素。

(二)棧的實現

1.棧的連結串列實現

在編寫例程之前,我們先給出定義:

 #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( ElementType X, Stack S ); ElementType Top( Stack S ); void Pop( Stack S ); #endif /*_Stack_h*/ struct Node { ElementType Element; PtrToNode Next; };
  • IsEmpty函式
int IsEmpty( Stack S )
{
    return S->Next
== NULL; }
  • CreateStack函式
Stack CreateStack( void )
{
    Stack S;

    S = 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 ); }
  • Push函式
void Push( ElementType X, Stack S )
{
    PtrToNode TmpCell;

    TmpCell = malloc( sizeof( struct Node ) );
    if( TmpCell == NULL )
        FatalError( "Out of Space!!!" );/*如果指標為空,就退出程式*/
    else
    {
        TmpCell->Element = X;
        TmpCell->Next = S->Next;
        S->Next = TmpCell;
    }
}
  • Top函式
ElementType Top( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Next->Element;
    Error( "Empty stack" );
    return 0;
}
  • Pop函式
void Pop( Stack S )
{
    PtrToNode FirstCell;

    if( IsEmpty( S ) )
        Error( "Empty stack" );
    eles
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        free( FirstCell );
    }
}

注意:棧的插入與刪除,即Push和Pop都是對棧頂(表頭)進行操作。
棧的連結串列實現的缺點是,對malloc和free的呼叫的開銷是昂貴的。

棧的陣列實現

在編寫具體的例程之前,先定義要編寫的函式:

 #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

#define EmptyTOS ( -1 )
#define MinStackSize ( 5 )

struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
}
  • CreateStack函式`
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;
}
  • DisposeStack函式
void DisposeStack( Stack S )
{
    if( S != NULL )
    {
        free( S->Array );
        free( S );
    }
}
  • IsEmpty函式
int IsEmpty( Stack S )
{
    return S->TopOfStack == EmptyTOS;
}
  • IsFull函式
int IsFull( Stack S )
{
    return S->TopOfStack == MaxElements-1;
}
  • MakeEmpty函式
void MakeEmpty( Stack S )
{
    S->TopOfStack = EmptyTOS;
}
  • Push函式
void Push( ElementType X, Stack S )
{
    if( IsFull( S ) )
        Error( "Full Stack " );
    else
        S->Array[ ++S->TopOfStack ] = X;
}
  • Top函式
ElementType Top( Stack S )
{
    if( !IsEmpty( S ) )
        return  S->Array[ S->TopOfStack ];
    Error( "Empty stack " );
    return 0;
}
  • Pop函式
void Pop( Stack S )
{
    if( IsEmpty( S ) )
        Error( "Empty stack" );
    else
        S->TopOfStack--;
}
  • TopAndPop函式
ElementType TopAndPop( Stack S )
{
    if( !IsEmpty( S ) )
        return S->Array[ S->TopOfStack-- ];
    Error( "Empty stack " );
    return 0;
}

棧的陣列實現唯一的危害是需要提前宣告一個數組的大小。