顺序栈操作(C语言代码)

简介: 顺序栈操作(C语言代码)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define Maxsize 100
using namespace std;
typedef struct SqStack{
    int data[Maxsize];
    int top;
}SqStack;
/*
    栈顶指针:S.top,初始时设置S.top=-1,;栈顶元素:S.data[S.top]。
    进栈操作:栈不满时,栈顶指针先加1,再送值到栈顶元素。
    出栈操作:栈非空时,先取栈顶元素值,再将栈顶指针减一。
    栈空条件:S.top==-1;栈满条件:S.top==Maxsize-1;栈长:S.top+1
*/
//初始化
int Init_Sqstack(SqStack &S)
{
    S.top=-1;
}
//进栈
bool push_SqStack(SqStack &S,int x)
{
    if(S.top==Maxsize-1)
        return false;//栈满
    S.data[++S.top]=x;
    return true;
}
//出栈
bool pop_SqStack(SqStack &S,int &x)
{
    if(S.top==-1)
        return false;
    x=S.data[S.top];
    S.top--;
    return true;
}
//获取栈顶元素
bool GetTop(SqStack S,int &x)
{
    if(S.top==-1)
        return false;
    x=S.data[S.top];
    return true;
}
void show_SqStack(SqStack S)
{
    for(int i=0;i<=S.top;i++)
    {
        printf("%d ",S.data[i]);
    }
}
int main()
{
    SqStack S;
    Init_Sqstack(S);
    push_SqStack(S,1);
    push_SqStack(S,2);
    push_SqStack(S,3);
    show_SqStack(S);
    int a=0;
    pop_SqStack(S,a);
    printf("%d \n",a);
    show_SqStack(S);
    GetTop(S,a);
    printf("%d \n",a);
}
目录
相关文章
|
8天前
|
C语言
链栈的初始化以及用C语言表示进栈、出栈和判断栈空
链栈的初始化以及用C语言表示进栈、出栈和判断栈空
18 3
|
1天前
|
存储 C语言
c语言:文件处理操作-3
c语言:文件处理操作
12 0
|
1天前
|
C语言 C++
c语言:文件处理操作-2
c语言:文件处理操作
8 0
|
1天前
|
存储 编译器 C语言
c语言:文件处理操作-1
c语言:文件处理操作
10 0
|
2天前
|
算法 C语言 容器
纯c语言模拟栈和队列(初学必看)
纯c语言模拟栈和队列(初学必看)
|
3天前
|
C语言 C++
C语言进阶⑭(内存函数_以字节操作)momcpy+mommove+memcmp+memset
C语言进阶⑭(内存函数_以字节操作)momcpy+mommove+memcmp+memset
7 0
|
3天前
|
程序员 编译器 测试技术
C语言初阶⑨(调试)(如何写出好的代码)(模拟实现strcpy和strlen)
C语言初阶⑨(调试)(如何写出好的代码)(模拟实现strcpy和strlen)
11 1
|
7天前
|
C语言
C语言扫雷代码(蹦蹦炸弹)(下)
C语言扫雷代码(蹦蹦炸弹)(下)
6 0
|
8天前
|
存储 C语言
C语言进阶第十课 --------文件的操作-2
C语言进阶第十课 --------文件的操作
|
8天前
|
存储 编译器 C语言
C语言进阶第十课 --------文件的操作-1
C语言进阶第十课 --------文件的操作