path To SE

        Here we will learn how to implement a Stack using an integer array with CLI (Command Line Interface).




      Here we have 3 private data. MaxSize to store the maximum number of data that can be stored in the stack.Top is to indicate the index of the last data inserted to the stack. We will have a pointer to a integer type array. We will create the array dynamically inside the constructor.

Here we implement the class as follows.


Class Stack

{

        private:

                int maxSize;

                int *stackArray;

                int top;

        public:

                Stack(int size);

                ~Stack();

                void push(int data);

                int pop();

                int peek();

                bool isFull();

                bool isEmpty();

};


Then we will start to implement the Stack.cpp file method by method.


 

The Constructor

Stack::Stack(int size)

{

        maxSize = size;

        stackArray = new int(maxSize);

        top = -1;

}


Here the value of size is assigned to maxSizemaxSize is the maximum number of values that we can store in this stack. Aafter that we will create an array with maxSize number of elements. Then we will assign -1 to top. top is the index that hold the value what is inserted at last to the array.


Now take a look at Destructor.

Stack::~Stack()

{

        delete []stackArray;

}

This is automatically called when the program ends. This will delete the array what we crated dynamically.

 

Push method is used to insert a value to the stack. This will accept an integer type value.

void Stack::push(int data)

{

        if(! isFull())

                 stackArray[++top] = data;

        else

                cout << "Stack is full...! Cannot insert any more Data..."

                        << endl;

}

Here we can use if((top+1) > maxSize) instead of if(isFull()). But we have isFull() method to check the same thing. So we can call it here. isFull() method checks that the stack is full or not. If there no more empty spaces available, this will return false! mark inverts the return value. It means that the method returned false! mark will turn it into true. So if stack is not full, value of data will be assigned into the next available index. Otherwise, it will display an error message. It means if the Stack is Full, it will inform user that the Stack is full & cannot insert any more data.top contains the index of last data inserted.So if any data needs to be inserted, we have to increase top by 1 before it. ++top will increase the value of top by 1 before performing the action.


Pop method will remove the last data which is inserted to the array.

int Stack::pop()

{

        if (! isEmpty())

                return stackArray[top--];

        else

                cout  << "The Stack is Empty..!" << endl;

                return -999;

}

This method will delete the last data inserted to the array. top contains the index of last data inserted. So we have to do it using top. Here we will not delete the value which inserted at last. We just decrease the value of top by one, & then it acts like the last value is deleted. Because we cannot access it anymore. When we insert another value, it will be replaced. As we did in the push method, we will check if the Stack is empty or not & we will decrease the top by one. Else, this also will give an error message. 

After decreasing the top, we cannot access the value any more. So we have to return it before removing. We can do it by just adding return statement. But if the Stack is empty, removing will not be performed. So we have to return a value that indicate the action is not performed. We can use any value for this but we have to make sure that this value will not be inserted to the Stack by user. As an example, Lets think that we are going implement a Stack to store marks of a exam. We can return any minus value to indicate that the action is not performed because we know that minus values will not be entered as exam marks.


Peek method.

int Stack::peek()

{

        if (! isEmpty())

                return stackArray[top];

        else

                cout  << "The Stack is Empty..! " << endl;

                return -999;

}

This is same as pop() method but only difference is we will not decrease top here. We just return the value of top.


isFull method.

bool Stack::isFull()

{

        return (top == maxSize-1);

}

This is to check whether the Stack is full or not. maxSize is the value that contains the size of the Stack. top is the value that contains the index of value on top. If the stack already contains it's maximum number of values, this method will return true. If noy this will return false


isEmpty method.

bool Stack::isEmpty()

{

        return (top == -1);

}

This is used to check whether the Stack is Empty or not. If there no values in the Stack, This will return true. Otherwise this will return false.

 <  Back   

 

Download the Complete Project

Integer Stack.zip Integer Stack.zip
Size : 1401.506 Kb
Type : zip

This will work on both Linux & Windows

This free website was made using Yola.

No HTML skills required. Build your website in minutes.

Go to www.yola.com and sign up today!

Make a free website with Yola