Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello friends,
 
I need a C++ code that implement a stack ( push , pop , print elements )..
Posted 7-Oct-12 23:13pm
Comments
haitrieu749 at 11-Oct-12 12:10pm
   
It's very easy! Documents about this is very much in google :D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you are looking for a simple C++ stack program, then have a look at this:
#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;
}
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

What's wrong with the STL provided one[^]?
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100