Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please follow steps accordingly and provide/label all files to run. Will rat

ID: 3731950 • Letter: C

Question

C++ Please follow steps accordingly and provide/label all files to run. Will rate thumbs up if everything is done correctly.

1. Create a completely templated array based stack. Make it so it works with linked lists to implement the stack.So instead of it being based off a dyanamic array, the stack will be based off a linked list. It should have two versions of the template stack. One based off arrays and one based off linked list.

2. Optional but appreciated - Change the linked list so it inherits from a static singly linked list class. Technically split the said stack into two classes -- one class is linked list & one which is not a stack.

Please comment clearly so i can follow along and recreate it the assignment myself. Having trouble following along with what its asking. Will update if you have any questions or not clear. Thank you

Explanation / Answer

// array based stack

#include <iostream>

#include <cstdlib>

using namespace std;

#define SIZE 15

template <class X>

class stack

{

X *arr;

int top;

int capacity;

public:

stack(int size = SIZE);

void push(X);

X pop();

X peek();

int size();

bool isEmpty();

bool isFull();

};

template <class X>

int stack<X>::size()

{

return top + 1;

}

template <class X>

bool stack<X>::isEmpty()

{

return top == -1;

}

template <class X>

bool stack<X>::isFull()

{

return top == capacity - 1;

}

template <class X>

stack<X>::stack(int size)

{

arr = new X[size];

capacity = size;

top = -1;

}

template <class X>

void stack<X>::push(X x)

{

if (isFull()) {

cout << "OverFlow ";

}

else {

cout << "Insert into stack " << x << endl;

arr[++top] = x;

}

}

template <class X>

X stack<X>::pop()

{

if (isEmpty()) {

cout << "UnderFlow ";

return "-1";

}

else {

cout << "Remove peek element from stack " << peek() << endl;

return arr[top--];

}

}

template <class X>

X stack<X>::peek()

{

if (!isEmpty())

return arr[top];

else

cout<<"Peek element doesn't exist"<<endl;

}

int main()

{

int N,id;

char ch;

string str;

// N=Maximum Element in stack if you chosse N>15 change SIZE at the top also

cout<<"Maximum Element in Stack"<<endl;

cin>>N;

stack<string> stk(N);

//Operations

do {

cout<<"Press/n1. push/n2. Pop"<<endl;

cin>>id;

switch(id){

case 1: {

cout<<"Enter String"<<endl;

cin>>str;

stk.push(str);

break;

}

case 2: {

stk.pop();

break;

}

default:

break;

}

cout<<"Want to continue ? press Y|y for 'YES'"<<endl;

cin>>ch;

} while(ch=='y'||ch=='Y');

cout << "Top element: " << stk.peek() << endl;

cout << "Stack size: " << stk.size() << endl;

if (stk.isEmpty())

cout << "Stack is empty ";

else

cout << "Stack is not empty ";

return 0;

}

//linked list based stack

// please check variable names and run it on gcc

//main.cpp

#include<bits/stdc++.h>
using namespace std;
#include "stack.h"
int main() {

int id;
stack<string> A;
stack<string> B;

while( 1 )

{

cout << "1. Push an item into stack A "

<< "2. Pop an item from Stack A "

<< "3. Print top of Stack A "

<< "4. Push an item into Stack B "

<< "5. Pop an item from Stack B "

<< "6. Assign B = A "

<< "0. Exit ";

cin >> id;

switch( id ){

case 1: {

string str;

cout << "Enter the string: ";
cin >> str;
A.push( str );

break;

}

case 2: {

try {

A.pop();

}

catch ( stack_empty ) {

cout << "stack A is already empty ";

}

break;

}

case 3: {

try {

string result = A.top();

cout << "top = " << result << " ";

}

catch ( stack_empty ) {

cout << "stack A is empty! ";

}

break;

}

case 4:{

string str;

cout << "Enter the string:";
cin >> str;
B.push( str );

break;

}

case 5: {

try {

B.pop();

}

catch ( stack_empty ) {

cout << "stack B is already empty! ";

}

break;

}

case 6:

{

B = A;

cout << "stack A has: " << A.size() << " element(s). ";

cout << "stack B has: " << B.size() << " element(s). ";

break;

}

default:

{

exit();

break;

}

}

}

return 0;

}

//stack.h

#ifndef STACK_H

#define STACK_H

#include <stdexcept>

using namespace std;

class stack_empty : public logic_error

{

public:

stack_empty() : logic_error( "stack is empty!" ) {};

};

template <class Item>

class stack

{

public:

stack();

stack( const stack& originalStack );

~stack();

const stack& operator=( const stack& otherstack );

void push( const Item& entry );

void pop( );

Item& peek();

int size() const;

private:

struct stacknode

{

Item item;

stacknode* next;

};

stacknode* list;

int currentSize;

void copy( const stack& otherstack );

};

#endif

//stack.cpp

#include<bits/stdc++.h>

using namespace std;

template <class Item>

stack<Item>::stack(){

currentSize = 0;

list = NULL;

}

template <class Item>

stack<Item>::stack( const stack<Item>& originalStack ){

copy( originalStack );

}

template <class Item>

stack<Item>::~stack(){

stacknode* target;

while( list != NULL)

{

target = list;

list = list -> next;

delete target;

}

}

template <class Item>

const stack<Item>& stack<Item>::operator=( const stack<Item>& otherstack )

{

if ( &otherstack != this )

{

stacknode* target;  

while( list != NULL)

{

target = list;

list = list -> next;

delete target;

}

copy( otherstack );

}

return *this;

}

template <class Item>

void stack<Item>::push( const Item& item )

{

stacknode* newNode = new stacknode;

newNode->item = item;

newNode->next = list;

list = newNode;

currentSize++;

}

template <class Item>

void stack<Item>::pop()

{

if (list == NULL){

throw stack_empty();

}

else

{

stacknode* ptr = list;

list = list->next;

delete ptr;

currentSize--;

}

}

template <class Item>

Item& stack<Item>::peek()

{

if (list == NULL)

{

throw stack_empty();

}

else

{

return list->item;

}   

}

template <class Item>

int stack<Item>::size() const

{

return currentSize;

}

template <class Item>

void stack<Item>::copy( const stack<Item>& otherstack )

{

stacknode* ptr;   

stacknode* last;   

stacknode* newNode;  

currentSize = otherstack.currentSize;

list = NULL;

last = NULL;

for( ptr = otherstack.list; ptr != NULL; ptr = ptr->next )

{

newNode = new stacknode;

newNode->item = ptr->item;

newNode->next = NULL;

if (list == NULL)

{

list = newNode;

last = newNode;

}

else

{

last->next = newNode;

last = newNode;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote