A stack class

Discussion in 'Computer Science & Culture' started by S.Tarafdar, Jun 29, 2003.

Thread Status:
Not open for further replies.
  1. S.Tarafdar Registered Senior Member

    Messages:
    35
    Dear All,
    I am trying to write a c++ program named a stack class for integers .The methods are:
    void push (int a)-pushes a given value onto the stack.

    int pop()-removes the topmost element from the stack and returns it to the caller

    bool isempty()-returns true when there are no elements on the stack.
    This is what I wrote:

    #include <iostream>

    using namespace std;
    #define size 10

    class stack{
    int number[size];
    int index;
    public:
    stack();
    void push(int a);
    int pop();
    bool isempty();

    };

    stack::stack(){
    index=0;
    }


    void stack:

    Please Register or Log in to view the hidden image!

    ush(int a){
    number[index]=a;
    index++;


    }

    bool stack::isempty(){
    if(index==0) return true;

    }

    int stack:

    Please Register or Log in to view the hidden image!

    op(){
    index--;
    return number[index];
    }

    int main(){
    stack a,b;
    a.push(1);
    a.push(2);
    a.push(3);
    b.push(4);
    b.push(5);
    b.push(6);
    for( int i=0;i<3;i++) cout<<"pop a:"<<a.pop()<<endl;

    for( int j=0;j<3;j++) cout<<"pop b:"<<b.pop()<<endl;


    return 0;
    }

    I do not want to change the methods and their return types. So,how can I make bool isempty() function effective?

    Then also these are the restrictions:
    stack size should be a compile-time constant.
    It has to pop all the items from the first stack and push them onto the second,printing all integers while it does so.Finally,it has to pop all elements from the second stack and print them again.

    What modifications shall I make?

    Would you pls anybody help me out?

    Regards,
    S.Tarafdar
     
    Last edited: Jun 29, 2003
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. SG-N Registered Senior Member

    Messages:
    1,051
    Is it a joke? Do you understand the C++ program? If you do, then you should know how to make your modifications (that's so easy...).
    Anyway, I've got 10b points to talk about :
    - you should add a line to check if the stack is full before adding a new value...
    - I can't remember what will happen if you use isempty() on a non-empty stack : it does not return anything, is it?

    What do you mean?

    Note : That's funny : when I put my mouse over the end of your text, it becomes blue...

    Please Register or Log in to view the hidden image!

     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. S.Tarafdar Registered Senior Member

    Messages:
    35
    About SG.N

    Hello SG.N,
    You talk too much.You are simply talkative.You yourself are a clown,that is why everything seems funny to you!
    Well,had I had C++ known very well,why should I have asked questions to this group?Only can a nonsense like you not understand this fact!
    From now on,I do not expect any reply from you.Just shut up!
    I did not reply to your previous response.......I should have done it!No one in this group are so foolish and idiot like you!

    S.Tarafdar
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. SG-N Registered Senior Member

    Messages:
    1,051
  8. SG-N Registered Senior Member

    Messages:
    1,051
    Well... NO, that's not all!

    Does it sound agressive to you? I don't think so! You give us one of the most easy C++ program I've ever seen and you don't seem to understand it! Sorry, I just thought you did...
    Have you got a problem with advices?!
    ??? That's YOUR question! "So,how can I make bool isempty() function effective?" The function should work (maybe following my second advice) so I didn't understood what you were meaning! And I still don't.
    Shit, what's wrong here? That bug in the forum is funny!

    Now, if you've got a problem with me, I don't care! I don't have any problem with C++ (not at this level) so if you ask clear questions, you will have clear answers...

    Oh, and one more thing : You are a "Master's student in Mechatronics", not a kid! Don't cry as soon as someone seems to be laughing about you! (I didn't...)
     
  9. SG-N Registered Senior Member

    Messages:
    1,051
    Here is your hard solution !

    Code:
    #include
    
    using namespace std;
    #define size 10
    
    class stack{
    int number[size];
    int index;
    public:
    stack();
    void push(int a);
    int pop();
    bool isempty();
    
    };
    
    stack::stack(){
    index=0;
    }
    
    
    void stack::push(int a){
    number[index]=a;
    index++;
    
    
    }
    
    bool stack::isempty(){
    if(index==0) return true;
    return false;
    }
    
    int stack::pop(){
    index--;
    return number[index];
    }
    
    int main(){
    stack a,b;
    a.push(1);
    a.push(2);
    a.push(3);
    b.push(4);
    b.push(5);
    b.push(6);
    while (!(a.isempty())){
      c = a.pop();
      b.push(c);
      cout << "pop a:" << c;
    }
    while (!(b.isempty())){
      cout << "pop b:" << b.pop();
    }
    
    return 0;
    } 
    
    And you could change the "push" method with :
    Code:
    void stack::push(int a){
    if (index == size){
      cout << "sorry, the stack is full";
      exit(1);
    }
    number[index]=a;
    index++;
    
    }
    
     
  10. SG-N Registered Senior Member

    Messages:
    1,051
    And just for your education...

    Code:
    #include <iostream>
    using namespace std;
    
    static const int SIZE = 10;
    
    class Stack {
    
    private:
    int number[];
    int index;
    
    public:
      Stack() {
        number = new int[SIZE];
        index = 0;
      }
    
      void push(int value) {
        if(isFull()) {
          cout << "Stack overflow !" << endl;
          exit(-1);
        }
        number[index++] = value;
      }
    
      int pop() {
        if(isEmpty()) {
          cout << "Stack overplouf !" << endl;
          exit(-1);
        }
        return number[--index];
      }
    
      bool isEmpty() {
        return index == 0;
      }
    
      bool ifFull() {
        return index == SIZE;
      }
    
    }
    
    int main(){
      Stack a,b;
    
      a.push(1);
      a.push(2);
      a.push(3);
      b.push(4);
      b.push(5);
      b.push(6);
    
      while (!a.isempty()){
        c = a.pop();
        b.push(c);
        cout << "pop a:" << c << endl;
      }
    
      while (!b.isempty()){
        cout << "pop b:" << b.pop() << endl;
      }
    
      exit(0);
    }
    
     
Thread Status:
Not open for further replies.

Share This Page