A simple stack

// simple stack.cpp : 定義控制檯應用程序的入口點。
//ios

#include "stdafx.h"
#include<iostream>
using namespace std;
const int SIZE = 100;
class Stack
{
    public:
        void init()
        {
            position = 0;
        }
        char push(char ch);
        char pop();
private:
    char stk[SIZE];
    int position;
};spa


char Stack::push(char ch)
{
    if (position == SIZE)
    {
        cout << endl << "stack is full" << endl;
        return 0;
    }
    stk[position++] = ch;
    return ch;
}blog

char Stack::pop()
{
    if (position ==0)
    {
        cout << endl << "stack is null" << endl;
        return 0;
    }
   
    return stk[--position] ;
}
int main()
{
    Stack s;
    s.init();
    char ch;
    cin >> ch;
    while (ch != '!'&&s.push(ch))
        cin >> ch;
    cout << "data in stack:";
    while (ch = s.pop())
        cout << ch;
    system("pause");
    return 0;
}ci

圖像 6

相關文章
相關標籤/搜索