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.
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;
}
C++ Write an implementation of the ADT stack that uses a resizable array (vector class of...
C++ Question: Write an implementation of the ADT list (list class of C++ STL) that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array. In other words, create an object of type list and fill this list with resizable arrays (vectors).
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...
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...
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 STL stack). b) Calculate the complexity of your algorithm.
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...
Write an Assembly Language program StackReverse which uses the runtime stack to reverse an array Vector of N unsigned double-word integers.
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...
Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++. Assignment In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type. A driver program is provided for this assignment to test your implementation. You don't have to write the tests. Program You will need to write a single template class for this...
- 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 {...