您好,登錄后才能下訂單哦!
棧是一種后進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),也是在程序中用的較多的一種方法,在C語言函數(shù)參數(shù)傳遞的入棧過程就是一種棧的數(shù)據(jù)結(jié)構(gòu),做個(gè)比喻就是×××的彈夾,壓入×××,后壓入彈夾的×××,先被射擊出槍膛。
頭文件:
/*****************************************************************************************************
*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:初始化一個(gè)棧
*
*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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。