Stacks with C++ ?

Data is important. Storing & Organizing Data has been the key goal of most complex Computer Programs. Not just Store & organize, retrieve Data in the most efficient & reliable way could have unprecedented importance. To achieve this goal, we can use Data Structures.
 
Data Structures are particular way of storing data & manage the data the way we want it. Among the Composite, Abstract & Primitive data structures, Stack is of course abstract.

Stack? Stack is a data structure with Sequential Access as stored data could be accessed in a sequence Like in Cassette Tape. Stack is built on (LIFO) Last In First Out principle.

Three operations are involved with Stack. Push, Pop & Peek. These, operations proves the sequential accessibility of Stack.


The C++ code could be developed for these operations like follows. First, create Stack.h, Stack.cpp & main.cpp files in C++ project. Header file (*.h) includes the attribute & method declarations.

//Stack.h
class Stack
{
      private:
              int maxSize;
              double *Store;
              int top; // index
      public:
             ~Stack(); // destructor frees up the memory
             Stack(int s);
             double pop(); // pop operation
             void push(double num); // push operation & push number
             double peek(); // peek the top value
      };

*.cpp file includes the method definitions of the Stack class.

//Stack.cpp
#include "Stack.h"
#include <iostream>
using namespace std;

Stack::Stack(int s)
{
     maxSize = s; // determines the size of array
     top = -1; // initial index number
     Store = new double[maxSize];
}
               
double Stack:: peek()
{     
      if(top == -1)
      {
             cout<<"Stack is Empty"<<endl;
             return -999;
      }
      else if (top == maxSize)
      {
              cout<<"Stack is Full"<<endl;
              return Store[top];
      }
      else
      {
              return Store[top];
      }
}

double Stack::pop()
{
       if(top ==-1)
       {
             cout<<"Stack is Empty"<<endl;
             return -999;                   
       }
       else
       {
             return Store[top--];
       } 
                   
}
            
void Stack::push(double j)
{
       if(top == maxSize-1)
       {
              cout<<"Stack is Full"<<endl;
       }
       else
       {
              Store[++top] = j;
       }
}

'main.cpp' should be the main execution file which ultimately helps to run the program.

//main.cpp
#include "Stack.h"
#include<iostream>
using namespace std;

int main()
{       
     Stack *a = new Stack(5);
     a->push(10);
     cout<<a->peek()<<endl;
     a->push(20);
     a->push(30);
     a->push(40);
     a->push(50);
     a->push(60); // these values won't be inserted
     a->push(70); // as Stack size is 5 & it's full
     cout<<a->peek()<<endl; //peeks the final value should be 50
     system("PAUSE");
     return 0;
}


Even though, the C++ Programming Code sometimes may be problematic for those who're not familiar with C++, the Concept of Stacks is Clear.

1. Push - Enter an Item. If stack isn't Full.
2. Pop -  Remove an Item, if Stack isn't Empty.
3. Peek - Check the Item at the Top.

The idea is similar to a Stack of Books. "If you need to Remove the firstly entered material you've to remove all other material to do so."




Thank You!

No comments: