Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are required to implement the following functions for both singly- and doubly-linked lists. Appendix 1 provides sample code to create some lists for testing

You are required to implement the following functions for both singly- and doubly-linked lists. Appendix 1 provides sample code to create some lists for testing purposes.

Insert before: find a given value and insert a node before it.

template

bool insertBeforeS(SNode* node, Object before, Object newValue);

template

bool insertBeforeD(DNode* node, Object before, Object newValue);

Erase: find a given value and delete the node from the linked list.

template

bool eraseD(DNode* node, Object value);

template

bool eraseS(SNode* node, Object value);

Remove duplicates -- find and delete all copies of a given value.

deleteDuplicatedD(DNode* node, Object value);

deleteDuplicatedS(SNode* node, Object value);

Write an appropriate main driver to test all the functions.

Appendix 1:

template

struct SNode

{

Object data;

SNode *next;

SNode(const Object & d = Object{ }, SNode * n = nullptr)

: data{ d }, next{ n } { }

};

//function to create Singly Linked List with values

template

SNode* createTestList_S(Object ary[],int size)

{

SNode* first = new SNode(ary[0]);

SNode* temp = first;

for (int i = 1; i < size; i++)

{

SNode* node= new SNode(ary[i]);

temp->next = node;

temp = node;

}

return first;

}

//Doubly Linked List structure

template

struct DNode

{

Object data;

DNode *prev;

DNode *next;

DNode(const Object & d = Object{ }, DNode * p = nullptr, DNode * n = nullptr)

: data{ d }, prev{ p }, next{ n } { }

};

//function to create Doubly Linked List with values

template

DNode* createTestList_D(Object ary[], int size)

{

DNode* first=new DNode(ary[0]);

DNode* temp = first;

for (int i = 1; i < size; i++)

{

DNode* node = new DNode(ary[i]);

temp->next = node;

temp = node;

}

temp = first;

DNode* x = first;

temp = temp->next;

while (temp != NULL)

{

temp->prev = x;

x = temp;

temp = temp->next;

}

return first;

}

int main()

{

int ary[] = { 1,7,3,7,5,6,7 };

SNode* headS = createTestList_S(ary,7);

cout << "Printing Singly linked list: ";

while (headS != NULL)

{

cout << headS->data << "\t";

headS = headS->next;

}

cout << " Printing Doubly Linked List: ";

DNode* headD = createTestList_D(ary,7);

DNode* headDTemp = headD;

while (headD != NULL)

{

cout << headD->data << "\t";

headD = headD->next;

}

headD = headDTemp;//move headD to the tail

while (headD->next != nullptr)

{

headD = headD->next;

}

cout << endl << endl;

cout << " Printing Doubly Linked List in reverse: ";

while (headD != NULL)

{

cout << headD->data << "\t";

headD = headD->prev;

}

system("pause");

}

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

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

List the purpose of cross-validation of data.

Answered: 1 week ago

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago

Question

9. Power and politics can be destructive forces in organizations.

Answered: 1 week ago