Question

In: Computer Science

Suppose that two stacks of positive integers are needed for a project. Each stack is to...

Suppose that two stacks of positive integers are needed for a project. Each stack is to contain integers that are less than or equal to 500. One stack is to contain even integers; the other stack is to contain odd integers. Also, the total number of values in the combined stacks at any given time will not exceed 200. ▪ Design a way to implement both stacks in only one(pay attention!!!) 1-D array. ▪ Write a Python class definition(s) for the double stack representation designed previously. Include the following operations: ◦ isEmpty (for each stack) ◦ size (for each stack) ◦ push (insert an integer onto the correct stack depending on whether it is even or odd) ◦ pop (for each stack) ◦ top (for each stack) ▪ Fully test all the stack operations in a Python main program.Note two stacks together should only use one array.

Solutions

Expert Solution

Solution :

#include<iostream>
#include<stdlib.h>
#define size 1000

using namespace std;

class twoStacks
{
    int arr[size];
    int top1, top2;
public:
   twoStacks() // constructor
   {
       top1 = -1;
       top2 = size;
   }

   // Method to push an element x to stack1
   void push1(int x)
   {
       // There is at least one empty space for new element
       if (top1 < top2 - 1)
       {
           top1++;
           arr[top1] = x;
       }
       else
       {
           cout << "Stack Overflow";
           exit(1);
       }
   }

   // Method to push an element x to stack2
   void push2(int x)
   {
       // There is at least one empty space for new element
       if (top1 < top2 - 1)
       {
           top2--;
           arr[top2] = x;
       }
       else
       {
           cout << "Stack Overflow";
           exit(1);
       }
   }

   // Method to pop an element from first stack
   int pop1()
   {
       if (top1 >= 0 )
       {
          int x = arr[top1];
          top1--;
          return x;
       }
       else
       {
           cout << "Stack UnderFlow";
           exit(1);
       }
   }

   // Method to pop an element from second stack
   int pop2()
   {
       if (top2 < size)
       {
          int x = arr[top2];
          top2++;
          return x;
       }
       else
       {
           cout << "Stack UnderFlow";
           exit(1);
       }
   }
    void print_stack1 ()
    {
    int i;
     for (i = top1; i >= 0; --i)
   {
      
       printf ("%d ", arr[i]);
   }
    printf (" ");
    }

    void print_stack2 ()
{
int i;
for (i = top2; i < size; ++i)
{
      
    printf ("%d ", arr[i]);
}
printf (" ");
}
};


/* Driver program to test twStacks class */
int main()
{
    twoStacks st;
    int num,choice=1;
    while(choice==1)
    {
        cout <<"enter the element";
        cin >> num;
        if(num%2==0)
        st.push1(num);
        else
        st.push2(num);
        cout<<"enter 1 to enter other element in array ";
        cin>>choice;
               
    }
    cout<<"content of stack 1 ";
    st.print_stack1();
    cout<<"content of stack 2 ";
    st.print_stack2();
    return 0;
}

Output :


Related Solutions

Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Assume you have a stack of integers. The stack contains same number of positive and negative...
Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following interface: ourStack() constructor, pop,...
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
Python Stack: The following was done already in the Lab in the Stacks Module with Doubly...
Python Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List. Create an application to help you stack and un-stack containers in the ship. Create a class called container which will have the object (data), the link (next) Create a class called Pod which is Stack. Include methods addContainer and removeContainer Implement these classes by creating multiple containers to go inside the pod. ADD the Following feature: Include a class attribute in the...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
suppose a and b are positive integers. price the following by biconditional statement ---- a +1...
suppose a and b are positive integers. price the following by biconditional statement ---- a +1 divides B and B divides b + 3 if and only if a = 2 and b = 3
please in java ! Assume you have a stack of integers. The stack contains same number...
please in java ! Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following...
1.Suppose n and k are two positive integers. Pick a uniformly random lattice path from (0,...
1.Suppose n and k are two positive integers. Pick a uniformly random lattice path from (0, 0) to (n, k). What is the probability that the first step is ‘up’?
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT