Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In orderto receive credit, your programs must compile and run; and you must provide the actual source code file so that I can compile and run your programs (e.g. files ending in .cpp). The remaining problems for the assignment must be written up within a single Microsoft Word document file. You must include your name and course number within all files that you submit, including source code files as a comment at the top of each file that you create or modify.
Problem 1. [5 points] Arrays and Linked Lists: Read the assigned chapters and notes for Module3 located in the Learning Activities area. Then provide solutions to the following:
(a) [3 points] Download the List.zip file then modify the file to implement details for the helper function called createUnion(). The prototype for this function is as follows:
template <class Object>
void createUnion(list<Object> &results, list<Object> L1, list<Object> L2);
The implementation will use the C++ Standard Template Library list implementation. The print() function provides an example. The createUnion() function will create the unionof two given lists (L1 and L2) and will place the results in a third list called results. The union of two lists will consist of all items in both lists.
[img width="581" height="132" src="file:///C:\Users\樊宇博\AppData\Local\Temp\ksohtml\wps69D1.tmp.png">
Hint: You only need to modify the areas of the code in the List.cpp file that are markedwith the TODO comments. Everything else should remain that same. You can implement this function anyway you like, but you must use the provided lists and function signature and the list STL library. The results of the union must be placed in the results list. The print() function provides an example on how to use a for-loop with an iterator for apassed in list parameter. Note that the order of the displayed results list does not matter as long as the correct items are in the results list.
[img width="581" height="194" src="file:///C:\Users\樊宇博\AppData\Local\Temp\ksohtml\wps69D2.tmp.png">
Start by setting the results list equal to the first list:
results = L1;
This ensures that all items in first list are in the results list. Next create a for-loop and add all of the items in the second list to the results list.
Output: An example of the output for the program after the function is implemented is asfollows:
The union of
List 1 with List 2 is:
** Press any
key to continue **
(b) [2 points] Consider a function called deleteItemAtPosition(int position) in an array-based list data structure. The purpose of this method is to delete an item at the given position, if the items is in the list. This methods works fine for small array-based lists, but is very inefficient for large lists. Briefly explain why the same function is more efficient when implemented using a linked list implementation that uses pointers.
Problem 2. [5 points] Stacks and Queues: Read the assigned chapters and notes for Module 4located in the Learning Activities area, then provide solutions to the following:
(a) [3 points] Download the file Queue.zip and modify the Queue.cpp file by implementing the details for the helper function called addToFront(), which will take the in a queue and an item as parameters, and will add the item to the front of the queue. The remaining items in the queue will remain unchanged. The prototype for this function is as follows:
template <class Object>
void addToFront(queue<Object> &Q, Object item);
[img width="581" height="165" src="file:///C:\Users\樊宇博\AppData\Local\Temp\ksohtml\wps69E3.tmp.png">
Hint: Again, you only need to modify the areas in the Queue.cpp file by adding thenecessary code to implement the TODO areas as noted in the comments. The prototype for the function and everything else in the program will remain unchanged. You must use the function signature for your implementation. Consider using another queue called results to hold the results as you are processing the items in the queue:
Start by adding the passed in item to the results queue by calling results.push(item). Then walk through the queue, which is the passed in Q variable using a while loop like the one in the print() function. If the tmpItem does not equal the passed in item, add the tmpItem to the queue by calling the push() function on the results queue. Before returningfrom function set Q equal to the results queue:
[img width="581" height="209" src="file:///C:\Users\樊宇博\AppData\Local\Temp\ksohtml\wps69E4.tmp.png">
Q = results;
Output: The output for the program after the function is implemented should appear asfollows:
68.42 12.15 20.51 17.28 86.88
Queue after moving item 4 to front: 17.4 68.42 12.15 20.51 17.28 86.88
Queue after moving item 2 to front: 20.51 17.4 68.42 12.15 17.28 86.88
Queue after moving item 3 to front:
13.7 20.51 17.4 68.42 12.15 17.28 86.88
* Press any key to continue **
(b) [2 points> Suppose you were required to implement a function called orderQueue() that takes in a queue and reorders its items in ascending order. Explain why this function would violate the original purpose and design for a queue data structure and even a priority queue data structure.
Other Notes: Submit your solutions using the Problem Set 2 link provided in the Assignmentsarea. If you are using the Visual C++ or Dev-C++ compiler, you should only submit the source code files for your program (e.g. the files ending in .cpp). For space reasons, please do not submit the entire Visual C++ or Dev-C++ project folders. Do not hesitate to ask if you have any questions or need clarification on what to do for this assignment.
List.zip Queue.zip PS2(1).pdf.doc