C語言棧順序結構實現代碼_C 語言

來源:互聯網
上載者:User

複製代碼 代碼如下:

/**
* @brief C語言實現的順序結構類型的棧
* @author wid
* @date 2013-10-29
*
* @note 若代碼存在 bug 或程式缺陷, 請留言反饋, 謝謝!
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

typedef struct Point2D
{
    int x;
    int y;
}ElemType;      //棧元素結構

typedef struct
{
    ElemType *btm;      //棧底
    ElemType *top;      //棧頂
    int height;         //棧高
    int size;           //棧總大小
}ArrStack;      //棧結構

//棧方法聲明
ArrStack *CreateStack( int nSize );             ///建立一個大小為nSize的棧
void DestroyStack( ArrStack *pStack );          ///銷毀棧 pStack
void ClearStack( ArrStack *pStack );            ///清空棧 pStack 內的元素
int GetHeight( ArrStack *pStack );              ///擷取棧 pStack 的高度
int GetSize( ArrStack *pStack );                ///擷取棧 pStack 的總容量
int IsEmpty( ArrStack *pStack );                ///檢測棧 pStack 是否為空白棧
int Push( ArrStack *pStack, ElemType *pt );     ///將元素 pt 壓入棧 pStack
int Pop( ArrStack *pStack, ElemType *pt );      ///將棧頂元素出棧到 pt
int GetTop( ArrStack *pStack, ElemType *pt );   ///擷取棧頂元素到 pt
void ForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) );      ///從棧底到棧頂的每個元素依次執行 func 函數
void ReForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) );    ///從棧頂到棧底的每個元素依次執行 func 函數


//棧方法實現

/**
* @brief 建立一個大小為 nSize 的棧
*
* @param nSize 棧的初始大小
*
* @return 返回指向建立的棧的指標
*
* @note nSize 初始大小需大於0
*/
ArrStack *CreateStack( int nSize )
{
    //根據棧結構建立一個棧
    ArrStack *pStack = (ArrStack *)malloc( sizeof(ArrStack) );

    //申請棧初始空間
    pStack->btm = (ElemType *)calloc( nSize, sizeof(ElemType) );

    //令棧頂指向棧底元素
    pStack->top = &pStack->btm[0];

    //初始化棧高度為 0
    pStack->height = 0;

    //初始化棧大小為初始大小
    pStack->size = nSize;

    return pStack;
}

/**
* @brief 銷毀棧 pStack
*
* @param pStack 指向待銷毀的棧的指標
*
* @return void
*/
void DestroyStack( ArrStack *pStack )
{
    //釋放棧內元素
    free( pStack->btm );

    //釋放棧
    free( pStack );
}

/**
* @brief 清空棧內元素
*
* @param pStack 指向待清空元素的棧的指標
*
* @return void
*/
void ClearStack( ArrStack *pStack )
{
    //令棧頂指向棧底
    pStack->top = &pStack->btm[0];

    //將棧高度置為 0
    pStack->height = 0;
}

/**
* @brief 擷取棧 pStack 的高度
*
* @param pStack 指向待擷取高度的棧的指標
*
* @param 返回當前棧的高度
*/
int GetHeight( ArrStack *pStack )
{
    return pStack->height;
}

/**
* @brief 擷取棧 pStack 的總容量
*
* @param pStack 指向待擷取總容量的棧的指標
*
* @return 返回棧的當前總容量
*/
int GetSize( ArrStack *pStack )
{
    return pStack->size;
}

/**
* @brief 檢測棧 pStack 是否為空白棧
*
* @param pStack 指向待檢測的棧的指標
*
* @return 若棧為空白, 則返回 TRUE, 否則返回 FALSE
*/
int IsEmpty( ArrStack *pStack )
{
    return pStack->height == 0 ? TRUE : FALSE;
}

/**
* @brief 將元素 pt 壓入棧 pStack
*
* @param pStack 指向待壓入元素的棧的指標
* @param pt 指向待壓入元素的指標
*
* @return 返回成功壓入後棧的高度
*/
int Push( ArrStack *pStack, ElemType *pt )
{
    ///檢測是否需要擴容
    if( pStack->height == pStack->size )
    {   //需要擴容

        //重新申請於原棧大小2倍大小的棧空間
        ElemType *pe = (ElemType *)calloc( pStack->size * 2, sizeof(ElemType) );

        //將舊棧內容拷貝到新棧內容
        memcpy( pe, pStack->btm, pStack->size * sizeof(ElemType) );

        //重設棧總容量大小
        pStack->size = pStack->size * 2;

        //釋放舊棧空間
        free( pStack->btm );

        //將棧底指向新開闢的棧空間
        pStack->btm = pe;

        //棧頂指向新棧最後一個元素
        pStack->top = &pe[pStack->height-1];
    }

    //將新元素壓入棧
    pStack->btm[pStack->height].x = pt->x;
    pStack->btm[pStack->height].y = pt->y;

    //棧高度自增一
    ++pStack->height;

    //棧頂指向最新棧元素
    pStack->top = &pStack->btm[pStack->height-1];

    return pStack->height;
}

