Question

In: Computer Science

5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6};...

5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6}; and we also have a linked list L of 5 entries 1 -> 2 -> 3 -> 5 -> 6, where 1 is the first in the linked list L, followed by 2 etc. We want to put a new item 4 between 3 and 5 in the array a and in the linked list L

(a) Explain in plain English how you do that in the array a. You may have to shift items right (or left?) Do you use a loop like a for loop? You can write some C / C++ / C# / Java code etc. to convince me, but the code does not have to run.

(b) Explain in plain English how you do that in the linked list L. How do you change the link from 3 to 5 in the original list L? You can write some code or pseudo code to convince me.

Solutions

Expert Solution

(a) In order to insert the number 4 between 3 and 5 in the array, we have to first find the element after which we have to insert the element 4. In this case, the element we have to find is 3. So, we will need a for loop for searching for an element. Once the element is found, we will first increase the array size by 1, add the new element (here 4) and shift the rest of the elements to the right by one. Let us see the c++ for loop to achieve this.

Let n = number of elements = 5

f = is set to 0 if element we are looking for(3) is not found, othwerise false = 0

ins = initially set to the element to be inserted = 4

//start of code snippet

for(int i = 0; i < n; i++){

int temp;

if(a[i] == 3){

f = 1;

n++;

continue;

}

if(f==1){

temp = a[i];

a[i] = ins;

ins = temp;

}

}

(b) In order to insert a new element anywhere inside a linked list, all we need to do is forst find the node after which the new element is to be inserted, in this case, 3. Then we make a new node containing the element to be inserted(4) and change the link field of 3 to point to node 4, and the link field of node 4 to point to node 5. A snippet of c++ code to explain this:

node = abstract data type to store declare variables of node type. It has 2 fields, int data and node* next

head = pointer to the head of linked list

//start of snippet

node *ptr = new node;

ptr->data = 4;

ptr->next = NULL;

node *temp = head;

while(temp != NULL){

if(temp->data == 3){

ptr->next = temp->next;

temp->next = ptr;

}

}


Related Solutions

Suppose A is (10, 2, 5, 9, 1, 8, 2, 4). Consider the function: int BBOX(int...
Suppose A is (10, 2, 5, 9, 1, 8, 2, 4). Consider the function: int BBOX(int n, int k)             if (n <= 0) return 0;             else if (A[n] < k) return (1+ 2*BBOX(n-1,k+1));             else return BBOX(n-1,k-2);             Find BBOX(8, 5)
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3 5 4 7 9 7 0 0 4 5 6 9 9 8 1 3 5 6 8 9 9 0 1 2 3 5 9 The table represent a random sample of 31 test scores taken from a large lecture class. Find the following [round to 2 decimal points X. XX] a) [2 pts] Find the 5 number summary [L, Q1, Q2, Q3,...
trace by using quicksort on the array : 4 6 5 3 2 7 1 using...
trace by using quicksort on the array : 4 6 5 3 2 7 1 using 7 as the pivot
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
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def;...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure how much memory will be allocated when these three lines of codes will execute. ➢ Describe what will happened after execution of each of these line of code in term of memory allocation and data storage Qno.1 Part(B) A capacitor is constructed from two conductive metal plates 30cm x 50cm which are spaced 6mm apart from each other, and uses...
In all cases we have an array of int or char of some fixed size. The...
In all cases we have an array of int or char of some fixed size. The program will prompt the user to enter some values, such as: Enter 7 integers to be stored in the array: 5 13 8 5 1 2    The questions that could then be asked of this data might be: Count how many numbers are < the last element Count how many numbers are odd Similarly we might prompt for character input: Enter 7 characters...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
Suppose we have an array A that contains a prime numbers from 2 to 200 in...
Suppose we have an array A that contains a prime numbers from 2 to 200 in sorted order. How many items of the array A would the binary search algorithm have to examine before concluding that 60 is not in the array A? 30 200 100 6 2- Suppose we have an array that contains 182 village name. These names are sorted alphabetically. How many names would binary search algorithm examine to locate a particular name in the array, in...
Consider the following data table: x 8 5 4 6 2 5 3 y 1 3...
Consider the following data table: x 8 5 4 6 2 5 3 y 1 3 6 3 7 2 5 (15 points) Create a scatterplot of the data either by hand or with a computer.  Does there appear to be a linear relationship between x and y?  If so, what is the strength and direction of the relationship? (20 points) Give the Simple Linear Regression Model, using x as the predictor variable and y as the response variable.  What is the meaning...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT