首页 > 其他 > 详细

线性栈

时间:2015-10-30 14:10:25      阅读:240      评论:0      收藏:0      [点我收藏+]

敲了下线性栈实现的代码,有必要保存起来

#include <stdio.h>
#define MAX_STACK_SIZE 100
typedef int ElemType;
typedef struct
{
    ElemType stack_array[MAX_STACK_SIZE];
    int top;
}SqStack;
SqStack Init_Stack()
{
    SqStack S;
    S.top = -1;
    return S;
}
bool Push(SqStack &S, ElemType e)
{
    if(S.top == MAX_STACK_SIZE-1)
    {
        printf("栈满无法进栈\n");
        return 0;
    }
    else
    {
        S.top++;
        S.stack_array[S.top] = e;
        //printf("S.stack_array[%d] = %d\n", S.top, S.stack_array[S.top]);
        return 1;
    }
}
bool Pop(SqStack &S, ElemType &e)
{
    if(S.top == -1)
    {
        printf("栈空, 无法出栈\n");
        return 0;
    }
    else
    {
        e = S.stack_array[S.top];
        S.top--;
        return 1;
    }
}
void Print(SqStack S)
{
    for(int i = 0; i <= S.top; i++)
    {
        if(i % 10 == 0 && i)
            printf("\n");
        printf("%d\t", S.stack_array[i]);
    }
}
int main()
{
    SqStack S;
    int e;
    S = Init_Stack();
    for(int i = 0; i < MAX_STACK_SIZE; i++)
        Push(S, i);
    Push(S, 2);
    Print(S);
    Pop(S, e);
    printf("%d出栈\n", e);
    Pop(S, e);
    printf("%d出栈\n", e);
    Print(S);
    return 0;
}

 

线性栈

原文:http://www.cnblogs.com/rain-1/p/4923028.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!