Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Two stacks of positive integers are needed, one containing elements with val

ID: 3697083 • Letter: C

Question

C++

Two stacks of positive integers are needed, one containing elements with values less than or equal to 1,000 and the other containing elements with values larger than 1,000. The total number of elements in the small-value stack and the large-value stack combined are not more than 200 at any time, but we cannot predict how many are in each stack. (All of the elements could be in the small-value stack, they could be evenly divided, both stacks could be empty, and so on.) Can you think of a way to implement both stacks in one array?

a. Draw a diagram of how he stack might look.

b. Write definitions for such a double-stack structure.

c. Implement the push operation; it should store the new item into the correct stack according to its value (compared to 1,000).

Explanation / Answer

a.

class stack

{

public :

stack();

void push (int item);

void poplarge (int& item);

void popsmall (int& item);

private:

int smallTop;

int largeTop;

int items[200];

}

b.

Double stack -> means two stacks which are implemented using a single array. To prevent memory wastage, the two stacks are grown in opposite direction. The pointer top_left and top_right points to top-most element of left stack and right stack respectively. Initially, tops_left is initialized as -1 and top_right is initialized the capacity. As the elements are pushed into left stack, top_left is incremented.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote