Question

In: Computer Science

Stack and Queue In this question, you will implement a modified version of stackArray class. This...

Stack and Queue

In this question, you will implement a modified version of stackArray class. This modified class requires you to:

• Implement two stacks using a single array. One stack uses the beginning of the array and the other uses the other end.

• Your code should allowing pushes to and pops from each stack by passing the 0 and 1 to any function to identify the needed stack.

• All stack functions should be be applied on both stack. For example, print(0) will print the content of the first stack while print(1) will print the other stack.

• Your stack should contain the following functions at least: isEmpty, isFull, push, pop, top, clear, print and getsize.

• The capacity of the array may be grow depending on the sum of number of objects stored in the two stacks.

• Moreover, the capacity maybe reduced to the half of the current capacity if the number of objects remaining in the two stacks (after each pop) is 1/4 the capacity of the array or less. The capacity of the array may not be reduced below the initially specified capacity.

using C++

this is the stack array code

#define DEFAULT_CAPACITY 10000
template <class T> class StackArray{
public:
   StackArray(int capacity = DEFAULT_CAPACITY);
   ~StackArray();

   void push(T& val);
   T pop(void);
   T top(void);
   bool isEmpty(void);
   bool isFull(void);
   void clear(void);

private:
   T* data;
   int capacity, last;
};


template <class T>
StackArray<T>::StackArray(int cap){
   capacity = cap;
   data = new T[capacity];
   last = -1;
}

template <class T>
StackArray<T>::~StackArray(void){
   delete [] data;
}

template <class T>
void StackArray<T>::push(T& el)
{
   if(isFull() )
   {
       printf("\n Can't push into a full stack!");
       return;
   }
   last++;
   data[last] = el;
}

template <class T>
T StackArray<T>::pop()
{
   if(isEmpty() )
   {
       printf("\n Can't pop from an empty stack!");
       return -1;
   }
   T el = data[last];
   last--;
   return el;
}

template <class T>
T StackArray<T>::top()
{
   if(!isEmpty() )
   {
       printf("\n Stack is empty!");
       return -1;
   }
   return data[last];
}

template <class T>
bool StackArray<T>::isEmpty(void)
{
   return last == -1;
}

template <class T>
bool StackArray<T>::isFull(void)
{
   return last+1 == capacity;
}

template <class T>
void StackArray<T>::clear(void)
{
   last = -1;
}

Solutions

Expert Solution

Summary :

Provided Notes , Code and sample output with testing push and pop with resizing (reducing) the size of internal structures.

Notes :

In order to store both stack info in same array , utilized last[] which store the index of stack[0] in last[0] and stack[1] in last[1] and thus most of the operations are automatic in terms pushing and poping and other operations.

Only isEmpty() requires special handing in both cases which involves comparing the respective elements .

Resizing : Here upon resizing store the old data and depending on reducing or increasing the size create the array and copy the old data and only require to offset the last[1] pointer as the last[0] remains same in resizing .

added printInfo() funciton to print the stats of the current state of both the stacks .

As part of the test program created a intial stack of size 6 and insert 24 elements into the stack with index module on stack numbe so that equal number of elements are pushed in both the stacks ( 0 & 1 ) . and similarly pop() 24 times on the stack .   this allows to test increasing the size and reducing the size twice that initial size .

It should work with distinct sizes of stack 0 & 1 which requires resize .

################ Code ######################


#ifndef DSTACK_H_
#define DSTACK_H_
#define DEFAULT_CAPACITY 10000

template <class T>
class StackArray{
public:
   StackArray(int capacity = DEFAULT_CAPACITY);
   ~StackArray();

   void push(T& val, int);
   T pop(int);
   T top(int);
   bool isEmpty(int);
   bool isFull(int);
   void clear(int);
   void printInfo();


private:
   void resize();
   T* data;
   // capacity[0] store the initial capacity
   // capacity[1] store current capacity
   int capacity[2];
   // last[0] store the stack[0] last item
   // last[1] store the stack[1] last item
   int last[2];
   int op[2];
};


#endif /* DSTACK_H_ */
#include <iostream>

#include "dStack.h"

using namespace std;


template <class T>
StackArray<T>::StackArray(int cap){
   capacity[0] = cap;
   capacity[1] = cap;
   data = new T[capacity[1]];
   last[0] = -1;
   last[1] = capacity[1];
   op[0] = 1;
   op[1] = -1;
}

template <class T>
StackArray<T>::~StackArray(void){
   delete [] data;
}

// THis method pushes the given element to the top of stack
template <class T>
void StackArray<T>::push(T& el,int stack_index)
{
   if(isFull(stack_index) )
   {
       resize();
   }
   // Chance the last index based on given index
   // ie for 0 , it should increment and for 1 it should decrement
   last[stack_index] = last[stack_index] + op[stack_index];
   data[last[stack_index]] = el;
}

// This method returns the top of the stack if its not empty
// further it resizes the stack if the occupancy is 1/4 of current capacity.
template <class T>
T StackArray<T>::pop(int stack_index)
{
   if(isEmpty(stack_index) )
   {
       printf("\n Can't pop from an empty stack!");
       return -1;
   }
   T el = data[last[stack_index]];
   last[stack_index] = last[stack_index] + op[ ( stack_index+1)%2] ;
   int current_occupancy = last[0] +1+ ( capacity[1] - last[1]) ;

   if ( current_occupancy <= ( capacity[1]/4 ))
   {
     //     cout << " Time to reduce \n";
     resize();
   }

   return el;
}


