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

Code in C++ ~Create a sorted linked list ~Addition needs to add the node in the

ID: 3812204 • Letter: C

Question

Code in C++

~Create a sorted linked list

~Addition needs to add the node in the right position

#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;
       }
   }
}

1 #include kiostream using namespace std; 4 class IntNode public: Int Node Int Node (int d, Int Node* n) data d; ext. 10 int getData 12 13 return data; 14 15 E IntNode getNext 16 return next; 17 18 void setData (int d) 19 20 data d 21 22 E void set Next (IntNode* n) 23 ext. 26 private: 27 int data; IntNode *n. ext. 28 29 30 31 bool search (IntNode* ead int target) 32 void add (IntNode* head, int value) 33 void deleteNode (IntNode* &head;, int value) 34 void print (IntNode* ead) 35 36 E int main IntNode ead NULL 37 38 char choice; int value 39 40

Explanation / Answer

Here are the function completed 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();
    }
}

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