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

Write the member functions: void Delete(int data) which delete the node with dat

ID: 3769651 • Letter: W

Question

Write the member functions: void Delete(int data) which delete the node with dataVal == data in the list if it exists in the list void Add_End() which adds a node to the end of the list void Delete_Front() which deletes the first node in the list if there is a first node Please use this code:

#include <iostream>
using namespace std;


// Node class
class Node {
int data;
Node* next;

public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};

// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Add_End(int data);
void Delete(int data);

void Delete_Front();

void Add_Front(int data);

void Delete_End();
Node* Find(int data);
void Print();

}
//FUNCTION DEFINITIONS: -

//YOU MUST WRITE THESE
//Add_End, Delete, Delete_Front

/* Append a node to END of the linked list
*/
void List::Add_End(int data) {
/*
FILL IN CODE
*/
}
/* Delete a node from the list */

void List::Delete(int data) {
/*
FILL IN CODE
*/

}

void List::Delete_Front()
{
/*
FILL IN CODE
*/
  
}

void List::Add_Front(int data)
{
   // Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;

}
  
void List::Delete_End()
{
   if(head == NULL)
   {
      cout<<"List has no member so cannot delete end"<<endl;
return;     
   }
     
   // check if one in length
   if(head->Next() == NULL)
{
    head = NULL;
    return;
   }
// 2 or greater in length

Node *current;
Node *prev;
prev = head;
   for(current = head->Next(); current->Next() != NULL; current = current->Next())     
   {
      prev = current;
       }
   prev->SetNext(NULL);
   return;
           }
  
Node* List::Find(int data)
{
   Node *current;
  
   for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{}
if(current == NULL)
{
    cout<<"Did not find "<<data<<"."<<endl;
    return NULL;
       }
       else // found
       {
          cout<<"Found "<<data<<"."<<endl;
          return current;
       }
   }
int main()
{
// New list
List list;
Node *answer;
// Add_End nodes to the list
list.Add_End(111);
list.Print();
list.Add_End(222);
list.Print();
list.Add_End(333);
list.Print();
list.Add_End(444);
list.Print();
list.Add_End(555);
list.Print();

// Delete nodes from the list
list.Delete(444);
list.Print();
list.Delete(333);
list.Print();
list.Delete(222);
list.Print();
list.Delete(555);
list.Print();
list.Delete(111);
list.Print();
cout<<"Testing Add_Front: and others"<<endl;
list.Add_Front(888);
list.Print();
   list.Add_Front(999);
   list.Print();
   list.Add_Front(49);
   list.Print();
   cout<<"Checking find function"<<endl;
   answer = list.Find(888);
   cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
   cout<<"Checking find function"<<endl;
   answer = list.Find(999);
   cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
  
   cout<<"Checking find function"<<endl;
   answer = list.Find(49);
   cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
   cout<<"Call find function with value not in list."<<endl;
   answer = list.Find(7);
if(answer == NULL)
{
    cout<<"returned null pointer since 7 not found"<<endl;  
   }
   else
   {
      cout<< "in else of answer == NULL where Value for node returned by find function call with 7 is "<<answer->Data()<<"."<<endl;
   }  
   cout<<"testing delete_front: "<<endl;
   list.Delete_Front();
   list.Print();
   cout<<"testing delete_end: "<<endl;
   list.Delete_End();
   list.Print();
system("Pause");
return 0;

Explanation / Answer

As per your requirement I have filled the blanks.

#include <iostream>
using namespace std;

// Node class
class Node {
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Add_End(int data);
void Delete(int data);
void Delete_Front();

void Add_Front(int data);
void Delete_End();
Node* Find(int data);
void Print();
}
//FUNCTION DEFINITIONS: -
//YOU MUST WRITE THESE
//Add_End, Delete, Delete_Front
/* Append a node to END of the linked list
*/
void List::Add_End(int data) {
/*
FILL IN CODE
*/
}
/* Delete a node from the list */

void List::Delete(int data) {
/*
FILL IN CODE
*/
nodetype *current, **previous; // pointer *s are connected to vars
// blank line between section
previous = start;
current = *start;
// blank line between section
// I use blank lines a lot, they help
// me to organize my thoughts

while((current != NULL) && (akey != current->adata))
{ // indentation inside nested scope
previous = &current->ptr; // no space for unary operators like &
current = current->ptr; // assignments justified to same level
}

if (current != NULL)
{
*previous = current->ptr; // no space for unary *, space for =
delete current;
}
// more blank lines between sections
return;
}


}
void List::Delete_Front()
{
/*
FILL IN CODE
*/
previous = &current->ptr;
current = current->ptr;
}
void List::Add_Front(int data)
{
// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;
}
  
void List::Delete_End()
{
if(head == NULL)
{
cout<<"List has no member so cannot delete end"<<endl;
return;   
}

// check if one in length
if(head->Next() == NULL)
{
head = NULL;
return;
}
// 2 or greater in length
Node *current;
Node *prev;
prev = head;
for(current = head->Next(); current->Next() != NULL; current = current->Next())   
{
prev = current;
}
prev->SetNext(NULL);
return;
}
  
Node* List::Find(int data)
{
Node *current;
  
for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{}
if(current == NULL)
{
cout<<"Did not find "<<data<<"."<<endl;
return NULL;
}
else // found
{
cout<<"Found "<<data<<"."<<endl;
return current;
}
}
int main()
{
// New list
List list;
Node *answer;
// Add_End nodes to the list
list.Add_End(111);
list.Print();
list.Add_End(222);
list.Print();
list.Add_End(333);
list.Print();
list.Add_End(444);
list.Print();
list.Add_End(555);
list.Print();
// Delete nodes from the list
list.Delete(444);
list.Print();
list.Delete(333);
list.Print();
list.Delete(222);
list.Print();
list.Delete(555);
list.Print();
list.Delete(111);
list.Print();
cout<<"Testing Add_Front: and others"<<endl;
list.Add_Front(888);
list.Print();
list.Add_Front(999);
list.Print();
list.Add_Front(49);
list.Print();
cout<<"Checking find function"<<endl;
answer = list.Find(888);
cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
cout<<"Checking find function"<<endl;
answer = list.Find(999);
cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
  
cout<<"Checking find function"<<endl;
answer = list.Find(49);
cout<<"Value for node returned by find function call with 888 is "<<answer->Data()<<"."<<endl;
cout<<"Call find function with value not in list."<<endl;
answer = list.Find(7);
if(answer == NULL)
{
cout<<"returned null pointer since 7 not found"<<endl;
}
else
{
cout<< "in else of answer == NULL where Value for node returned by find function call with 7 is "<<answer->Data()<<"."<<endl;
}
cout<<"testing delete_front: "<<endl;
list.Delete_Front();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
system("Pause");
return 0;