栈和列队

栈(Stack)是一种重要的线性结构,是后进先出(Last in first out,LIFO)的数据结构。它要求只在表尾进行删除和插入操作。

表尾称为栈的栈顶(top),相应的表头称为栈底(bottom)。

typedef struct
{
    ElemType *base;//栈底
    ElemType *top;
    int stacksize;//最大容量
}sqStack;
#define STACK_INIT_SIZE  100
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;
}

 

出栈操作
Pop(sqStack *s, ElemType *e)
{
    if( s->top == s->base )
         return;
    *e = *--(s->top)
}
posted @ 2019-12-22 19:22  星空下聆听  阅读(111)  评论(0编辑  收藏  举报