Question

QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

QUESTION: ADT stack: resizable array-based implementation
   for Ch4 programming problem 4
"maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp

//FILE StackInterface.h

#ifndef STACK_INTERFACE_
#define STACK_INTERFACE_

template<class ItemType>
class StackInterface
{
public:
   /** Sees whether this stack is empty.
   @return True if the stack is empty, or false if not. */
   virtual bool isEmpty() const = 0;

   /** Adds a new entry to the top of this stack.
   @post If the operation was successful, newEntry is at the top of the stack.
   @param newEntry The object to be added as a new entry.
   @return True if the addition is successful or false if not. */
   virtual bool push(const ItemType& newEntry) = 0;

   /** Removes the top of this stack.
   @post If the operation was successful, the top of the stack
   has been removed.
   @return True if the removal is successful or false if not. */
   virtual bool pop() = 0;

   /** Returns the top of this stack.
   @pre The stack is not empty.
   @post The top of the stack has been returned, and
   the stack is unchanged.
   @return The top of the stack. */
   virtual ItemType peek() const = 0;

   /** Destroys object and frees memory allocated by object. */
   virtual ~StackInterface() { }
}; // end StackInterface
#endif

//ArrayStackP4.h

#ifndef ARRAY_STACK_P4_
#define ARRAY_STACK_P4_
#include <cassert>
#include "StackInterface.h"

template<class ItemType>
class ArrayStackP4 : public StackInterface<ItemType>
{
private:
   static const int INIT_SIZE = 5; // initial size of the array

   int arraySize;   // current size of the array
           // "Anytime the stack becomes full, double the size of the array"

   int top;   // Index to top of stack

   int numEntries;   // number of entries in the stack
           // this number was deducible from top in ArrayStackP1 and ArrayStackP2

   ItemType* items; // resizable array of stack entries

public:
   ArrayStackP4() : arraySize(INIT_SIZE), top(INIT_SIZE - 1), numEntries(0) // Default constructor
   {
       items = new ItemType[arraySize];
   }
   ArrayStackP4(const ArrayStackP4<ItemType>& rhs) : arraySize(rhs.arraySize), top(rhs.top), numEntries(rhs.numEntries) // copy constructor
   {
       items = new ItemType[arraySize];
       for (int i = arraySize - numEntries; i < arraySize; ++i)
           items[i] = rhs.items[i];
   }
   ~ArrayStackP4()   // destructor
   {
       delete[] items;
   }
   bool isEmpty() const;
   bool push(const ItemType& newEntry);
   bool pop();
   ItemType peek() const {
       assert((!isEmpty()) && (top == arraySize - 1));
       return items[top];
   };
   int getNumEntries() const // returns the number of entries in the stack
   {
       return numEntries;
   }
   int getCapacity() const   // returns the size of the resizable array
   {
       return arraySize;
   }
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//ArrayStackP4.h

#ifndef ARRAY_STACK_P4_

#define ARRAY_STACK_P4_

#include <cassert>

#include "StackInterface.h"

template<class ItemType>

class ArrayStackP4 : public StackInterface<ItemType>

{

private:

static const int INIT_SIZE = 5; // initial size of the array

int arraySize; // current size of the array

// "Anytime the stack becomes full, double the size of the array"

int top; // Index to top of stack

int numEntries; // number of entries in the stack

// this number was deducible from top in ArrayStackP1 and ArrayStackP2

ItemType* items; // resizable array of stack entries

public:

ArrayStackP4() : arraySize(INIT_SIZE), top(INIT_SIZE - 1), numEntries(0) // Default constructor

{

items = new ItemType[arraySize];

}

ArrayStackP4(const ArrayStackP4<ItemType>& rhs) : arraySize(rhs.arraySize), top(rhs.top), numEntries(rhs.numEntries) // copy constructor

{

items = new ItemType[arraySize];

for (int i = arraySize - numEntries; i < arraySize; ++i)

items[i] = rhs.items[i];

}

~ArrayStackP4() // destructor

{

delete[] items;

}

bool isEmpty() const;

bool push(const ItemType& newEntry);

bool pop();

bool isEmpty() const {

return numEntries == 0;

}

bool push(const ItemType& newEntry) {

if (numEntries == arraySize) {

ItemType* newItems = new ItemType[arraySize * 2];

int i;

for(i = 0; i < numEntries; i++) {

newItems[arraySize * 2 - 2 - i] = items[arraySize - 1 - i];

}

items = newItems;

arraySize *= 2;

top = arraySize - 1;

items[top] = newEntry;

numEntries++;

return true;

}

int i;

for (i = numEntries - 1; i >= 0; i--) {

items[arraySize - 2 - i] = items[arraySize - 1 - i];

}

items[top] = newEntry;

numEntries++;

return true;

}

bool pop() {

if(isEmpty())

return false;

int i;

numEntries--;

for (i = 0; i < numEntries; i++) {

items[arraySize - 1 - i] = items[arraySize - 2 - i];

}

return true;

}

ItemType peek() const {

assert((!isEmpty()) && (top == arraySize - 1));

return items[top];

};

int getNumEntries() const // returns the number of entries in the stack

{

return numEntries;

}

int getCapacity() const // returns the size of the resizable array

{

return arraySize;

}

};

#endif

Add a comment
Know the answer?
Add Answer to:
QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Define a class ArraySet using an array that represents a set and implements the ADT Set....

    Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT