Answer complete and correct to get 100% feedback!! I need typed answers. No use of C++ is required. Consider a stack imp
Posted: Mon May 02, 2022 12:48 pm
Answer complete and correct to get 100% feedback!! I
need typed answers. No use of C++ is required.
Consider a stack implemented using a dynamic array. The code is
attached. See that the Stack class has a private member called arr,
which is an object of type Array. In other words, Stack.arr in turn
has two private members, called els and capacity. The Stack class
has another private member called top.
Show the values of the three data members (top; the array
arr.els; arr.capacity) after each operation, as per the attached
code. The first two are shown for you. Each operation is done on
the result of the previous operation. [6 pts]
Operation 1: Stack s1;
arr.els:
arr.capacity:
2
top: -1
Operation 2: s1.push(5);
arr.els:
5
arr.capacity:
2
top: 0
Operation 3: (this is done after Operation 2, so start
from the result of Operation 2).
Confidence Level:
Confidence Level:
s1.push(7);
Operation 4: int x; s1.pop(x); (done after Operation
3)
Confidence Level:
Operation 5: s1.push(6); (done after Operation
4)
Confidence Level:
Operation 6: s1.push(10); (done after Operation
5)
Confidence Level:
Operation 7: int x; s1.pop(x); (done after Operation
6)
Confidence Level:
Operation 8: s1.makeEmpty(); (done after Operation
7)
Code:
Main:
#include
#include "StackArray.h"
using namespace std;
int main()
{
Stack myStack;
myStack.push(11);
myStack.push(12);
myStack.push(13);
int val;
cout << myStack.peek(val) << endl;
cout << val << endl;
cout << myStack.pop(val) << endl;
cout << val << endl;
cout << myStack.pop(val) << endl;
cout << val << endl;
cout << myStack.isEmpty() << endl;
myStack.makeEmpty();
cout << myStack.isEmpty() << endl;
return 0;
}
Dynamic Array:
#ifndef DYNAMICARRAY_H_INCLUDED
#define DYNAMICARRAY_H_INCLUDED
template
class Array {
public:
Array(int initSize);
Array(const Array & obj) { deepCopy(obj); }
Array & operator = (const Array & obj);
~Array() { makeEmpty(); }
T & operator [] (int index);
int length() const { return capacity; }
void changeSize(int newSize);
int errCode() const { return err; }
private:
T * els; // collection of elements of type
T
int capacity;
int err;
T dud;
void deepCopy(const Array & obj);
void makeEmpty() { delete [] els; }
};
template
Array::Array(int initSize) {
if (initSize < 1) { capacity = 1; err = 1; }
else { capacity = initSize; err = 0; }
els = new T [capacity];
}
template
Array & Array::operator = (const Array & obj) {
if (this == & obj) return (*this);
makeEmpty();
deepCopy(obj);
return (*this);
}
template
T & Array::operator [] (int index) {
if (index < 0 || index >= capacity) {
err |= 2; // err = err
| 2;
return dud;
}
return els[index];
}
template
void Array::changeSize(int newSize) {
if (newSize < 1) {
err |= 4;
return;
}
T * temp = new T[newSize];
int limit = (newSize < capacity) ? newSize :
capacity; // ternary operator..
boolExp ? exp1 : exp2
for (int i = 0; i < limit; i++)
temp = els;
delete [] els;
els = temp;
capacity = newSize;
}
template
void Array::deepCopy(const Array & obj) {
els = new T[obj.capacity];
capacity = obj.capacity;
err = obj.err;
dud = obj.dud;
for (int i = 0; i < capacity; i++)
els = obj.els;
}
#endif // DYNAMICARRAY_H_INCLUDED
Stack Array
#ifndef STACKARRAY_H_INCLUDED
#define STACKARRAY_H_INCLUDED
#include "DynamicArray.h"
template
class Stack {
public:
Stack() : arr(2) { top = -1; }
void push (const T & el); // O(1) steps --
amortized analysis
bool pop (T & el); // O(1) steps
bool peek (T & el); // O(1) steps
bool isEmpty() const { return (top == -1); } // O(1)
steps
void makeEmpty() { top = -1; }
private:
Array arr;
int top;
};
template
void Stack::push (const T & el) {
if (top >= arr.length() - 1) {
arr.changeSize(arr.length() *
2);
}
top++;
arr[top] = el;
}
template
bool Stack::pop (T & el) {
if (isEmpty()) return false;
el = arr[top];
top--;
return true;
}
template
bool Stack::peek (T & el) {
if (isEmpty()) return false;
el = arr[top];
return true;
}
#endif // STACKARRAY_H_INCLUDED
need typed answers. No use of C++ is required.
Consider a stack implemented using a dynamic array. The code is
attached. See that the Stack class has a private member called arr,
which is an object of type Array. In other words, Stack.arr in turn
has two private members, called els and capacity. The Stack class
has another private member called top.
Show the values of the three data members (top; the array
arr.els; arr.capacity) after each operation, as per the attached
code. The first two are shown for you. Each operation is done on
the result of the previous operation. [6 pts]
Operation 1: Stack s1;
arr.els:
arr.capacity:
2
top: -1
Operation 2: s1.push(5);
arr.els:
5
arr.capacity:
2
top: 0
Operation 3: (this is done after Operation 2, so start
from the result of Operation 2).
Confidence Level:
Confidence Level:
s1.push(7);
Operation 4: int x; s1.pop(x); (done after Operation
3)
Confidence Level:
Operation 5: s1.push(6); (done after Operation
4)
Confidence Level:
Operation 6: s1.push(10); (done after Operation
5)
Confidence Level:
Operation 7: int x; s1.pop(x); (done after Operation
6)
Confidence Level:
Operation 8: s1.makeEmpty(); (done after Operation
7)
Code:
Main:
#include
#include "StackArray.h"
using namespace std;
int main()
{
Stack myStack;
myStack.push(11);
myStack.push(12);
myStack.push(13);
int val;
cout << myStack.peek(val) << endl;
cout << val << endl;
cout << myStack.pop(val) << endl;
cout << val << endl;
cout << myStack.pop(val) << endl;
cout << val << endl;
cout << myStack.isEmpty() << endl;
myStack.makeEmpty();
cout << myStack.isEmpty() << endl;
return 0;
}
Dynamic Array:
#ifndef DYNAMICARRAY_H_INCLUDED
#define DYNAMICARRAY_H_INCLUDED
template
class Array {
public:
Array(int initSize);
Array(const Array & obj) { deepCopy(obj); }
Array & operator = (const Array & obj);
~Array() { makeEmpty(); }
T & operator [] (int index);
int length() const { return capacity; }
void changeSize(int newSize);
int errCode() const { return err; }
private:
T * els; // collection of elements of type
T
int capacity;
int err;
T dud;
void deepCopy(const Array & obj);
void makeEmpty() { delete [] els; }
};
template
Array::Array(int initSize) {
if (initSize < 1) { capacity = 1; err = 1; }
else { capacity = initSize; err = 0; }
els = new T [capacity];
}
template
Array & Array::operator = (const Array & obj) {
if (this == & obj) return (*this);
makeEmpty();
deepCopy(obj);
return (*this);
}
template
T & Array::operator [] (int index) {
if (index < 0 || index >= capacity) {
err |= 2; // err = err
| 2;
return dud;
}
return els[index];
}
template
void Array::changeSize(int newSize) {
if (newSize < 1) {
err |= 4;
return;
}
T * temp = new T[newSize];
int limit = (newSize < capacity) ? newSize :
capacity; // ternary operator..
boolExp ? exp1 : exp2
for (int i = 0; i < limit; i++)
temp = els;
delete [] els;
els = temp;
capacity = newSize;
}
template
void Array::deepCopy(const Array & obj) {
els = new T[obj.capacity];
capacity = obj.capacity;
err = obj.err;
dud = obj.dud;
for (int i = 0; i < capacity; i++)
els = obj.els;
}
#endif // DYNAMICARRAY_H_INCLUDED
Stack Array
#ifndef STACKARRAY_H_INCLUDED
#define STACKARRAY_H_INCLUDED
#include "DynamicArray.h"
template
class Stack {
public:
Stack() : arr(2) { top = -1; }
void push (const T & el); // O(1) steps --
amortized analysis
bool pop (T & el); // O(1) steps
bool peek (T & el); // O(1) steps
bool isEmpty() const { return (top == -1); } // O(1)
steps
void makeEmpty() { top = -1; }
private:
Array arr;
int top;
};
template
void Stack::push (const T & el) {
if (top >= arr.length() - 1) {
arr.changeSize(arr.length() *
2);
}
top++;
arr[top] = el;
}
template
bool Stack::pop (T & el) {
if (isEmpty()) return false;
el = arr[top];
top--;
return true;
}
template
bool Stack::peek (T & el) {
if (isEmpty()) return false;
el = arr[top];
return true;
}
#endif // STACKARRAY_H_INCLUDED