For C++ Extend the class linkedListType by adding the following operations: a. W
ID: 3856829 • Letter: F
Question
For C++
Extend the class linkedListType by adding the following operations:
a. Write a function infoOfKthElement() that returns the info of the k-th element of the linked list. If no such element exists, throw exception with an appropriate message, print this message and do nothing with the linked list.
b. Write a function deleteKthElement() that deletes the k-th element of the linked list. If no such element exists, throw exception with an appropriate message, print this message and do nothing with the linked list.
c. Write the functions reversePrint() and recursiveReversePrint() (Printing a single linked list backwards) (see ch16) reversePrint() must be iterative function ; recursiveReversePrint() must be recursive function. . If list is empty, throw exception with an appropriate message, and print this message.
Test these functions using class UnorderedLinkedList.
Provided: stub program with all necessary classes and stub Driver program.
To Do: implement functions:
1. infoOfKthElement()
2. deleteKthElement()
3. reversePrint()
4. recursiveReversePrint()
throw and catch exceptions when it is appropriate (see a, b, c)
Explanation / Answer
void myLinkedList::infoOfKthElement()
{
int position, i, _ctr = 0;
if (head == NULL)
{
cout<<"empty list"<<endl;
return;
}
cout<<"Enter the position: ";
cin>>position;
struct node *s, *ptr;
s = head;
if (position == 1)
{
head = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
_ctr++;
}
if (position > 0 && position <= _ctr)
{
s = head;
for (i = 1;i < position;i++)
{
ptr = s;
s = s->next;
}
cout<<"val: "<<s->info<<endl;
}
else
{
cout<<"Position not in range"<<endl;
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
void myLinkedList::deleteKthElement()
{
int position, i, _ctr = 0;
if (head == NULL)
{
cout<<"empty lsit"<<endl;
return;
}
cout<<"Enter the position to delete: ";
cin>>position;
struct node *s, *ptr;
s = head;
if (position == 1)
{
head = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
_ctr++;
}
if (position > 0 && position <= _ctr)
{
s = head;
for (i = 1;i < position;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position not in range"<<endl;
}
free(s);
cout<<"Node Deleted successfully"<<endl;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
void myLinkedList::reversePrint() {
if (head == NULL)
{
cout<<"empty lsit"<<endl;
return;
}
struct node *s, *temp;
int _ctr = 0, index = 0;
while (s != NULL)
{
s = s->next;
_ctr++;
}
//declare array
int arr[_ctr];
temp = head;
cout<<"Reverse list: "<<endl;
while (temp != NULL)
{
arr[index] = temp->info;
index++;
temp = temp->next;
}
for(int i=_ctr-1;i>0;i--){
cout<<arr[i];
}
}
--------------------------------------------------------------------------------------------------------------------------
void myLinkedList::recursiveReversePrint() {
if (head == NULL)
{
cout<<"empty lsit"<<endl;
return;
}
recursiveReversePrint(head->next);
// printing elements
cout << head->data << endl;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.