Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fill in the following member function definitions: add Add_End, Delete, and Delete_Front in the following code. #include using namespace std; class Node { public:

Please fill in the following member function definitions:

add Add_End, Delete, and Delete_Front

in the following code.

#include

using namespace std;

class Node {

public:

Node() {};

void SetData(int aData) { data = aData; };

void SetNext(Node* aNext) { next = aNext; };

int Data() { return data; };

Node* Next() { return next; };

private:

int data;

Node* next;

};

class List {

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();

private:

Node *head;

};

/* 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"<

return;

}

if(head->Next() == NULL)

{

delete head;

head = NULL;

return;

}

Node *current;

Node *prev;

prev = head;

for(current = head->Next(); current->Next() != NULL; current = current->Next()) {

prev = current;

}

prev->SetNext(NULL);

delete current;

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 "<

return NULL;

}

else

{

cout<<"Found "<

return current;

}

}

void List::Print()

{

Node *current;

cout<<"Linked List Nodes: "<

if(head == NULL)

{

cout<<"List is empty." <

}

for(current = head; current != NULL; current = current->Next())

{

cout<Data();

cout<

}

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"<

list.Add_Front(888);

list.Print();

list.Add_Front(999);

list.Print();

list.Add_Front(49);

list.Print();

cout<<"Checking find function"<

answer = list.Find(888);

cout<<"Value for node returned by find function call with 888 is "<Data()<<"."<

cout<<"Checking find function"<

answer = list.Find(999);

cout<<"Value for node returned by find function call with 999 is "<Data()<<"."<

cout<<"Checking find function"<

answer = list.Find(49);

cout<<"Value for node returned by find function call with 49 is "<Data()<<"."<

cout<<"Call find function with value not in list."<

answer = list.Find(7);

if(answer == NULL)

{

cout<<"returned null pointer since 7 not found"<

}

else

{

cout<< "in else of answer == NULL where Value for node returned by find function call with 7 is "<Data()<<"."<

}

cout<<"testing delete_end: "<

list.Delete_End();

list.Print();

cout<<"testing delete_end: "<

list.Delete_End();

list.Print();

cout<<"testing delete_end: "<

list.Delete_End();

list.Print();

system("Pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions