Простая реализация класса Stack на C++

#include "stdafx.h"
#include <iostream>
using namespace std;

const int SIZE=8;

class Stack{
private:
char mas_simvol[SIZE];
char simvol;
int index;

public:
Stack(char s);
void push(char s1);
char pop();
void clear();
char top();
bool empty();
void show();
};

Stack::Stack(char s){
index=0;
mas_simvol[index]=s;
simvol=s;
}

void Stack::push(char s1){
if(index==SIZE)
{
cout<<"Stack переполнен "<<simvol<<endl;
return;
}
mas_simvol[++index]=s1;
}

char Stack::pop(){
if(index==0){
cout<<"Stack пустой "<<simvol<<endl;
return 0;
}
return mas_simvol[index--];
}

void Stack::clear(){
while(index!=0){
index--;
}
}

char Stack::top(){
return mas_simvol[index];
}

bool Stack::empty(){
if(index==0)
{
return false;
}
else
{
return true;
}
}

void Stack::show(){
int temp=index;
if(temp==0){
cout<<"Stack пустой "<<simvol<<endl;
return;
}
while(temp!=0){
cout<<mas_simvol[temp]<<endl;
temp--;
}
cout<<mas_simvol[0]<<endl;
}

int main()
{
setlocale(LC_ALL,"Rus");
int i;
Stack MyStack('1');
MyStack.push('2');
MyStack.push('3');
MyStack.push('7');
MyStack.show();
cout<<endl;
for(i=0; i<4; i++){
cout<<MyStack.pop()<<endl;
}
MyStack.clear();
MyStack.push('K');
MyStack.push('R');
MyStack.show();
cout<<endl;
cout<<MyStack.top()<<endl;
cout<<MyStack.empty()<<endl;
system("pause");
return 0;
}

Результат работы программы класса Stack

класс Stack на C++

1560

Leave a Reply

Ваш адрес email не будет опубликован.