13 Nisan 2010 Salı

C++ ile Stack(Yığın) Tanımlama Pop() ve Push() Fonksiyonları

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
#define size 10
using namespace std;
class stack{
      char dizi[size];
      int tos;
      public:
             stack();
             void push(char ch);
             char pop();
      };
stack::stack(){
               cout<<"yigin olusturuluyor"<<endl;
               tos=0;
               }
void stack::push(char ch){
     if(tos==size){
                   cout<<"yigin is full\n";
                   return;
                   }
     dizi[tos]=ch;
     tos++;
     }
char stack::pop(){
     if(tos==0){
                cout<<"stack is empty\n";
                return 0;
                }
     tos--;
     return dizi[tos];
     }
int main(int argc, char *argv[])
{
    stack s1,s2;
    int i;
    s1.push('K');
    s1.push('E');
    s1.push('W');
    s1.push('I');
    s1.push('N');
    s2=s1;
    for(i=0;i<5;i++) cout<<s1.pop()<<"\t";
    cout<<endl;
    for(i=0;i<5;i++) cout<<s2.pop()<<"\t";
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder