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

LinkedList.h ~~~~~~~~~~~~~~~~ #ifndef LINKEDLIST_H #define LINKEDLIST_H using na

ID: 3574203 • Letter: L

Question

LinkedList.h

~~~~~~~~~~~~~~~~

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

using namespace std;

// Representation of an element in the linked list
template<class T>
struct Node
{
T val; // Value of the node
Node *next; // Pointer to the next node
};

template<class T>
class LinkedList
{
// Public Functions/Variables
public:
/* IMPLEMENT THESE FUNCTIONS FOR EXERCISE1 */
LinkedList(); // Constructor
~LinkedList(); // Destructor
void insertAtBack(T valueToInsert);
bool removeFromBack();
void print();
bool isEmpty();
int size();
void clear();

/* IMPLEMENT THSES FUNCTIONS FOR EXERCISE2 */
void insertAtFront(T valueToInsert);
bool removeFromFront();

Node<T> *first; // Pointer pointing to the begining of the list
Node<T> *last; // Pointer pointing to the end of the list
};

// Implement your functions here:

#endif

Stack.h from lab 9

~~~~~~~~~~~~~~~~~~~

#ifndef STACK_H#define STACK_H
#include "LinkedList.h"

using namespace std;
class Stack : public LinkedList   

{

public:Stack();

                 ~Stack();           

       void push(int value);

int pop();int& top();

};#endif

Exercise2.cpp

~~~~~~~~~~~~~~~~~~

#include <iostream>
#include "LinkedList.h"
#include "Stack.h"

using namespace std;

int main()
{
Stack<char> s1;                   //create stack
Stack<int> s2; //create stack

cout << "Testing Stack #1: " << endl;
   cout << "isEmpty(): ";           //call isEmpty
   if(s1.isEmpty()){
       cout << "true" << endl;
   } else {
       cout << "false" << endl;
   }

   cout << "push(A) Stack contents:";       //push A into stack
   s1.push('A');
   s1.print();
   cout << endl;

   cout << "push(Y) Stack contents:";       //push Y into stack
   s1.push('Y');
   s1.print();
   cout << endl;

   cout << "Size(): ";               //get size of stack
   cout << s1.size();
   cout << " Stack contents: ";
   s1.print();
   cout << endl;

   cout << "Pop(): " << s1.pop() << " Stack contents: ";
   s1.print();                   //pop out the top of the stack
   cout << endl;

   cout << "isEmpty(): ";           //call isEmpty
   if(s1.isEmpty()){
       cout << "true" << endl;
   } else {
       cout << "false" << endl;
   }

   cout << "push(D) Stack contents:";       //push D into stack
   s1.push('D');
   s1.print();
   cout << endl;

   cout << "Top(): " << s1.top() << " Stack contents: ";
   s1.print();                   //get the top of the stack
   cout << endl;

   cout << "push(T) Stack contents:";
   s1.push('T');
   s1.print();
   cout << endl;

   cout << "Pop(): " << s1.pop() << " Stack contents: ";
   s1.print();
   cout << endl;


cout << endl;
cout << "Testing Stack #2: " << endl;
cout << "isEmpty(): ";           //call isEmpty
   if(s2.isEmpty()){
       cout << "true" << endl;
   } else {
       cout << "false" << endl;
   }

   cout << "push(1) Stack contents:";       //push A into stack
   s2.push(1);
   s2.print();
   cout << endl;

   cout << "push(5) Stack contents:";       //push Y into stack
   s2.push(5);
   s2.print();
   cout << endl;

   cout << "Size(): ";               //get size of stack
   cout << s1.size();
   cout << " Stack contents: ";
   s2.print();
   cout << endl;

   cout << "Pop(): " << s2.pop() << " Stack contents: ";
   s2.print();                   //pop out the top of the stack
   cout << endl;

   cout << "isEmpty(): ";           //call isEmpty
   if(s2.isEmpty()){
       cout << "true" << endl;
   } else {
       cout << "false" << endl;
   }

   cout << "push(10) Stack contents:";       //push D into stack
   s2.push(10);
   s2.print();
   cout << endl;

   cout << "Top(): " << s2.top() << " Stack contents: ";
   s2.print();                   //get the top of the stack
   cout << endl;

   cout << "push(-5) Stack contents:";
   s2.push(-5);
   s2.print();
   cout << endl;

   cout << "Pop(): " << s2.pop() << " Stack contents: ";
   s2.print();
   cout << endl;

return 0;
}

The size of the first list is: 0 The size of the second list is: 0 The first list is empty! The second list is empty (Exercise 2) Stack In this part of the lab, you will convert the Stack.h created in Lab #9 and use it with the LinkedList created from Exercise 1. Follow the example from the LinkedList class and implement the Stack as a template class. You can copy your implementations of Stack from Lab #9 and make the changes accordingly. Again, you must implement all the functions within the same Stack.h Test your Stack with Exercise2.cpp (on CatCourses) and compare the output with the sample bellow: Example2: Testing Stack #1: isEmpty(): true push(A) Stack contents:A push (Y) Stack contents:Y,A Size(): 2 Stack contents: Y,A Pop Y Stack contents: A isEmpty(): false push(D) Stack contents :D,A Top D Stack contents: D,A push (T) Stack contents:T, D,A Pop T Stack contents: D,A Testing Stack #2: isEmpty(): true push (1) Stack contents: 1 push (5) Stack contents:5,1 Size(): 2 Stack contents: 5,1 Pop 5 Stack contents: 1 isEmpty(): false push (10) Stack contents: 10,1 Top 10 Stack contents: 10,1 push (-5) Stack contents: -5,10,1 Pop -5 Stack contents: 10,1

Explanation / Answer

templateclass T1

templateclass T1

struct Node{

T1 val;
Node *next;

Node( T1 t = { },Node link = nullptr) item{t} , next{link} { }    //here constructor fro linked list function

~Node() { delete next ; }    //destructor for inked list function

};

template<class T1>
class stack{
public:
bool isEmpty() const{return n == 0; }
int size();
void clear();

void push(const T1&);

T1 pop();

T1 peek();

private:

Nodet1 first;

stdsize_t n;

};

templateclass t1

void stackT1push(const T1& t) {

NodeT1 old{first};

first = new Node{t,old};

++n;

}

templateclass T1

T1 stackpop(){

if(empty()){

throw stdout_of_range(underflow);

}

Node<T1>*oldnode = first;

T1 t = first->item;

first= first->next;

--n;

delete oldnode;

retun t;

}

templateclass T1

T1 stackTpeek(){

if(empty(){

thow stdout_of_range(underflow);

}

return first-item;

}

int main(){

Stack<<std::int> y{];

y.push(1);

y.push(2);

y.push(3);

stack<<std::int> z = y;

z.pop();

std::cout<<y.peek() << '/n';

z.pop();

std::cout<<y.peek() << '/n';

z.push(7);

std::cout<<y.peek() << '/n';

std::cout<<y.size() << '/n';

std::cout<<z.size() << '/n';

}

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