Data Structure Stack implementation in C

Data Structure: Stack implementation in C++

(A) Top of stack varying method

Algorithm for top of stack varying method

1. Declare and initialize necessary variables, eg top = -1, MAXSIZE etc.
2. For push operation,
If top = MAXSIZE - 1
print " stack overflow"
else
top = top + 1;
Read item from user
stack[top] = item
3. For next push operation, goto step 2.
4. For pop operation,
If top = -1
print "Stack underflow"
Else
item = stack[top]
top = top - 1
Display item
5. For next pop operation, goto step 4.
6. stop

Source code for top of stack varying method

#include
#include

#define MAX_SIZE 10

using namespace std;

class Stack{
    private:
        int item[MAX_SIZE];
        int top;
    public:
        Stack(){
            top = -1;
        }
        void push(int data){
            item[++top] = data;
        }
        int pop(){
            return item[top--];
        }

        int size(){
            return top+1;
        }

        bool is_empty(){
            if(top==-1)
                return true;
            else
                return false;
        }
        bool is_full(){
            if(top==MAX_SIZE-1)
                return true;
            else
                return false;
        }
        void display(){
            for(int i=0; i<this->size(); i++){
                cout< ";
            }
            cout<
        }
};

int main(){
    int choice;
    int temp;
    Stack stack;
    while(true){
        cout<<"Choose any one of the following"<
        cout<<"1. Push Item. "<
        cout<<"2. Pop Item."<
        cout<<"3. Size of Stack."<
        cout<<"4. Display all elements of stack."<
        cout<<"5. Exit."<
        cout<<"Your Choice: ";
        cin>>choice;
        switch(choice){
            case 1:
                if(!stack.is_full()){
                    cout<<"Enter number to push: ";
                    cin>>temp;
                    stack.push(temp);
                }else{
                    cout<<"Can't push. Stack is Full!!"<
                }
                break;
            case 2:
                if(!stack.is_empty()){
                    cout<<"The number popped is: "<
                }else{
                    cout<<"Cant't pop. Stack is Empty!!"<
                }
                break;
            case 3:
                cout<<"The size of the stack is: "<
                break;
            case 4:
                if(!stack.is_empty()){
                    cout<<"The elements in stack are: ";
                    stack.display();
                    cout<
                }else{
                    cout<<"Stack is Empty"<
                }
                break;
            case 5:
                exit(0);
                break;
        }
    }
    return 0;

}


(B) Top Fixed method ( Base varying method)

Algorithm for top fixed method

1. Declare and initialize necessary variables e.g. BOS = 0, MAXSIZE etc.
2. For push operation,
If BOS = MAXSIZE
print "stack overflow"
Else
Insert new element by making vacant space at stack[0] location
/* for(i = BOS; i > 0; i--){
stack[i] = stack[i-1];
}
stack[0] = BOS
++BOS
*/
3. For next push operation goto step 2.
4. For pop operation
If BOS = 0
print"stack underflow"
else
extract item from stack[0]. Decrement BOS and refil empty space by lowering each step by 1.
5. For next pop operation, goto step 4.
6. Stop

Source code for top fixed method

#include
#include
#define MAX_SIZE 10

using namespace std;

class Stack{
    private:
        int item[MAX_SIZE];
        int BOS;
    public:
        Stack(){
            BOS = 0;
        }
        void push(int data){
            for(int i=BOS; i>0; i--){
                item[i] = item[i-1];
            }
            item[0] = data;
            ++BOS;
        }

        int pop(){
            int temp = item[0];
            for(int i = 0; i
            item[i] = item[i+1];
            }
            BOS--;
            return temp;

        }

        void display(){
            for(int i = 0; i
                cout< ";
            }
            cout<
        }

        bool isEmpty(){
            if(BOS == 0){
                return true;
            }else{
                return false;
            }
        }

        bool isFull(){
            if(BOS == MAX_SIZE){
                return true;
            }else{
                return false;
            }
        }
};

int main(){
    int choice, data;
    Stack stack;
    while(true){
        cout<<"1. Push a Data"<
        cout<<"2. Pop a Data"<
        cout<<"3. Display All Element of Stack"<
        cout<<"4. Quit"<
        cout<<"Enter your choice: ";
        cin>>choice;
        switch(choice){
            case 1:
                if(!stack.isFull()){
                    cout<<"Enter the data to push: ";
                    cin>>data;
                    stack.push(data);
                    cout<
                }else{
                    cout<<"STACK IS FULL!!"<
                }
                break;
            case 2:
                if(!stack.isEmpty()){
                    cout<<"The data popped is "<
                }else{
                    cout<<"THE STACK IS EMPTY"<
                }
                break;
            case 3:
                if(!stack.isEmpty()){
                    stack.display();
                }else{
                    cout<<"STACK IS EMPTY"<
                }
                break;
            case 4:
                exit(0);
                break;
        }
    }
    return 0;

}