标签:ima 特性 分享 init malloc out 行操作 alt stdio.h
栈(stack):
栈是一种线性结构,是运算受限的线性表。其主要特性是后入先出(Last In First Out,LIFO),并且
只能够在栈的顶端进行操作(插入和删除),这个端称之为栈顶。
C语言的实现:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20 //初始化栈的空间
#define STACKINCREMENT 10 //栈满后每次添加的空间大小
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}sqStack;
//初始化栈
void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
exit(0);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
}
//入栈操作
void 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)
exit(0);
}
*(s->top) = e;
s->top++;
}
//出栈操作
void Pop(sqStack *s,ElemType *e)
{
if(s->top == s->base)
return ;
*e = *--s->top;
}
//返回栈中的元素个数
int StackLen(sqStack s)
{
return (s.top - s.base);
}
标签:ima 特性 分享 init malloc out 行操作 alt stdio.h
原文地址:https://www.cnblogs.com/ghost-98210/p/9483391.html