棧是一種後進先出的數據結構,也是在程序中用的較多的一種方法,在C語言函數參數傳遞的入棧過程就是一種棧的數據結構,做個比喻就是×××的彈夾,壓入×××,後壓入彈夾的×××,先被射擊出槍膛。

頭文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:Stack.h 
 * 
 *Function:棧相關數據定義和函數聲明 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-5-3 
 * 
 *Log:2011-5-3 由Abel Lee創建 
 *****************************************************************************************************/ 

#ifndef STACK_H 
#define STACK_H 

#include "global.h" 

#define STACKINCREMENT 10 

typedef struct _stack 
{ 
    ElemType *base; 
    ElemType *top; 
    int stacksize; 
}SqStack; 

int InitStack(SqStack *S); 
int GetTop(SqStack *S,ElemType *e); 
int Push(SqStack *S,ElemType e); 
int Pop(SqStack *S,ElemType *e); 

#endif

源文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:Stack.c 
 * 
 *Function:棧全基本操作 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-5-3 
 * 
 *Log:2011-5-3 由Abel Lee創建 
 *****************************************************************************************************/ 

#include "../inc/Stack.h" 

/**************************************************************************************************** 
 *Function Name:InitStack 
 * 
 *Function:初始化一個棧 
 * 
 *Parameter:     S:棧的首部 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int InitStack(SqStack *S) 
{ 
    S->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); 

    if(S->base == NULL) 
    { 
        perror("Malloc error,InitStack error!\n"); 
        return -1; 
    } 

    S->top = S->base; 
    S->stacksize = STACK_INIT_SIZE; 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:GetTop 
 * 
 *Function:獲取棧頂元素 
 * 
 *Parameter:     S:棧的首部 
 *               e:保存獲取的元素 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int GetTop(SqStack *S,ElemType *e) 
{ 
    if(S->top == S->base) 
    { 
        printf("The Stack is NULL!\n"); 
        return -1; 
    } 
    *e = *(S->top - 1); 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:Push 
 * 
 *Function:入棧操作 
 * 
 *Parameter:     S:站的首部 
 * 
 *Return Value:線性表的長度 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int Push(SqStack *S,ElemType e) 
{ 
 if (S->top - S->base >= S->stacksize) 
 { 
        S->base = (ElemType *) realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(ElemType)); 
        if (S->base == NULL) 
        { 
            perror("realloc error!\n"); 
            return -1; 
        } 
        S->top = S->base + S->stacksize; 
        S->stacksize += STACKINCREMENT; 
    } 
    *S->top++ = e; 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:Pop 
 * 
 *Function:彈棧操作 
 * 
 *Parameter:     S:棧的首部 
 *               e:保存出棧元素的值 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int Pop(SqStack *S,ElemType *e) 
{ 
    if (S->top == S->base) 
    { 
        perror("The stack is NULL!\n"); 
        return -1; 
    } 

    S->top--; 
    *e = *(S->top); 

    return 0; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章