The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Do I have the right answers for these question? if not what did I miss and why?
Questions are in the attached pdf.
My Answers are:
Chapter 5-2. What is the output of each of the following C++ statements?
a. 18
b. 32
c. 25
d. 16
Chapter 5-4. Mark each of the following statements as valid or invalid. If a statement is invalid, explain why.
a. VALID
b. VALID
c. VALID
d. *list = B;
INVALID (*list is a dereferenced pointer to list which is a collection of nodes and you are trying to assign it to an actual node.)
e. VALID
f. B = A->link->info;
INVALID (B is of nodeType<Type> and A->link->info is of type int)
g. VALID
h. list = B->link->link;
INVALID List is collection of nodes and B->link-> is a pointer to a node.
i. B = B->link->link->link;
INVALID (Read access violation because B->link->link->link is NULL;
COSC 2336 Programming Fundamentals III Lab 5 - Exercisesi Figure 5-1 Linked List for Exercises 2 & 4 Chapter 5-2.What is the output of each of the following C++ statements?
a.
b.
c.
d. cout
cout
cout
cout <<
<<
<<
<< list->info;
A->info;
B->link->info;
list->link->link->info Chapter 5-4. Mark each of the following statements as valid or invalid. If a statement is invalid, explain
why.
a.
b.
c.
d.
e.
f.
g.
h.
i. A = B;
list->link = A->link;
list->link->info = 45;
*list = B;
*A = *B;
B = A->link->info;
A->info = B->info;
list = B->link->link;
B = B->link->link->link; Professor R. Garrett COSC 2336 Programming Fundamentals III Page 1 COSC 2336 Programming Fundamentals III Chapter 5-14. What is the output of the following program segment?
list<int> intList;
ostream_iterator<int> screen(cout, " ");
list<int>::iterator listIt;
intList.push_back(5);
intList.push_front(23);
intList.push_front(45);
intList.pop_back();
intList.push_back(35);
intList.push_front(0);
intList.push_back(50);
intList.push_front(34);
copy(intList.begin(), intList.end(), screen);
cout << endl;
listIt = intList.begin();
intList.insert(listIt,76);
++listIt;
++listIt;
intList.insert(listIt,38);
intList.pop_back();
++listIt;
++listIt;
intList.erase(listIt);
intList.push_front(2 * intList.back());
intList.push_back(3 * intList.front());
copy(intList.begin(), intList.end(), screen);
cout << endl; Professor R. Garrett COSC 2336 Programming Fundamentals III Page 2 COSC 2336 Programming Fundamentals III Lab 5 - Programming Exercisesi
Chapter 5-6.
a. Add the following operation to the class orderedLinkedList:
void mergeLists(orderedLinkedList<Type> &list1,
orderedLinkedList<Type> &list2);
// This function creates a new list by merging the
// elements of list1 and list2.
// Postcondition: first points to the merged list; list1
// and list2 are empty
Example: Consider the following statements:
orderedLinkedList<int> newList;
orderedLinkedList<int> list1;
orderedLinkedList<int> list2;
Suppose list1 points to the list with the elements 2 6 7 and list2 points to the list with the
elements 3 5 8. The statement: newList.mergeLists(list1, list2); creates a new linked
list with the elements in the order 2 3 5 6 7 8 and the object newList points to this list. Also,
after the preceding statement executes, list1 and list2 are empty.
Implementation Notes: No additional memory is required when creating the merged list. The
correct solution should perform the following tasks:
1.
2.
3.
4.
5.
6.
7.
8. Create a pointer to the merged list (newList)
Compare the first element in list1 and list2
Insert the smaller of the 2 elements into the merged list through pointer manipulation.
Advance the “first” pointer of the list containing the smaller element.
a. The size of the list containing the smaller element is reduced by 1.
Repeat the process until one of the lists is empty.
The elements in the remaining list should then be added to the end of the merged list.
Don’t forget to update the “count” variable in each list after removing an element from
that list.
After the elements in both lists have been added to the merged list simply set the “first”
and “last” pointers in each list to NULL to indicate the lists are empty. This does not result
in a memory leak because the elements in each list were added to the merged list through
pointer manipulation. Professor R. Garrett COSC 2336 Programming Fundamentals III Page 3 COSC 2336 Programming Fundamentals III
Modify the file orderedLinkedList.h and add your changes at the end of the
orderedLinkedList class definition. The file linkedList.h required by the file
orderedLinkedList.h has been provided as well.
b. Write the definition of the function template mergeLists to implement the operation
mergeLists. Modify the class orderedLinkedList in the file orderedLinkedList.h
and add your function definition at the end of the file. Then use the test program lab5-6.cpp to test
your program. Use the input data (if any) shown in the output window on the following pages and
then compare your results with the expected results.
Expected Program Input and Output i
Exercises and Programming Exercises were extracted from the adopted course textbook, Data Structures Using
C++, Second Edition, D.S. Malik, 2010 Course Technology, Cengage Learning for the purpose of annotating each
exercise with Professor specific instructions (when necessary) that facilitate student completion. Professor R. Garrett COSC 2336 Programming Fundamentals III Page 4
-----------