Question

1. Here are codes to define a stack class based on dynamic array, please complete the...

  1. 1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor

//--- Definition of Stack copy constructor

Stack::Stack(const Stack & original)

: myCapacity(original.myCapacity), myTop(original.myTop)

{

//--- Get new array for copy

myArray = new(nothrow) StackElement[myCapacity];

if (myArray != 0) // check if memory available

                        // copy original's array member into this new array

{    

         // Please complete the function here

       }

else

{

         cerr << "*Inadequate memory to allocate stack ***\n";

         exit(1);

}

}

Note: to assistant you to fill the above function, Stack class declaration is attached as following.

/*-- DStack.h ---------------------------------------------------

This header file defines a Stack data type.

Basic operations:

This header file defines a Stack data type.

Basic operations:

constructor: Constructs an empty stack

empty:        Checks if a stack is empty

push:         Modifies a stack by adding a value at the top

top:          Retrieves the top stack value; leaves stack unchanged

pop:          Modifies stack by removing the value at the top

display:      Displays all the stack elements

Class Invariant:

1. The stack elements (if any) are stored in positions

0, 1, . . ., myTop of myArray.

2. -1 <= myTop < STACK_CAPACITY

--------------------------------------------------------------*/

#include <iostream>

using namespace std;

#ifndef DSTACK

#define DSTACK

typedef int StackElement;

class Stack

{

public:

/***** Function Members *****/

/***** Constructors *****/

Stack(int numElements = 128);

/*------------------------------------------------------------------------

Construct a Stack object.

Precondition: None.

Postcondition: An empty Stack object has been constructed (myTop is

initialized to -1 and myArray is an array with numElements

(default 128) elements of type StackElement).

------------------------------------------------------------------------*/

Stack(const Stack & original);

/*------------------------------------------------------------------------

Copy Constructor

Precondition: original is the stack to be copied and is received as

a const reference parameter.

Postcondition: A copy of original has been constructed.

------------------------------------------------------------------------*/

/***** Destructor *****/

~Stack();

/*-----------------------------------------------------------------------

Class destructor

Precondition: None

Postcondition: The dynamic array in the stack has been deallocated.

------------------------------------------------------------------------*/

/***** Assignment *****/

const Stack & operator= (const Stack & rightHandSide);

/*------------------------------------------------------------------------

Assignment Operator

Precondition: rightHandSide is the stack to be assigned and is

received as a const reference parameter.

Postcondition: The current stack becomes a copy of rightHandSide

and a const reference to it is returned.

------------------------------------------------------------------------*/

bool empty() const;

/*------------------------------------------------------------------------

Check if stack is empty.

Precondition: None

Postcondition: Returns true if stack is empty and false otherwise.

-----------------------------------------------------------------------*/

void push(const StackElement & value);

/*------------------------------------------------------------------------

Add a value to a stack.

Precondition: value is to be added to this stack

Postcondition: value is added at top of stack provided there is space;

otherwise, a stack-full message is displayed and execution is

terminated.

-----------------------------------------------------------------------*/

void display(ostream & out) const;

/*------------------------------------------------------------------------

Display values stored in the stack.

Precondition: ostream out is open.

Postcondition: Stack's contents, from top down, have been output to out.

-----------------------------------------------------------------------*/

StackElement top() const;

/*------------------------------------------------------------------------

Retrieve value at top of stack (if any).

Precondition: Stack is nonempty

Postcondition: Value at top of stack is returned, unless the stack is

empty; in that case, an error message is displayed and a "garbage

value" is returned.

-----------------------------------------------------------------------*/

void pop();

/*------------------------------------------------------------------------

Remove value at top of stack (if any).

Precondition: Stack is nonempty.

Postcondition: Value at top of stack has been removed, unless the stack

is empty; in that case, an error message is displayed and

execution allowed to proceed.

-----------------------------------------------------------------------*/

private:

/***** Data Members *****/

int myCapacity,           // capacity of stack

         myTop;                // top of stack

StackElement * myArray;   // dynamic array to store elements

}; // end of class declaration

#endif

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

// do comment if any problem arises
// code
#include <iostream>
using namespace std;
//--- Definition of Stack copy constructor
Stack::Stack(const Stack& original)
   : myCapacity(original.myCapacity), myTop(original.myTop)
{
   //--- Get new array for copy
   myArray = new (nothrow) StackElement[myCapacity];
   if (myArray != 0) // check if memory available
   // copy original's array member into this new array
   {
       // Please complete the function here
       //iterate over original array
       for (int i = 0; i < myCapacity; i++)
           // assign each element of original array to current myarray
           myArray[i] = original.myArray[i];
   }
   else
   {
       cerr << "*Inadequate memory to allocate stack ***\n";
       exit(1);
   }
}

Add a comment
Know the answer?
Add Answer to:
1. Here are codes to define a stack class based on dynamic array, please complete the...
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
  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • 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...

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