Question

C++ Write an implementation of the ADT stack that uses a resizable array (vector class of...

C++
Write an implementation of the ADT stack that uses a resizable array (vector class of C++ STL) to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack’s bottom entry at the beginning of the array the same way we did in array-based implementation.

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

ANSWER:

GIVEN THAT:

#include <iostream>
#include <vector>

using namespace std;

class Stack {
private:
   vector<int> vec;
public:
   void push(int n) {
      vec.push_back(n);
   }
   
   int pop() {
      int n = vec[vec.size()-1];
      vec.pop_back();
      return n;
   }
   
   int top() {
      return vec[vec.size()-1];
   }
   
   bool isEmpty() {
      return vec.empty();
   }
   
   int size() {
      return vec.size();
   }
};

int main() {
   Stack s;
   for(int i = 0; i < 5; ++i) {
      s.push(i);
   }
   cout << "Stack contains" << endl;
   while(!s.isEmpty()) {
      cout << s.pop() << endl;
   }
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ Write an implementation of the ADT stack that uses a resizable array (vector class of...
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
  • 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...

  • Write a Java program to work with a generic list ADT using a fixed size array,...

    Write a Java program to work with a generic list ADT using a fixed size array, not ArrayList. Create the interface ListInterface with the following methods a) add(newEntry): Adds a new entry to the end of the list. b) add(newPosition, newEntry): Adds a new entry to the list at a given position. c) remove(givenPosition): Removes the entry at a given position from the list. d) clear( ): Removes all entries from the list . e) replace(givenPosition, newEntry): Replaces the entry...

  • C++ ONLY a) Write a non-recursive implementation of Fibonacci function that uses stack (you can use...

    C++ ONLY a) Write a non-recursive implementation of Fibonacci function that uses stack (you can use STL stack). b) Calculate the complexity of your algorithm.

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

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class...

    (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class a different name, since it will be different from the latest version that uses a static array. Start off with a dynamic array size of 2, made by the constructor. Every time the dynamic array becomes full and a new check has to be written, double the current size of the array. Write a doubleArray function for the class to do this, but place...

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

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25...

    Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of Person objects, where each object will contain a Social Insurance Number (validate this), a first name, a last name, a gender and a data of birth. This implementation should prevent duplicate entries – that is, the Social Insurance...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

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