Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

R-3.8. Describe a method for finding the middle node of a doubly linked list with header and trailer sentinels by link hopping, and without

image text in transcribedimage text in transcribed

R-3.8. Describe a method for finding the middle node of a doubly linked list with header and trailer sentinels by "link hopping," and without relying on explicit knowledge of the size of the list. In the case of an even number of nodes, report the node slightly left of center as the "middle." What is the running time of this method? We want to approach the center of the doubly linked list through both the header and the trailer, meeting in the center. If the number of elements in the doubly linked list is even then they will cross each other, in this case we need to check to see if the header and trailer while moving by seeing if they are or are not next to each other. If the number of elements in the doubly linked list is odd, that is the answer. The running time of a node slightly to the left of the center is O(n/2) which is equivalent to O(n). R-3.9 Give an implementation of the size() method for the SingularlyLinkedList class, assuming that we did not maintain size as an instance variable. Public int size() { Node temp = new Node (0, head): int result = 0; while(temp.getNext() != null) { result++; temp = temp.getNext(); } return result; } C-3.22 Write a method, shuffle(A), that rearranges the elements of array A so that everypossible ordering is equally likely. You may rely on the nextInt(n) method ofthe java.util.Random class, which returns a random number between 0 and n- 1inclusive. public static void shuffle (int[] arr) { Random rand = new Random(); for (int I = arr.length - 1; i > 0; i--) { int index = rand.nextInt(i + 1); int temp = arr[index]; array[index] = arr[i]; arr[i] = temp; } public static void main(String ars[]) { Scanner sc = new Scanner(System.in); System.out.println("Input the length of array"); int n = sc.nextInt(); System.out.println("Input elements of array"); for (i = 0; i < n; i++) { } my_array[i] = sc.nextInt(); Shuffle(my_array); for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions

Question

CL I P COL Astro- L(1-cas0) Lsing *A=2 L sin(0/2)

Answered: 1 week ago