C++ Programming: Program Design Including Data Structures Ch 16 7th Edition 1. S
ID: 3854986 • Letter: C
Question
C++ Programming: Program Design Including Data Structures Ch 16 7th Edition
1. Suppose that first is a pointer to a linked list. What is stored in first?
2. Consider the linked list shown in the figure below. Assume that the nodes are in the usual info-link form. Use this list to answer Exercises 6 through 14. If necessary, declare additional variables. (Assume that list, current, temp, trail, and last are pointers of type nodeType.)
What are the effects, if any, of each of the following C++ statements?
a. trail->link = NULL;
delete last;
b. temp->link =trail;
c. list->info = 19;
d. current = current->link;
current->link = temp->link;
3. Using the figure from question 2 what is the output of the following C++ code?
a. while (current != NULL)
cout << current->info << " ";
current = current->link;
cout << endl;
b. while (current != last)
current = current->link;
cout << current->info << " ";
cout << endl;
4. Using the figure from question 2:
a. If the following C++ code is valid, show the output. If it is invalid, explain why.
ccurrent = temp->link;
trail = list;
temp = list->link;
trail = temp;
temp->link = current->link;
current = trail->link;
cout << trail->info << " " << current->info << endl;
5. Assume that the node of a linked list is in the usual info-link form with the info of type int. (list and ptr are pointers of type nodeType.) The following code creates a linked list:
ptr = new nodeType;
ptr->info = 16;
list = new nodeType;
list->info = 25;
list->link = ptr;
ptr = new nodeType;
ptr->info = 12;
ptr->link = NULL;
list->link->link = ptr;
Use the linked list created by this code to answer the following questions. (These questions are independent of each other.) Declare additional poin- ters if you need them.
a. Which pointer points to the first node of the linked list?
b. Determine the order of the nodes of the linked list.
c. Write a C++ code that creates and inserts a node with info 45 after the node with info 16.
d. Write a C++ code that creates and inserts a node with info 58 before the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list?
e. Write a C++ code that deletes the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list?
list 7535 861-10 501 28 551-39 current temp trail lastExplanation / Answer
The answer is as follows:
1. Suppose that first is a pointer to a linked list. What is stored in first?
first will store the address of first element in the linked list
2.
a. trail->link = NULL - The link of the node cotaining 65 will point to NULL instead of pointing to the nodse containing 39
delete last - No change in the list. last pointer is not pointing to any valid value.
b. temp->link = trail; - The link of the node containg 86 will point to the node containg 65
c. list->info = 19; The node containg value 75 will now contain the value 19
d. current = current->link; - Now current will point to the node containing value 35
current->link = temp->link; - Now the link of the node containing 35 will point to the node containing 65. As due to code in "b" above temp->link is now pointing to trail
3.Using the figure from question 2 what is the output of the following C++ code?
a. while (current != NULL)
cout << current->info << " ";
current = current->link;
cout << endl;
The ouput will be : 35 35 35.....keep on printing as it is a infinite loop.
b. while (current != last)
current = current->link;
cout << current->info << " ";
cout << endl;
The ouput will be a run time error because last has been deleted and should not be used now.
4. Using the figure from question 2:
ccurrent = temp->link; // assuming ccurrent is declared as variable of type pointer to nodeType
trail = list;
temp = list->link;
trail = temp;
temp->link = current->link;
current = trail->link;
cout << trail->info << " " << current->info << endl;
The output will be 35 65
5. Assume that the node of a linked list is in the usual info-link form with the info of type int. (list and ptr are pointers of type nodeType.) The following code creates a linked list:
ptr = new nodeType;
ptr->info = 16;
list = new nodeType;
list->info = 25;
list->link = ptr;
ptr = new nodeType;
ptr->info = 12;
ptr->link = NULL;
list->link->link = ptr;
Use the linked list created by this code to answer the following questions. (These questions are independent of each other.) Declare additional poin- ters if you need them.
a. Which pointer points to the first node of the linked list?
list points to the first node
b. Determine the order of the nodes of the linked list.
The order is 25, 16, 12
c. Write a C++ code that creates and inserts a node with info 45 after the node with info 16.
nodeType *ptr1;
ptr1 = list->link->link;
list->link->link = new nodeType;
list->link->link->info = 45;
list->link->link->link = ptr1;
d. Write a C++ code that creates and inserts a node with info 58 before the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list?
ptr = new nodeType;
ptr->info = 58;
ptr->link = list;
list = ptr;
yes.this requirs to the change the value of the pointer that was pointing to the first node of the linked list
e. Write a C++ code that deletes the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list?
list = list->link;
yes.this requirs to the change the value of the pointer that was pointing to the first node of the linked list
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.