Problem Set 2: Lists, Stacks, and Queues
CS3330 Data Structures and Algorithms
Term 5 2015: May 25 – July 26
Dr. Jack Davault
Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In order
to 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 Module
3 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
void createUnion(list &results,
list L1,
list L2);
The implementation will use the C++ Standard Template Library list implementation. The
print() function provides an example. The createUnion() function will create the union
of 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.
Hint: You only need to modify the areas of the code in the List.cpp file that are marked
with 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 a
passed 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.
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 as
follows:
1
List 1:
60 69 20 59 99 45
List 2:
46 37 41 21 92 73
The union of List 1 with List 2 is:
60 69 20 59 99 45 46 37 41 21 92 73
** 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 4
located 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
void addToFront(queue &Q,
Object item);
Hint: Again, you only need to modify the areas in the Queue.cpp file by adding the
necessary 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:
queue results;
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 returning
from function set Q equal to the results queue:
Q = results;
Output: The output for the program after the function is implemented should appear as
follows:
Queue:
68.42 12.15 20.51 17.28 86.88
2
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 Assignments
area. 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.
3
Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In
order to 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
Module 3 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
void createUnion(list &results,
list L1,
list L2);
The implementation will use the C++ Standard Template Library list implementation.
The print() function provides an example. The createUnion() function will create the
union of 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.
Hint: You only need to modify the areas of the code in the List.cpp file that are marked
with 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 a
passed 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.
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 as
follows:
1
List 1:
60 69 20 59 99 45
List 2:
46 37 41 21 92 73
The union of List 1 with List 2 is:
60 69 20 59 99 45 46 37 41 21 92 73
** Press any key to continue **
(b) [2 points] Consider a function called deleteItemAtPosition(int position) in an arraybased 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 4
located 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
void addToFront(queue &Q,
Object item);
Hint: Again, you only need to modify the areas in the Queue.cpp file by adding the
necessary 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:
queue results;
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
returning from function set Q equal to the results queue:
Q = results;
Output: The output for the program after the function is implemented should appear
as follows:
Queue:
68.42 12.15 20.51 17.28 86.88
2
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 Assignments
area. 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.
3
Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In
order to 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
Module 3 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
void createUnion(list &results,
list L1,
list L2);
The implementation will use the C++ Standard Template Library list implementation.
The print() function provides an example. The createUnion() function will create the
union of 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.
Hint: You only need to modify the areas of the code in the List.cpp file that are marked
with 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 a
passed 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.
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 as
follows:
1
List 1:
60 69 20 59 99 45
List 2:
46 37 41 21 92 73
The union of List 1 with List 2 is:
60 69 20 59 99 45 46 37 41 21 92 73
** Press any key to continue **
(b) [2 points] Consider a function called deleteItemAtPosition(int position) in an arraybased 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 4
located 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
void addToFront(queue &Q,
Object item);
Hint: Again, you only need to modify the areas in the Queue.cpp file by adding the
necessary 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:
queue results;
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
returning from function set Q equal to the results queue:
Q = results;
Output: The output for the program after the function is implemented should appear
as follows:
Queue:
68.42 12.15 20.51 17.28 86.88
2
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 Assignments
area. 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.
3
Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In
order to 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
Module 3 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
void createUnion(list &results,
list L1,
list L2);
The implementation will use the C++ Standard Template Library list implementation.
The print() function provides an example. The createUnion() function will create the
union of 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.
Hint: You only need to modify the areas of the code in the List.cpp file that are marked
with 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 a
passed 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.
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 as
follows:
1
List 1:
60 69 20 59 99 45
List 2:
46 37 41 21 92 73
The union of List 1 with List 2 is:
60 69 20 59 99 45 46 37 41 21 92 73
** Press any key to continue **
(b) [2 points] Consider a function called deleteItemAtPosition(int position) in an arraybased 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 4
located 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
void addToFront(queue &Q,
Object item);
Hint: Again, you only need to modify the areas in the Queue.cpp file by adding the
necessary 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:
queue results;
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
returning from function set Q equal to the results queue:
Q = results;
Output: The output for the program after the function is implemented should appear
as follows:
Queue:
68.42 12.15 20.51 17.28 86.88
2
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 Assignments
area. 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.
3
Purchase answer to see full
attachment