对数组进行压栈和出栈操作

打印 上一主题 下一主题

主题 885|帖子 885|积分 2655

#include <iostream>
#include "stdlib.h"
using namespace std;
class IntStack {
    public://1
        IntStack() {
        }
        IntStack(int s);
        void Push(int data);
        void Pop(int &data);
        ~IntStack();
        void Print();
    private:
//2
        int *stack;
        int pos;
        int size;
};
IntStack::IntStack(int s) {
//3
    size = s;
    stack = new int[size];
    pos = 0;
}
IntStack::~IntStack() {
//4
    delete[]stack;
}
void IntStack:ush(int data) {
//5
    if (pos < size) {
        stack[pos++] = data;
    } else {
        cout << "full" << endl;
        exit(0);
    }
}
void IntStack:op(int &data) {
//6
    if (pos > 0) {
        data = stack[pos--];
    } else {
        cout << "empty" << endl;
    }
}
void IntStack:rint() {
    int i;
    for (i = 0; i < pos; i++)
        cout << stack << " ";
}
int main() {
    IntStack is(5);
    int a[10] = {2, 5, 1, 7, 5, 4, 2, 7, 3, 6};
    int i = 0;
    int d;
    int x;
    cin >> x;
    while (x != -1) {
        if (x == 0) {
            is.Push(a);
            i++;
        } else
            is.Pop(d);
        cin >> x;
    }
    is.Print();
    return 0;
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表