Commit cb4a3019 authored by BRENO RILHO LEMOS's avatar BRENO RILHO LEMOS

Introduction to classes (2)

parent 6efb3459
#include <iostream>
using namespace std;
#define SIZE 10
class stack{
char stck[SIZE]; // holds the stack
int tos; //index of Top Of Stack
public:
void init(); // initialize stack
void push(char ch); // push character on stack
char pop(); // pop character from stack
void showtos(); // displays tos
};
void stack::init() {
tos = 0;
}
void stack::push(char ch) {
if(tos==SIZE) {
cout << "Stack is full";
return;
}
stck[tos] = ch;
tos++;
} // fazer com IF-ELSE ao inves de RETURN tambem funciona
char stack::pop() {
if (tos == 0) {
cout << "Stack is empty";
return 0; // returns null character
}
tos--;
return stck[tos];
}
void stack::showtos() {
cout << "Top of Stack: " << tos << "\n";
}
int main() {
stack s1, s2;
int i;
s1.init();
s2.init();
s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
s1.push('c');
s2.push('z');
for(i=0;i<3;i++) cout << "Pop s1: " << s1.pop() << "\n";
for(i=0;i<3;i++) cout << "Pop s2: " << s2.pop() << "\n";
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment