栈模型

#include <stdio.h>
struct stack
{
    char space[512];
    int top;
};
struct stack stack = { { 0 }, 0 };
int isempty(){
    return stack.top == 0;
}
int isfull(){
    return stack.top == 512;
}
void push(char val)
{
    stack.space[stack.top] = val;
    stack.top++;
}
char pop()
{
    stack.top--;
    return stack.space[stack.top];
}
int main()
{
    if (!isfull())
        push('a');
    if (!isfull())
        push('b');
    if (!isfull())
        push('c');
    while (!isempty())
        putchar(pop());
    return 0;
}

栈模型

 

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
class Stack
{
    public :
    void init();
    bool isEmpty();
    bool isFull();
    void push(int data);
    int pop();
private:
    int space[1024];
    int top;
};
void Stack::init()
{
    memset(space, 0, sizeof(space));
    top = 0;
}
bool Stack::isEmpty()
{
    return top == 0;
}
bool Stack::isFull()
{
    return top == 1024;
}
void Stack::push(int data)
{
    space[top++] = data;
}
int Stack::pop()
{
    return space[--top];
}
int main()
{
    Stack s;
    s.init();
    if (!s.isFull())
        s.push(10);
    if (!s.isFull())
        s.push(20);
    if (!s.isFull())
        s.push(30);
    if (!s.isFull())
        s.push(40);
    if (!s.isFull())
        s.push(50);
    while (!s.isEmpty())
        cout << s.pop() << endl;
    return 0;
}

栈模型

 

原文链接: https://www.cnblogs.com/yuguangyuan/p/5848641.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    栈模型

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/398198

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月11日 上午9:56
下一篇 2023年4月11日 上午9:56

相关推荐