I know this problem seems long, but it is just editing an existing program. I po
ID: 3621074 • Letter: I
Question
I know this problem seems long, but it is just editing an existing program. I posted the three files to the program below that I need to modify, (.cpp file, .h file, and tester file). I will rate the answer as Lifesaver!
(a) Develop a new function member called search() to search for a data value in the list. If the function found the data, it should return the index of the element. Not that the first element corresponds to the index 0. If the data value doesn’t exist in the list, your function should return -1. You have to declare the function at the LinkedList.h and provide the implementation at the LinkedList.cpp. Then, invoke the function at the linked_list_tester.cpp to test it. This is the prototype of the function.
int search(ElementType dataVal);
If the function can’t be executed by any reason (For example, index is too big.), it return false. Otherwise, it should return true.
----------------------------------------------------------------------------------------------------------------------------
LinkedList.h
Explanation / Answer
void LinkedList::search(int item) { Node * ptr = first; int pos = -1; while (ptr != NULL) { pos++; if (ptr->data == item) return pos; ptr = ptr->next; } return -1; } Here I've provided you with the search implementation, it's quite simple to follow. I'm sorry that I couldn't do the swap implementation for you because I had to go. Hopefully someone else will provide it if not and you still require it just contact me. If you wish to try it on your own, here's how you should go about doing it, you will first need to create two local pointer to find each items to be swapped then just swap their values. Also will need a local variable of ElementType. Steps: 1) Using a while loop to find the address of the first item. 2) Another while loop to find the address of the second item. 3) Assign one of the data to a local variable so you can swap them. 4) Just swap the data ad these addresses, since we do not care about their address. like such: localvar = localptr1->data; localptr1->data = localptr2->data; localptr2->data = localvar;