循序栈的设计

该代码示例展示了如何在C语言中使用结构体定义顺序栈,并实现初始化、进栈(Push)、出栈(Pop)、获取栈顶元素(GetTop)以及打印栈内容的功能。在主函数中,程序允许用户输入数据创建栈、插入元素、弹出元素及查看栈顶元素。

顺序栈

1. 表头文件

# include<stdio.h>

2. 代码内容

typedef int ElemType;

2.1 定义顺序栈

typedef struct 
{
    ElemType data[MaxSize];
    int top;//栈顶指针,记录了数组的下标
}SqStack;

2.2 初始化

void InitStack(SqStack &S)
{
    S.top = -1;
}

2.3 进栈

bool Push(SqStack &S, ElemType e)
{
    if(S.top == MaxSize-1)   //栈满的情况
    {
        return false;
    }
    S.top = S.top+1;
    S.data[S.top] = e;
    //S.data[++s.top] = e;
    return true;
}

2.4 出栈

bool Pop(SqStack &S, ElemType &e)
{
    if(S.top == MaxSize-1)
    {
        return false;
    }
    S.data[S.top] = e;
    S.top = S.top-1;
    //S.data[S.top--] = e;
    return true;
}

2.5 获取栈顶元素

int GetTop(SqStack S)
{
    return S.data[S.top];
}

2.6 打印顺序栈

void PrintStack(SqStack S)
{
    int i = S.top+1;
    if(S.top != -1)// 判空
    {    
        while(i--)// 遍历顺序栈
        {     
            printf("%d ", S.data[S.top--]);// 从上到下依次遍历
        }
    printf("\n");
    }
    else
    {
        printf("SqStack is empty!\n");
    }
}

2.7 主函数

int main()
{
    SqStack S;
    InitStack(S);
    ElemType e;
    
    
    //创建栈
    int len;
    printf("Please enter the length of data you want to insert!(Length < %d)\n", MaxSize);
    scanf("%d",&len);
    printf("Please enter %d elements one by one in order!\n", len);
    while (len--)
    {
        scanf("%d", &e);
        Push(S, e);
    }
    PrintStack(S);
    
    
    //入栈
    printf("please input the element you want to input to the stack\n");
    scanf("%d",&e);
    Push(S,e);
    printf("You have done it!\n");
    PrintStack(S);
    
    
    //出栈
    printf("please input the element you want to out \n");
    Pop(S, e);
    printf("the %d have be POPPED!\n",e);
    PrintStack(S);


    //获取栈顶元素
    printf("the top element is %d",GetTop(S));
    
    return 0;
}

2.完整代码

//顺序栈
#include<stdio.h>

#define MaxSize 10
typedef int ElemType;

typedef struct 
{
    ElemType data[MaxSize];
    int top;//栈顶指针,记录了数组的下标
}SqStack;

//初始化
void InitStack(SqStack &S)
{
    S.top = -1;
}


//进栈
bool Push(SqStack &S, ElemType e)
{
    if(S.top == MaxSize-1)   //栈满的情况
    {
        return false;
    }
    S.top = S.top+1;
    S.data[S.top] = e;
    //S.data[++s.top] = e;
    return true;
}

//出栈
bool Pop(SqStack &S, ElemType &e)
{
    if(S.top == MaxSize-1)
    {
        return false;
    }
    S.data[S.top] = e;
    S.top = S.top-1;
    //S.data[S.top--] = e;
    return true;
}

//获取栈顶元素
int GetTop(SqStack S)
{
    return S.data[S.top];
}


//输出
void PrintStack(SqStack S)
{
    int i = S.top+1;
    if(S.top != -1)// 判空
    {    
        while(i--)// 遍历顺序栈
        {     
            printf("%d ", S.data[S.top--]); // 从上到下依次遍历
        }
    printf("\n");
    }
    else
    {
        printf("SqStack is empty!\n");
    }
}




int main()
{
    SqStack S;
    InitStack(S);
    ElemType e;
    
    
    //创建栈
    int len;
    printf("Please enter the length of data you want to insert!(Length < %d)\n", MaxSize);
    scanf("%d",&len);
    printf("Please enter %d elements one by one in order!\n", len);
    while (len--)
    {
        scanf("%d", &e);
        Push(S, e);
    }
    PrintStack(S);
    
    
    //入栈
    printf("please input the element you want to input to the stack\n");
    scanf("%d",&e);
    Push(S,e);
    printf("You have done it!\n");
    PrintStack(S);
    
    
    //出栈
    printf("please input the element you want to out \n");
    Pop(S, e);
    printf("the %d have be POPPED!\n",e);
    PrintStack(S);


    //获取栈顶元素
    printf("the top element is %d",GetTop(S));
    
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值