/**
* @brief 將棧頂元素出棧 到 pt
*
* @param pStack 指向待彈出元素的棧的指標
* @param pt 指向接收彈出的元素的指標
*
* @return 出棧成功則返回出棧後棧的高度, 否則返回 -1
*/
int Pop( ArrStack *pStack, ElemType *pt )
{
    ///是否為空白棧
    if( pStack->height == 0 )
        return -1;

    //將棧頂元素賦值到 pt
    pt->x = pStack->top->x;
    pt->y = pStack->top->y;

    //棧高度減一
    --pStack->height;

    //棧頂指向棧頂元素的上一個元素
    pStack->top = &pStack->btm[pStack->height-1];

    return pStack->height;
}

/**
* @brief 擷取棧頂元素到 pt
*
* @param pStack 指向待彈出元素的棧的指標
* @param pt 指向接收彈出的元素的指標
*
* @return 擷取成功則返回棧頂元素的位置, 否則返回 -1
*
* @note 元素位置由 0 計起
*/
int GetTop( ArrStack *pStack, ElemType *pt )
{
    pt->x = pStack->top->x;
    pt->y = pStack->top->y;

    return pStack->height;
}

/**
* @brief 從棧底到棧頂的每個元素依次執行 func 函數
*
* @param pStack 指向待處理的棧的指標
* @param func 需要執行的函數的指標
*
* @return void
*/
void ForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) )
{
    int i = 0;
    for( i = 0; i <  pStack->height; ++i )
    {
        func( &pStack->btm[i] );
    }
}

/**
* @brief 從棧頂到棧底的每個元素依次執行 func 函數
*
* @param pStack 指向待處理的棧的指標
* @param func 需要執行的函數的指標
*
* @return void
*/
void ReForEachStack( ArrStack *pStack, void (*func)(ElemType *pt) )
{
    int i = pStack->height - 1;
    for( i; i >= 0; --i )
    {
        func( &pStack->btm[i] );
    }
}

//測試

void display( ElemType *pt )
{
    printf( "(%d,%d) ", pt->x, pt->y );
}

int main()
{
    ///測試建立初始大小為 5 的棧
    ArrStack *psk = CreateStack( 5 );

    ///測試 IsEmpty、GetSize、GetHeight
    if( IsEmpty(psk) == TRUE )
        printf( "Stack Size=%d, Stack Height=%d\n", GetSize(psk), GetHeight(psk) );

    ElemType pt;

    int i = 0;
    ///測試Push, 向棧內壓入8個元素
    printf( "\n向棧內壓入8個元素後:\n" );
    for( i = 0; i < 8; ++i )
    {
        pt.x = pt.y = i;
        Push( psk, &pt );
    }
    //輸出壓入8個元素後的棧狀態
    printf( "Is empty = %d\n", IsEmpty(psk) );
    printf( "Stack size = %d\n", GetSize(psk) );
    printf( "Stack height = %d\n", GetHeight(psk) );

    ///測試 ForEachStack、ReForEachStack
    printf( "\n測試 ForEachStack、ReForEachStack:\n" );
    ForEachStack( psk, display );
    putchar('\n');
    ReForEachStack( psk, display );
    putchar('\n');

    ///測試getTop
    GetTop( psk, &pt );
    printf( "\n棧頂元素為: (%d,%d)\n", pt.x, pt.y );

    ///測試 Pop
    Pop( psk, &pt );
    printf( "\nPop彈出的元素為(%d,%d), 彈出後棧高:%d\n", pt.x, pt.y, GetHeight(psk) );
    Pop( psk, &pt );
    printf( "\nPop彈出的元素為(%d,%d), 彈出後棧高:%d\n", pt.x, pt.y, GetHeight(psk) );

    ///測試Push
    pt.x = pt.y = 100;
    Push( psk, &pt );
    printf( "\nPop壓入的元素為(%d,%d), 壓入後棧高:%d\n", pt.x, pt.y, GetHeight(psk) );

    ///執行全面出棧操作
    printf( "\n執行全面出棧:\n" );
    int n = GetHeight(psk);
    for( i = 0; i < n; ++i )
    {
        Pop( psk, &pt );
        printf( "Pop彈出的元素為(%d,%d), 彈出後棧高:%d\n", pt.x, pt.y, GetHeight(psk) );
    }

    ///銷毀棧
    DestroyStack( psk );

    return 0;
}

測試結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.