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
a = [4, −9, 4] b = [7, 2, 3] c = [5, −8, 9] d...
a = [4, −9, 4] b = [7, 2, 3] c = [5, −8, 9] d = [1, −3, 2] e = [6, −2, −5, 9] f = [4, −3, 7, 5] g = [1, 3, −1, 5] h = [7, −5, 5] i = [5, 13, −7, 11] Express the hyperplane implicitly or explicitly given the following a) a and h b) f and i c) e, f, and g d) The hyperplane containing b, c and d can...
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8...
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8 9 int[] numbers = new int[10]; for(int i=0; i < numbers.length; ++i)         numbers[i] = i * 3; for(int i=0; i < numbers.length; ++i)         System.out.print(numbers[i] / 2 + 1 + " "); System.out.println();
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT