Answer to Question #244785 in C++ for Myname

Question #244785
Write a program to implement the following algorithm:
 Start with two indexes one at the left and other at the right end of the array.
 Left index simulate the first stack and second index simulate the right stack.
 If we want to put the element in the first stack then put the element at the left index.
Similarly, if we want to put the element in the second stack then put the element at the
right index.
 First stack grow towards left and second stack grow towards left.
A simple demonstration of dual stack can be observed from the following figure:
Stack A empty space stack B
1 2 3 4 [ ] [ ] [ ] 6 7 8 9
----> <-----
Entering elements Entering elements From this side From this side
1
Expert's answer
2021-09-30T04:46:10-0400
#include <iostream>
using namespace std;
int main(){
    int array[11], left_index = 0, second_index = 10;
    for(int i = 0; i < 9; i++) array[i] = INT_MIN;


    cout<<"Entering elements 1, 2, 3 and 4 in stack A...\n";
    for(int i = 1; i < 5; i++){
        array[left_index++] = i;
    }
    cout<<"Entering elements 9, 8, 7 and 6 in stack B...\n";
    for(int i = 9; i >= 6; i--){
        array[second_index--] = i;
    }
    cout<<"Stack A, Empty Space, Stack B\n";
    for(int i = 0; i < 11; i++){
        if(array[i] != INT_MIN) cout<<array[i]<<" ";
        else cout<<"[ ] ";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS