Tuesday, August 16, 2016

C++ Program demonstrate the Stacks in C++


#include <iostream>
#include <stdlib.h>
#include <cstdlib>
 
using namespace std;

class Stack {
 private:
  static const int max = 10;
  int A[max];
  int top;
 public:
  Stack() {top = -1;}
  bool isEmpty();
     bool isFull();
     int pop();
  void push(int x);
  int display();
};

bool Stack::isEmpty()
{
    if(top == -1)
        return true;
    return false;
}
 
bool Stack::isFull()
{
    if(top == max - 1)
        return true;
    return false;
}

int Stack::pop()
{
    if(isEmpty())
    {
        cout<<"Stack is Empty\n";
        system("read");
    }
    int x = A[top];
    top--;
    return x;
}

void Stack::push(int x){
 if(isFull())
    {
        cout<<"Stack is Full!\n";
        system("read");
    }
    top++;
    A[top] = x;
}

int Stack::display(){
 int i;
 //for (i=0;i<10;i++){
 // cout << A[i] << endl;
 //}
 for(i=top;i>=0;i--){
  //cout << "Stack ["+i+"] = ";
  cout << A[i] << endl;
 }
}

int main() {
 int x=0;
 int choice;
 Stack s;
 
 return 0; 
}

No comments:

Post a Comment