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

with button at the bottom of the lab. lab?\" Create a sorted linked list Additio

ID: 3812464 • Letter: W

Question

with button at the bottom of the lab. lab?" Create a sorted linked list Addition needs to add the node in the right position Lab 6 Submission 15.18.1 Activity Apr main Cppa 1 #include kiostream 2 using namespace std; 4 class IntNode public: IntNode IntNode (int d, IntNode n) data d; next int getData() return data 14 15 IntNode* getNext() return next 18 void setData (int d) data d; 22 void set Next (Int Node n) f Submit In "Develop" mode, you can run your program a Develop values (if desired) in the first box below, then cli second box. RUN PROGRAM Enter program input (optional)

Explanation / Answer

Here is the code for you:

#include <iostream>
using namespace std;
class IntNode {
public:
IntNode() {}
IntNode(int d, IntNode* n) {
data = d;
next = n;
}
  
int getData() {
return data;
}
IntNode* getNext() {
return next;
}
  
void setData(int d) {
data = d;
}
void setNext(IntNode* n) {
next = n;
}
private:
int data;
IntNode *next;
};
bool search(IntNode* head, int target);
void add(IntNode* &head, int value);
void deleteNode(IntNode* &head, int value);
void print(IntNode* head);
int main() {
IntNode* head = NULL;
char choice;
int value;
  
while(true){
cout<<"Please choose to Add(A), Delete(D), "<<
"Search(S), Print(P), or Quit(Q): ";
cin>>choice;
choice = tolower(choice);
switch(choice){
case 'a':
cout<<"Please input the number to add: ";
cin>>value;
add(head,value);
break;
case 'd':
cout<<"Please input the number to delete: ";
cin>>value;
deleteNode(head,value);
break;
case 's':
cout<<"Please input the number to search: ";
cin>>value;
cout<<"The number "<<value<<(search(head,value) ? " is " :
" is not ")<<"in the tree."<<endl;
break;
case 'p':
print(head);
break;
case 'q':
return 0;
default:
cout<<"Invalid input."<<endl;
break;
}
}
}

bool search(IntNode* head, int target)
{
    while(head != nullptr)
    {
       if(head->getData() == target)
           return true;
       head = head->getNext();  
    }
    return false;
}
void add(IntNode* &head, int value)
{
    IntNode *temp = new IntNode(value, nullptr);
    if(head == nullptr)
        head = temp;
    else if(head->getNext() == nullptr)
    {
       if(head->getData() < value)
           head->setNext(temp);
       else
       {
          temp->setNext(head);
          head = temp;
       }  
    }  
    else if(value < head->getData())
    {
       temp->setNext(head);
       head = temp;
    }
    else
    {
       IntNode* pass = head;
       while(pass->getNext()->getData() >= value)
           pass = pass->getNext();
       temp->setNext(pass->getNext());
       pass->setNext(temp);
    }
   
}
void deleteNode(IntNode* &head, int value)
{
    if(head == nullptr)
    {
       cout << "No nodes to delete->" << endl;
    }
    else if(head->getData() == value)
    {
       head = head->getNext();
    }
    else
    {
       IntNode *pass = head;
       while(pass->getNext() != nullptr && pass->getNext()->getData() < value)
           pass = pass->getNext();
       if(pass->getNext()->getData() == value)
           pass->setNext(pass->getNext()->getNext());
    }
}
void print(IntNode* head)
{
    while(head != nullptr)
    {
       cout << head->getData() << endl;
       head = head->getNext();
    }
}