栈
栈是一种后进先出的数据结构,也是在程序中用的较多的一种方法,在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; }
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。