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
template
bool insertBeforeD(DNode
Erase: find a given value and delete the node from the linked list.
template
bool eraseD(DNode
template
bool eraseS(SNode
Remove duplicates -- find and delete all copies of a given value.
deleteDuplicatedD(DNode
deleteDuplicatedS(SNode
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
{
SNode
SNode
for (int i = 1; i < size; i++)
{
SNode
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
{
DNode
DNode
for (int i = 1; i < size; i++)
{
DNode
temp->next = node;
temp = node;
}
temp = first;
DNode
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
cout << "Printing Singly linked list: ";
while (headS != NULL)
{
cout << headS->data << "\t";
headS = headS->next;
}
cout << " Printing Doubly Linked List: ";
DNode
DNode
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started