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

Use a linked list to implement this. 2. Add functionality for Mult, Div, Pop, an

ID: 3718215 • Letter: U

Question

Use a linked list to implement this.

2. Add functionality for Mult, Div, Pop, and Display.

3. Create a looping menu-driven program to demonstrate your code so that the user has the choice of:

1. Push (an integer onto the stack)

2. Pop (an integer off the stack)

3. Add (the top two values in the stack and replace those two values with the sum)

4. Subtract (the top two values on the stack and replace those two values with the difference)

5. Mult (same as above)

6. Div (same as above but check for div by 0)

7. Display Stack

8. End


************************************************************

1. Please Help me to diveded this porgram to 3 part that can run it : *****.ccp, *****.h , main.cpp ?

/***********************************************************

#include <iostream>

using namespace std;

//Node for stack
struct Node
{
int data;
Node *next;
};

//Push function of stack
Node* push(Node* head, int data)
{
//Creating a new node with given data
Node *temp=new Node;
temp->data=data;
temp->next=head;

return temp; //Returning the new head;
}

//Pop function of stack
Node* pop(Node* head)
{
if(head==NULL)
return NULL;
//Popping the current head
Node *temp=head;
head=head->next;
delete temp;

return head; //Returning the new head
}

//Function to add 2 top values
void Add(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data+head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to subtract 2 top values
void Subtract(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data-head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to multiply 2 top values
void Mult(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data*head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to divide 2 top values
void Div(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
if(head->next->data==0)
return;
int num=head->data/head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to print all values
void print(Node* head)
{
Node *trav=head;
while(trav!=NULL) //Traversing through the stack
{
cout<<trav->data<<" ";
trav=trav->next;
}
cout<<" ";
return;
}

int main()
{
int res,a;
Node *head=NULL;
do //Menu driving loop
{
cout<<"What do you want to do? 1.Push 2.Pop 3.Add 4.Subtract 5.Mult 6.Div 7.Display Stack 8.End Choice: ";
cin>>res;
switch(res)
{
//Push function
case 1: cout<<"Enter the number you want to push: ";
cin>>a;
head=push(head,a);
break;
  
//Pop function
case 2:
head=pop(head);
break;
  
//Add function
case 3:
Add(head);
break;
  
//Subtract function
case 4:
Subtract(head);
break;
  
//Multiply function
case 5:
Mult(head);
break;
  
//Divide function
case 6:
Div(head);
break;
  
//Printing the stack
case 7:
print(head);
break;
  
//End
case 8: break;
default:cout<<"Invalid code ";
}
}while(res!=8);
return 0;
}

Explanation / Answer

The three files would be

main.cpp

#include "link.h"

int main()
{
int res,a;
Node *head=NULL;
do //Menu driving loop
{
cout<<"What do you want to do? 1.Push 2.Pop 3.Add 4.Subtract 5.Mult 6.Div 7.Display Stack 8.End Choice: ";
cin>>res;
switch(res)
{
//Push function
case 1: cout<<"Enter the number you want to push: ";
cin>>a;
head=push(head,a);
break;

//Pop function
case 2:
head=pop(head);
break;

//Add function
case 3:
Add(head);
break;

//Subtract function
case 4:
Subtract(head);
break;

//Multiply function
case 5:
Mult(head);
break;

//Divide function
case 6:
Div(head);
break;

//Printing the stack
case 7:
print(head);
break;

//End
case 8: break;
default:cout<<"Invalid code ";
}
}while(res!=8);
return 0;
}

link.h

#include <iostream>

using namespace std;

//Node for stack
struct Node
{
int data;
Node *next;
};
Node* push(Node* head, int data);
Node* pop(Node* head);
void Add(Node *head);
void Subtract(Node *head);
void Mult(Node *head);
void Div(Node *head);
void print(Node* head);

link.cpp

#include "link.h"
//Push function of stack
Node* push(Node* head, int data)
{
//Creating a new node with given data
Node *temp=new Node;
temp->data=data;
temp->next=head;

return temp; //Returning the new head;
}

//Pop function of stack
Node* pop(Node* head)
{
if(head==NULL)
return NULL;
//Popping the current head
Node *temp=head;
head=head->next;
delete temp;

return head; //Returning the new head
}

//Function to add 2 top values
void Add(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data+head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to subtract 2 top values
void Subtract(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data-head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to multiply 2 top values
void Mult(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
int num=head->data*head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to divide 2 top values
void Div(Node *head)
{
if(head==NULL||head->next==NULL) //Checking for 2 nodes
return ;
if(head->next->data==0)
return;
int num=head->data/head->next->data;
head->data=num;
head->next->data=num;
return;
}

//Function to print all values
void print(Node* head)
{
Node *trav=head;
while(trav!=NULL) //Traversing through the stack
{
cout<<trav->data<<" ";
trav=trav->next;
}
cout<<" ";
return;
}

Do give a thumbs up