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

Write a C++ class (or a set of classes) that implements a stack using a linked l

ID: 3875122 • Letter: W

Question

Write a C++ class (or a set of classes) that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement at least the following methods:

.

· Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts.

· double pop (); // pops an element from the stack and returns its value. 5 pts.

· int isEmpty(); // returns 1 if the stack is empty, 0 otherwise. 5 pts.

· int numOfElements(); // returns the number of elements in the stack. 5 pts.

· void printElements(); // print out the current stack to the console. 5 pts.

· A main function; // execute each method of your stack (except the destructor) at least once without asking input from the users.

Explanation / Answer

#include #include class Node { protected: Node* next; double data; public: Node(double d){data=d;} friend class Stack; }; class Stack { public: Stack() {top->next=NULL; length=0;} ~Stack() { while(top!=NULL) { Node* toDelete=top; top=top->next; delete toDelete; } } void push(double d) { if (length==30) { print "Stack full"; return ; } Node *n=new Node(d); n->next=top; top=n; length++; } double pop() { Node* oldtop=top; top=top->next; double oldtopdata=oldtop->data; delete(oldtop); --length; return oldtopdata; } int isEmpty() { if numOfElements()>0 return 0; else return 1; } void printElements() { Node* current=top; while(current->next!=NULL) { cout
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