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

DO WORK this Write a C++ class that implements a stack using a linked list. The

ID: 3875596 • Letter: D

Question

DO WORK

this Write a C++ class 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 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<iostream>
using namespace std;

struct node
{
double data;
struct node *next;
};

class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push (double value);
double pop ();
void show();
int isEmpty();
int numOfElements();
void printElements();
};

void stack::push(double val)
{
int value;
struct node *ptr;
ptr=new node;
ptr->data=val;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<" Value inserted successfully";
}

double stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<" The stack is empty!!!";
}
temp=top;
top=top->next;
delete temp;
return temp->data;
}

void stack::printElements()
{
struct node *ptr=top;
cout<<" Elements is stack is: ";
while(ptr!=NULL)
{
cout<<ptr->data<<" -> ";
ptr=ptr->next;
}
cout<<"NULL";
}

int stack::numOfElements(){
if(top == NULL){
return 0;
}
struct node *ptr=top;
int count = 0;
while (ptr!=NULL){
count++;
}
return count;
}

int stack::isEmpty(){
if(top == NULL)
return 1;
else
return 0;
}

int main()
{
stack s;
s.push(1.0);
s.push(2.0);
s.push(3.0);
s.pop();

s.printElements();
return 0;
}