Question

In: Computer Science

Write the code that will produce the given "after" result from the given "before" starting point...

Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown. Assume that the nodes have already been declared and initialized to match the "before" figure below. There may be more than one way to write the code, but do NOT change any existing node's data field value. Do not create any new nodes, you must just rearrange the existing nodes.  

If a variable does not appear in the "after" picture, it doesn't matter what value it has after the changes are made. If a given node object does not appear in the "After" picture, you must free its memory to avoid a memory leak.

You should not be depending on the values of the nodes, the second test case here will test a list1->a->b->c->d (some other list of the values). And it must be re-arranged in the same way.  

Before
list1: 1 -> 2 -> 3 -> 4 /
After
list1: 3 -> 2 /
list2: 4 -> 1 /

Assume that you are using the following ListNode structure:

struct ListNode {
    int data;        // data stored in this node
    ListNode* next;  // a link to the next node in the list
​};

Edit here:

#pragma once
#include <iostream>
using namespace std;

struct ListNode {
int data;
ListNode* next;
};

void pointerFun(ListNode* &list1, ListNode* &list2) {
// TODO: write your code here
}

Solutions

Expert Solution

#pragma once
#include <iostream>
using namespace std;

struct ListNode {
int data;
ListNode* next;
};

void pointerFun(ListNode* &list1, ListNode* &list2) {
  
   ListNode* temp = NULL;
  
   // loop to delete existing the nodes of list2 (if any)
   while(list2 != NULL)
   {
       temp = list2;
       list2 = list2->next;
       temp->next = NULL;
       delete temp;
       temp = NULL;
   }  
  
   int count = 0;
   ListNode* curr = list1;
   // loop to keep first 4 nodes of list1 in list1 and keep the rest nodes to list2
   while(curr != NULL)
   {
       count++;
       if(count == 4)
       {
           list2 = curr->next;
           curr->next = NULL;
       }
      
       curr = curr->next;
   }

   // loop to delete all the nodes after the first 4 nodes of list1 (if any)
   while(list2 != NULL)
   {
       temp = list2;
       list2 = list2->next;
       temp->next = NULL;
       delete temp;
       temp = NULL;
   }
  
   // list1 -> 1 -> 2 -> 3 -> 4 -> NULL
  
   temp = NULL;
   curr = list1;
   // loop to get the last and second last node
   while(curr->next != NULL)
   {
       temp = curr;
       curr = curr->next;
   }

// remove the last node from list1
   temp->next = NULL; // temp : 3 -> NULL
  
   // set list2 to last node
   list2 = curr; // list2 : 4 -> NULL
  
   // set temp to first node
   temp = list1; // temp : 1 -> 2 -> 3 -> NULL
  
   // set list1 to second node
   list1 = list1->next; // list1 : 2 -> 3 -> NULL
  
   // add temp to list2
   // set next of temp to null
   temp->next = NULL; // temp : 1 -> NULL
  
   // set next of list2 to temp
   list2->next = temp; // list2 : 4 -> 1 -> NULL
  
   // set temp to previous third node
   temp = list1->next; // temp : 3 -> NULL
  
   // set next of list1 to null
   list1->next = NULL; // list1: 2 -> NULL
  
   // set next of temp to list1
   temp->next = list1; // temp: 3 -> 2 -> NULL
  
   // update list1 to temp
   list1 = temp; // list1: 3 -> 2 -> NULL
      
}

// testing the function


int main()
{
ListNode *list1, *list2 = NULL, *curr;
ListNode *node;
list1 = new ListNode;
list1->data = 1;
list1->next = NULL;

node = new ListNode;
node->data = 2;
node->next = NULL;
list1->next = node;
curr = list1->next;

node = new ListNode;
node->data = 3;
node->next = NULL;
curr->next = node;
curr = curr->next;

node = new ListNode;
node->data = 4;
node->next = NULL;
curr->next = node;
curr = curr->next;

node = new ListNode;
node->data = 5;
node->next = NULL;
curr->next = node;
curr = curr->next;

node = new ListNode;
node->data = 6;
node->next = NULL;
curr->next = node;
curr = curr->next;


node = new ListNode;
node->data = 7;
node->next = NULL;
curr->next = node;
curr = curr->next;


node = new ListNode;
node->data = 10;
node->next = NULL;
list2 = node;
curr = list2;

node = new ListNode;
node->data = 12;
node->next = NULL;
curr->next = node;
curr = curr->next;

node = new ListNode;
node->data = 15;
node->next = NULL;
curr->next = node;
curr = curr->next;

node = new ListNode;
node->data = 18;
node->next = NULL;
curr->next = node;
curr = curr->next;


cout<<"Before: "<<endl<<"List1: ";
curr = list1;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}

cout<<"NULL"<<endl;

cout<<"List2: ";
curr = list2;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}

cout<<"NULL"<<endl;

pointerFun(list1, list2);

cout<<"After: "<<endl<<"List1: ";
curr = list1;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}

cout<<"NULL"<<endl;

cout<<"List2: ";
curr = list2;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}

cout<<"NULL";


return 0;
} //end of testing the function

Output:


Related Solutions

Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
Open selfmodifying.asm in MARS. Before running the code, predict: what will be the result in register...
Open selfmodifying.asm in MARS. Before running the code, predict: what will be the result in register $s0when this program is finished running? Now when you run the program, you will see an error: “Cannot read directly from text segment!”. To allow code to read and write the text segment, check Settings | “Self-modifying code”. Now click run again. a) Explain the result in register $s0when this program is finished running. In order to understand what is going on, we suggest......
TOPRAK A.Ş. After the separation point, they produce G10 and H8 products, and after the additional...
TOPRAK A.Ş. After the separation point, they produce G10 and H8 products, and after the additional cost expense of 15.000 TL for G10 product and 45.000 TL for H8 product, new product is produced under the names Super G10 and Super H8. 250 kg from Super G10 and 400 kg from Super H8 are produced. The sales price of Super G10 is 400TL / kg, and the sales price of Super H8 is 800 TL / kg. Total combined costs...
Write a program that acts as a basic calculator Assume the starting result is 0 Ask...
Write a program that acts as a basic calculator Assume the starting result is 0 Ask the user for the operation to be performed 0 – Exit 1 – Add 2 – Subtract 3 – Multiply 4 – Divide (Assume input will not be 0) Keep on performing the operation and showing the result until the user inputs the ‘0’ (exit) operation. You need to have everything written as functions E.g. - You should only have the options listed once...
Question: Using this code as a starting point, restructure the solution so that each sub-task (there...
Question: Using this code as a starting point, restructure the solution so that each sub-task (there are three) is performed in its own, separate function. When done, the function toDecimal should (1) do none of the actual "work" itself and (2) should call three separate functions in an appropriate order to complete the conversion process. This is similar to the work you did in exercise 6.4 (cengage mindtap) to restructure function newton. However, there is no recursion involved in this...
Please carefully review the code to fill in after the given instructions and complete the code...
Please carefully review the code to fill in after the given instructions and complete the code for me. Thank you in advance. In this problem, we will implement a simple dictionary of common words in the English language, represented as an array of words paired with their lengths. You will need to implement each of the below methods in the Dictionary class. In this problem, the first line of input represents the method to call. It will be one of...
Before we begin graphing systems of equations, a good starting point is to review our knowledge...
Before we begin graphing systems of equations, a good starting point is to review our knowledge of 2-D graphs. These graphs are known as 2-D because they have two axes. Find an online image of a graph to use as the foundation of your discussion. (This is easily accomplished by searching within Google Images.) Using your graph as the example: 1.) Select any two points on the graph and apply the slope formula, interpreting the result as a rate of...
A test of abstract reasoning is given to a sample of students before and after they...
A test of abstract reasoning is given to a sample of students before and after they completed a formal logic course. Assume that two dependent samples have been randomly selected from normally distributed populations. The results are given below. At the 0.05 significance level, use critical value method to test the claim that the mean score is not affected by the course. Does the course improve the students’ skill on abstract reasoning?      Before 74 83 75 88 84 63 93...
A test of abstract reasoning is given to a random sample of students before and after...
A test of abstract reasoning is given to a random sample of students before and after they completed a formal logic course. The results are given below. Construct a 95% confidence interval for the mean difference in tests scores. Research has shown that the distribution of such differences is approximately normal. Interpret your result. Before 74 83 75 82 80 63 93 84 91 77 After 73 77 70 77 74 67 95 83 84 75
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT