棧是一種后進先出的數(shù)據(jù)結(jié)構(gòu),也是在程序中用的較多的一種方法,在C語言函數(shù)參數(shù)傳遞的入棧過程就是一種棧的數(shù)據(jù)結(jié)構(gòu),做個比喻就是×××的彈夾,壓入×××,后壓入彈夾的×××,先被射擊出槍膛。
創(chuàng)新互聯(lián)是專業(yè)的武山網(wǎng)站建設(shè)公司,武山接單;提供網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行武山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
頭文件:
/*****************************************************************************************************
*Copyright:Yue Workstation
*
*FileName:Stack.h
*
*Function:棧相關(guān)數(shù)據(jù)定義和函數(shù)聲明
*
*Author:Abel Lee
*
*CreateOn:2011-5-3
*
*Log:2011-5-3 由Abel Lee創(chuàng)建
*****************************************************************************************************/
#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創(chuàng)建
*****************************************************************************************************/
#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;
}
分享文章:棧
分享網(wǎng)址:http://muchs.cn/article14/ihcege.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站策劃、定制開發(fā)、網(wǎng)站營銷、靜態(tài)網(wǎng)站、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)