Question

In: Computer Science

After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is...

After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is the head)

1a)

Node* newNode = new Node(26);
newNode->next = head->next;
head = newNode;

1b)

Node* curNode = head;
for(int i=0; i<2; i++)
curNode = curNode->next;
Node* temp = curNode->next;
curNode->next = curNode->next ->next;
delete temp;
temp = nullptr;

Solutions

Expert Solution

Given Linked list:-

8->4->3->7->5

1a) Given Code:-

Node* newNode = new Node(26);
newNode->next = head->next;
head = newNode;

After execution of the above code, newNode is created with data field:- 26 and address field is assigned with the address of the second node(i.e the node having data value 4). After this, the address of the new node is assigned to head Node. So, indirectly the new node comes before the node having data value 4.

So, the final list is:-

8->26->4->3->7->5

1b) Given Code:-

Node* curNode = head;
for(int i=0; i<2; i++)
curNode = curNode->next;
Node* temp = curNode->next;
curNode->next = curNode->next ->next;
delete temp;
temp = nullptr;

In the above code, curNode is assigned with the address of head. Now the for loop will run twice and after execution of loop. The curNode is pointing to a node having data value 4.

After this, the temp will point to the node having data value 7. Now, the address field of curNode is assigned with the address of node having data value 7, and the temp is deleted. Due to this node having value 3 is deleted from the list.

So, the final list is:-

8->26->4->7->5


Related Solutions

What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5]...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5] D = [1, -9, 5, -3] E = [3, 3, -1] Uz = 1/|z| ^z d(X,Y) = (Rθ) d = diameter R = Radius θ = Theta Find a. Uc b. d (D, C) c. Let P = B + 3E, UP = d. A x B e. 3B x E f. C x D
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
a = [3, -4, 7] b = [-6, 9, 8] c = [4, 0, 8] d...
a = [3, -4, 7] b = [-6, 9, 8] c = [4, 0, 8] d =[7, 1, 7] e = [3, -5, 2, 1] f =[5, -7, -3, 6] g = [3, -4, 4, 3] P = Projection of ex. C = |g|(gf/gf) C = gf/|f| ex. P g --> f = Cgf = C(gf/f) (1/|f|) (f) =( gf/ff)(f) Find a. Pg --> f b. Pa --> 3b + e Find (cross multiply) a. ||a X b|| b. ||g...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question 's scores, If three points were added to every score in this distribution, what would be the new mean? If three points were added to every score in this distribution, what would be the new standard deviation. Remember, you have already calculated population standard deviation in a previous problem. This problem requires two answers.
Consider the matrix list x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write...
Consider the matrix list x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write a list comprehension to extract the first column of the matrix [1, 4, 7]. Write another list comprehension to create a vector of twice the square of the middle column.
Python programming question: Suppose i have a list like: ['a:10', 'b:9', 'c:8', 'd:7', 'e:6', 'f:5', 'g:4',...
Python programming question: Suppose i have a list like: ['a:10', 'b:9', 'c:8', 'd:7', 'e:6', 'f:5', 'g:4', 'h:3', 'i:2', 'j:1', 'k:0'] How do i trans form this into a dictionary or something i can plot a graph with using these keys and value pairs? thanks.
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C,...
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. How many hexadecimal strings of length twelve have five A’s and five B’s? How many hexadecimal strings of length twelve have at most three E’s? How many hexadecimal strings of length twelve have exactly three A’s and at least two B’s? How many hexadecimal strings of length twelve have exactly two A’s and exactly two B’s, so that the two...
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT