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

First time working with stack implementation. Newbie to java Two stacks of positive integers are needed,...
First time working with stack implementation. Newbie to java Two stacks of positive integers are needed, both containing integers with values less than or equal to 1000. One stack contains even integers, the other contains odd integers. The total number of elements in the combined stacks is never more than 200 at anytime. but we cannot predict how many are in each stack. (All of the elements could be in one stack, they could be evenly divided, both stacks could...
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...
/** * Write a recursive function that accepts a stack of integers and * replaces each...
/** * Write a recursive function that accepts a stack of integers and * replaces each int with two copies of that integer. For example, * calling repeatStack and passing in a stack of { 1, 2, 3} would change * the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use * any data structures other than the stack passed in as a parameter. * @param stack */ public static void...
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack...
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws...
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...
Write a program that implements a stack of integers, and exercises the stack based on commands...
Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws runtime_error("stack is empty")...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT