栈的顺序存储结构及其基本运算实现

来源:互联网 发布:java全局map 编辑:程序博客网 时间:2024/05/16 02:58

 栈的顺栈栈的顺序存储结构及其基本运算实现的顺序存储结构及其基本运算实现序存储结构及其基本运算实现

 

栈是一种只能在一端进行插入和删除操作的线性表,主要特点:后进先出。

1.初始化栈InitStack(SqStack *&s)

void InitStack(SqStack *&s)

{

  s=(SqStack *)malloc(sizeof(SqStack));

  s->top=-1;//栈顶指针置为-1     

}

2.销毁栈 DestroyStack(s)

void DestroyStack(SqStack *&s)

{

  free(s);     

3.判断栈是否为空StackEmpty(s)

bool StackEmpty(SqStack *s)

{

  return (s->top==-1);     

}

4.进栈Push(s,e)

bool Push(SqStack *&s,ElemType &e)

{

  if(s->top==MaxSize-1)

    return false;

  s->top++;

  s->data[s->top]=e;

  return true;     

}

5.出栈 Pop(s,e)

bool Pop(SqStack *&s,ElemType &e)

{

  if(s->top==-1)

    return false;

  e=s->data[s->top];  

  s->top--;

  return true;          

6.取出栈顶元素GetTop(s,e)

bool GetTop(SqStack *s,ElemType &e)

{

  if(s->top==-1)

    return false;

  e=s->data[s->top];

  return true;     

 

 

 

 

0 0
原创粉丝点击