Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!