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;
}
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';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.