C語言實現棧的進棧與出棧、輸出棧頂元素、元素個數、銷燬棧

 /********************************引入頭文件**************************************************/ 
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

/**********************************定義*******************************************************/ 
/*定義狀態碼*/  
#define OK 0            //正常 
#define ERROR -1        //出錯 
#define OVERFLOW -2        //內存申請不成功    
#define DEFSIZE 10      //棧的默認大小 
#define INCREAMSIZE 10     //每次當棧空間滿時,增量 
/* 定義結構體 */
typedef int Status;     //定義狀態     
typedef int ElemType;      //定義棧內元素類型

//定義棧的的數據結構 
typedef struct{
    ElemType *base;     //棧底指針 
    ElemType *top;        //棧頂指針 
    int stackSize;        // 棧大小 
    int realSize;        // 棧當前大小,能夠不定義 
}SqStack; 

/*********************************stack操做方法**************************************************/

//初始化一個棧
Status InitStack(SqStack &sqstack)
{
    //申請默認棧大小 
    sqstack.base = (ElemType*)malloc(DEFSIZE*sizeof(ElemType)); 
    if(!sqstack.base) exit(OVERFLOW);
    
    sqstack.top  = sqstack.base;
    sqstack.stackSize = DEFSIZE;
    sqstack.realSize = 0; 
    return OK;
}

//進棧
Status Push(SqStack &sqstack,ElemType &e)
{
    if(sqstack.top-sqstack.base>=sqstack.stackSize)
	{
        sqstack.base = (ElemType*)realloc(sqstack.base,(sqstack.stackSize+INCREAMSIZE)*sizeof(ElemType)); 
        //若是申請失敗返回溢出 
        if(!sqstack.base) exit(OVERFLOW);
        sqstack.top = sqstack.base + sqstack.stackSize;
        sqstack.stackSize = sqstack.stackSize + INCREAMSIZE;
    }
    *sqstack.top++ = e;
    sqstack.realSize++;
    return OK;
}

//出棧
Status Pop(SqStack &sqstack,ElemType &e)
{
    if(sqstack.base==sqstack.top)
	{
        exit(ERROR);
    }
    e = *--sqstack.top;
    sqstack.realSize--;
    return OK;
}

//獲得棧頂元素 
Status GetTop(SqStack &sqstack,ElemType &e)
{
    if(sqstack.base==sqstack.top)
	{
        exit(ERROR);
    }
    e = *(sqstack.top-1);
    return OK;
}

//判斷棧是否爲空
bool IsEmpty(SqStack &sqstack)
{
    if(sqstack.realSize>0)
        return false;
    else
        return true;
} 

//銷燬棧 
Status DestroyStack(SqStack &sqstack)
{
    sqstack.top = sqstack.base;
    free(sqstack.base);
    sqstack.realSize = 0;
    sqstack.stackSize = DEFSIZE; 
    return OK;
} 
 
//獲得棧的元素個數 
int StackLength(SqStack &sqstack)
{
    return sqstack.realSize;
} 

/*******************************主函數************************************************/
int main(int argc, char *argv[])
{ 
    
    SqStack sqstack;
    int N = 0;         //用於記錄輸入棧的個數 
    int temp = 0;    //用於臨時存棧 
    
    /****初始化棧***/
    InitStack(sqstack);
    printf("初始化時,堆的大小爲:%d\n",sqstack.stackSize);
    
    /**根據輸入來填充棧**/
    printf("請入你想要輸入幾個數據進棧:");
    scanf("%d",&N) ;
    while(N--)
	{
        scanf("%d",&temp);
        Push(sqstack,temp);
        printf("進棧的大小爲:%d\t",temp);
        printf("壓棧後,棧的大小爲:%d,%d\n",temp,sqstack.stackSize);
        
    } 
    /**獲得棧頂元素**/
    GetTop(sqstack,temp);
    printf("獲得棧頂元素爲:%d",temp); 
    

    /**將棧的元素逐一出棧**/
/*    printf("如今開始逐一出棧:\n");
    while(!IsEmpty(sqstack)){
    //若是棧不爲空則進行出棧 
        Pop(sqstack,temp);
        printf("%d \t",temp);
    } 
    printf("\n棧輸出完成!!\n");
*/
    
    DestroyStack(sqstack);
    printf("銷燬棧完成!!\n");
    scanf("%d",&temp);

    return 0;
}
相關文章
相關標籤/搜索