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

can someone please look at what I have competed on this and correct anything tha

ID: 3669283 • Letter: C

Question

can someone please look at what I have competed on this and correct anything that is wrong as well as fill in whats missing? Thank you in advance!

This is what i have to do:

Before exercise, please introduce a Node class that stores integer. Exercise Summary:

1. Introduce bool find(Node*front, int input) function. If there is a node that has input value in the singly linked list, it will return true. Otherwise, it will return false.

2. Introduce Node* insertInOrder(Node* front, int input) function. When this function is called, a new node with the input value will be inserted into the singly linked list in order (e.g., 2->5->7->9->NULL)

3. Introduce Node* deleteMiddle(Node* front). When the function is called the middle node (size/2-th for both odd and even number of nodes) will be deleted.

4. Introduce Node* deleteFront(). When the function is called, the front node will be deleted. Or it will do nothing if no front is available.

5. Introduce Node* deleteTarget(Node* front, int target). When the function is called, the node with target value will be deleted. Or it will do nothing if no front is available.

6. Introduce Node* print(Node* front) function that will print out all integer values of the singly linked list. Please return front for all the functions that have Node* as return type. Then please make sure you invoke all functions in main function.

and this is what I've got

#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node* next;
};
bool find(Node*front, int input){
   Node *curr= front;
   while(curr != NULL){
       if (curr->data == input){
       return 0;}
       else{
           curr=curr->next;
       }
           }
   };
Node*insertInOrder(Node* front, Node* newItem){
Node* p = front;
if (p==NULL) {
front = newItem;
return front;
}
if(p->data > newItem->data) {
newItem->next = p;
front = newItem;
return front;
}
while(p->next != NULL && p->next->data < newItem->data) {
p = p->next;
}
newItem->next = p->next;
p->next = newItem;
return front;
}
/*Node*p = front;
if (front == NULL) { front = newItem; return front; }
if(p->data > newItem->data) {
   newItem->next = p;
   front = newItem;
   return front;
}
while(p->next != NULL && p->next->data < newItem->data) {
   p = p->next;
}
newItem->next = p->next;
p->next = newItem;
}
/*Node* deleteMiddle(Node* front){
  
  
};*/

Node*deleteFront(Node*front){
Node*p;
while (front != NULL){
p = front;
front = front->next;
delete p;}
};

Node*deleteTarget(Node* front, int target){
Node*p = front;
if(p->data == target) {
front = p->next;
delete p;
}
p = front;
while(p->next != NULL) {
Node *curr = p->next;
if(curr->data == target) {
   p->next = curr->next;
   delete curr;
}
else p = p->next;
}

};

Node*print(Node* front){
Node* p;
p = front;
while(p->next != NULL) // while ( p != NULL)
{
   cout<<p->data<<" ";
   p=p->next;
}
}

int main() {
Node* front = NULL;
front = insertInOrder(front, 5);
front = insertInOrder(front, 3);
front = insertInOrder(front, 7);
front = insertInOrder(front, 9);
front = insertInOrder(front, 3);
front = insertInOrder(front, 3);

front = print(front);
  
cout<<find(front,7)<<endl;
cout<<find(front, 10)<<endl;
  
//cout<<size(front)<<endl;
front = print(front);
front = deleteMiddle(front);
front = print(front);

//front = deleteMiddle(front);
front = print(front);
  
front = deleteTarget(front, 3);
front = print(front);
return 0;
}

Explanation / Answer

Hi I have corrected your code.

#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
bool find(Node*front, int input){
Node *curr= front;
while(curr != NULL){
if (curr->data == input){
           return true;
           }
curr=curr->next;
  
}
return false;
};
Node* insertInOrder(Node* head_ref, int ele){
   Node* current;
   Node* new_node = new Node();
   new_node->data = ele;
/* Special case for the head end */
if (head_ref == NULL || head_ref->data >= new_node->data)
{
new_node->next = head_ref;
head_ref = new_node;
return head_ref;
}
else
{
/* Locate the node before the point of insertion */
current = head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
return head_ref;
}
}

Node*deleteFront(Node*front){
   Node*p;
   if(front == NULL)// list is empty
       return front;
   while (front != NULL){
       p = front;
       front = front->next;
       delete p;
   }
   return front;
};
Node* deleteTarget(Node* head_ref, int key){
       // Store head node
Node* temp = head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == key)
{
head_ref = temp->next; // Changed head
delete temp; // free old head
return head_ref;
}

// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}

// If key was not present in linked list
if (temp == NULL)
       return head_ref;

// Unlink the node from linked list
prev->next = temp->next;

delete temp; // Free memory
return head_ref;
};
void print(Node* front){
   Node* p;
   p = front;
   while(p != NULL) // while ( p != NULL)
   {
       cout<<p->data<<" ";
       p=p->next;
   }
}
int main() {
Node* front = NULL;
front = insertInOrder(front, 5);
front = insertInOrder(front, 3);
front = insertInOrder(front, 7);
front = insertInOrder(front, 9);
front = insertInOrder(front, 3);
front = insertInOrder(front, 3);
print(front);
  
cout<<find(front,7)<<endl;
cout<<find(front, 10)<<endl;
  
//cout<<size(front)<<endl;
print(front);
   print(front);
//front = deleteMiddle(front);
print(front);
  
front = deleteTarget(front, 3);
print(front);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote