Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I need a C++ code that implement a stack ( push , pop , print elements )..
Posted
Comments
haitrieu749 11-Oct-12 12:10pm    
It's very easy! Documents about this is very much in google :D

What's wrong with the STL provided one[^]?
 
Share this answer
 
If you are looking for a simple C++ stack program, then have a look at this:
C++
#include <iostream>
using namespace std;

#define MAX 10

class Stack
{
private:
       int arr[MAX];
       int top;

public:
      Stack()
      {
            top = -1;
      }

       void Push(int item)
      {
             if(top == MAX-1)
            {
                  cout<<endl<< "Stack is full";
                   return;
            }

            top++;
            arr[top] = item;
      }

       int Pop()
      {
             if(top == -1)
            {
                  cout<<endl<< "Stack is empty";
                   return NULL;
            }

            int data = arr[top];
            top--;

             return data;
      }

};

int main()
{
      Stack s;

      s.Push(1);
      s.Push(2);
      s.Push(3);

       int i = s.Pop();
      cout<<endl<< "item= popped = "<<i<<endl;

      i = s.Pop();
      cout<<endl<< "Item popped = "<<i<<endl;

       return 0;
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900