(IN C++) /*Provide output to show your code works*/ Write the member functions f
ID: 3837690 • Letter: #
Question
(IN C++)
/*Provide output to show your code works*/
Write the member functions for the class List:
void Delete(int data) which delete the node with data value = data in the list if it exists in the list
void Add_End(int data) 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
as discussed below:
#include <iostream>
using namespace std;
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; };
};
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();
};
/* you need to add Add_End, Delete, and Delete_Front */
void List::Add_End(int data) {
/* need to write */
}
void List::Delete(int data) {
/* need to write */
}
void List::Delete_Front() {
/* Need to write */
}
void List::Add_Front(int data) {
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;
}
if(head->Next() == NULL)
{
head = NULL;
return;
}
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())
{}
f(current == NULL)
{
cout<<"Did not find "<<data<<"."<<endl;
return NULL;
}
else
{
cout<<"Found "<<data<<"."<<endl;
return current;
}
}
void List::Print()
{
Node *current;
cout<<"Linked List Nodes: "<<endl;
for(current = head; current != NULL; current = current->Next())
{
cout<<current->Data();
cout<<endl;
}
return;
}
int main()
{
List list;
Node *answer;
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();
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 999 is "<<answer->Data()<<"."<<endl;
cout<<"Checking find function"<<endl;
answer = list.Find(49);
cout<<"Value for node returned by find function call with 49 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
Implemented the required 3 functions
------------------------------------------------------------
void List::Add_End(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
if(head == NULL)
{
head = newNode;
return;
}
Node *current;
for(current = head; current->Next() != NULL; current = current->Next());
current->SetNext(newNode);
return;
}
void List::Delete(int data) {
Node *current, *prev;
prev = head;
for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{
prev = current;
}
if(current == NULL)
{
cout<<"Did not find "<<data<<"."<<endl;
return;
}
else if (current == head )
{
head = head->Next();
return;
}
else
{
prev->SetNext(current->Next());
return ;
}
}
void List::Delete_Front() {
if(head == NULL)
{
cout<<"List has no member so cannot delete end"<<endl;
return;
}
head = head->Next();
}
void List::Add_Front(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;
}
Output
------------------
Linked List Nodes:
111
Linked List Nodes:
111
222
Linked List Nodes:
111
222
333
Linked List Nodes:
111
222
333
444
Linked List Nodes:
111
222
333
444
555
Linked List Nodes:
111
222
333
555
Linked List Nodes:
111
222
555
Linked List Nodes:
111
555
Linked List Nodes:
111
Linked List Nodes:
Testing Add_Front: and others
Linked List Nodes:
888
Linked List Nodes:
999
888
Linked List Nodes:
49
999
888
Checking find function
Found 888.
Value for node returned by find function call with 888 is 888.
Checking find function
Found 999.
Value for node returned by find function call with 999 is 999.
Checking find function
Found 49.
Value for node returned by find function call with 49 is 49.
Call find function with value not in list.
Did not find 7.
returned null pointer since 7 not found
testing delete_front:
Linked List Nodes:
999
888
testing delete_end:
Linked List Nodes:
999
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.