M3_Ch16HomeworkQuestions(1)

docx

School

Harvard University *

*We aren’t endorsed by this school

Course

161

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

2

Uploaded by HighnessBatMaster978

Report
CSC161 Chapter 16 Homework Questions 1. Suppose that first is a pointer to a linked list. What is stored in first? first would contain a memory address to the first node in the list 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 2 through 4. 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? i. trail->link = NULL; delete last; The first statement will clear the pointer to the last node which is then deleted by the second statement. ii. temp->link =trail; the link from temp will lead to trail instead of to the next node. iii. list->info = 19; this statement changes the info in the first node from 75 to 19 iv. current = current->link; current->link = temp->link; the first statement shifts pointer to point to the second node. Then it will change the link of the second node to temp 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; 75 35 86 10 50 28 65 39 b. while (current != last) current = current->link; cout << current->info << " "; cout << endl; 35 86 10
CSC161 50 28 65 4. Using the figure from question 2: If the following C++ code is valid, show the output. If it is invalid, explain why. current = temp->link; trail = list; temp = list->link; trail = temp; temp->link = current->link; current = trail->link; cout << trail->info << " " << current->info << endl; 35 50 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 pointers if you need them. a. Which pointer points to the first node of the linked list? list b. Determine the order of the nodes of the linked list. 25->16->12 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?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help