template <class T>
T StackArray<T>::top(int stack_index)
{
  // return the top of the stack for respective stack index
  if(!isEmpty(stack_index) )
   {
       printf("\n Stack is empty!");
       return -1;
   }
   return data[last[stack_index]];
}

// If stack index of respect stacks are at bottom then its empty
template <class T>
bool StackArray<T>::isEmpty(int stack_index)
{
  if ( stack_index == 0 )
   return last[stack_index] == -1;
  else
    return last[stack_index] == capacity[1];
}


template <class T>
bool StackArray<T>::isFull(int stack_index)
{
 // if last index of both stacks are adjacent then stack is full
  return last[0] + 1 == last[1];

}


template <class T>
void StackArray<T>::clear(int stack_index)
{
  if ( stack_index == 0 )
   last[stack_index] = -1;
  else
    last[stack_index] = capacity[1];

}


// The same resize method is used for increasing the size as welll as reducing the capacity of the Array
// which holds two stacks.

template <class T>
void StackArray<T>::resize()
{
  //cout << " Prior to resize : ";
  //printInfo();
  int prev_capacity = capacity[1];
  T* old_data = data ;

  if ( isFull(1) )
  {
    //cout << " Prior to resize : ";
    //printInfo();
    int prev_capacity = capacity[1];
    capacity[1] = prev_capacity * 2 ;

    data = new T[capacity[1]];

    for(int i=0 ; i <= last[0] ; i++)
    {
      data[i] = old_data[i] ;
    }

    for(int i= 0 ; i <  (prev_capacity - last[1])  ; i++)
    {
      data[capacity[1] -1 - i] = old_data[prev_capacity -1 -i ] ;
    }

    last[1] = capacity[1] - ( prev_capacity - last[1]);
    //cout << " Resize Done \n";
    //printInfo();
    delete[] old_data;
  } else {
    if ( prev_capacity/2 >= capacity[0])
    {
         capacity[1] = prev_capacity/2 ;

         data = new T[capacity[1]];

         for(int i=0 ; i <= last[0] ; i++)
         {
           data[i] = old_data[i] ;
         }

         for(int i= 0 ; i <  (prev_capacity - last[1])  ; i++)
            {
              data[capacity[1] -1 - i] = old_data[prev_capacity -1 -i ] ;
            }

        last[1] = capacity[1] - ( prev_capacity - last[1]);
       // cout << " Resize Done \n";
            //printInfo();
            delete[] old_data;




    }
  }

}


// THis method prints the Internal stack info such as the last index of both
// occupancy , the current size , etc.
template <class T>
void StackArray<T>::printInfo()
{

  cout << "\n###################################################################\n";
  cout << " Current Capacity : " << capacity[1] << "\n";
  cout << " Occupancy "  << ( last[0] +1 + ( capacity[1] - last[1])) << " \n";
  cout << " Stack[0] Last :  " << last[0] << " \n Stack[1] Last : " << last[1] << "\n";

  cout << "\n Stack[0] -> Empty - " << this->isEmpty(0) << "\n";
  cout << " Stack[0] -> Full - " << this->isFull(0) << "\n";

  cout << " Contents of Stack[0] - > " ;
  for( int i=0 ; i <= last[0]; i++)
    cout << data[i]   << " " ;


  cout << "\n\n Stack[1] -> Empty - " << this->isEmpty(1) << "\n";
  cout << " Stack[1] -> Full - " << this->isFull(1) << "\n";

  cout << " Contents of Stack[1] - > " ;
    for( int i = capacity[1] -1  ; i >= last[1]; i--)
      cout << data[i]   << " " ;

    cout << "\n###################################################################\n";
}



int main()
{

  // Initialize the stack with 6 size
   StackArray<int> st(6);


   // Insert 24 elements which should double the size from 6 to 12 to 24
   for ( int i=0 ; i < 24 ; i++ )
   {
       st.push( i, i%2 );
   }

   // Print Stack info.
   st.printInfo();


   // Now Pop all the elements and at the end the size should be initial size which is 6
   cout << " Popping - > ";
   for ( int i=0 ; i < 24 ; i++ )
   {
     cout << " ( Stack[" << (i%2 ) << "] -> " <<  st.pop( i%2 ) << " ) , ";
   }

   // Print Stack info.
   st.printInfo();
}


#################### Output ####################


Related Solutions

Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); pu
Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); public Stack() { }    public Stack(int n) { pool.ensureCapacity(n); }    public void clear() { pool.clear(); }    public boolean isEmpty() { return pool.isEmpty(); }    public Object topEl() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.get(pool.size()-1); }    public Object pop() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.remove(pool.size()-1); }    public void push(Object el) { pool.add(el); }    public String toString() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Are you able to implement a stack using just one queue? If yes, please provide the...
Are you able to implement a stack using just one queue? If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If no, explain.
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array...
3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. The implementation can be found in the book and in our lecture slides. import java . io .*; import java . util .*; public class Stack2540Array...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT