In this assignment you are to utilize the Node datastructure provided on Blackboard.
In this assignment you are to write a main program that implementstwo methods and a main
method as their driver. So, only main and two methods with it.
Implementation Details:
Method 1:
^^^^^^^^^
Parameters and return type:
Takes as parameters an array of integers, the size of the array ofinteger (.length is acceptable also)
and it should return a Node that is supposed to represent the headof a linked list.
Note: You may also choose to make it a void method andpass the head of the linked list as a parameter,
if that's easier for you. You have the choice here.
Method Logic:
The method is supposed to create a linked list represented by itshead and populate it with the
numbers stored in the array by placing the even numbers in thearray first, followed by the odd
numbers. You may not change the order of the numbers inside of thearray, the order must remain
the same as it was read from input.
Example: Assume the array is: [0,1, 4, 6, 7, 9, 2, 10,11, 14, 13, 19, 20]
The linked list should be20->14->10->2->6->4->0->1->7->9->11-13->19
So return back from the function the head of thislinked list.
Method 2:
^^^^^^^^^
Parameters and return type:
This method should take as a parameter the linked list generated inmethod 1 represented by
its head.
Method logic:
The method should start by reading in one integer from standardinput, and based on that
integer, it needs to shift the linked list by that many positions.Keep in mind that you need
to do the necessary error checking, the shifting can be between 0and the size of the linked list - 1.
Example:
Assume the given linked list is:20->14->10->2->6->4->0->1->7->9->11-13->19
You read in an integer: You input the number 3.
The linked list should look like:2->6->4->0->1->7->9->11-13->19->20->14->10
If you read in a 6:
The linked list should look like:0->1->7->9->11-13->19->20->14->10->2->6->4
If you read in a 13 The method should print an errorasking you to enter a number between 0-12.
The main program:
^^^^^^^^^^^^^^^^^
1. Your program should run and ask the user to input the size of anarray of integers. Once that's
done, the program should read these integers and store them into anarray.
2. Once the array has been populated and its size isknown, then you need to call method 1 defined
above. The result should be a head pointer of a linked list.
At this point you declare a cursor method and go through the linkedlist and print it to the screen.
Based on the above example: 20 14 10 2 6 4 0 1 7 9 11 13 19.
3. Call method 2
4. Print the linked list resulting from calling method2. The rotated linked list.
- method 1 hints
The following should happen after reading an array a of size n Keepin mind that this is closer to pseudocode, so you need to fix it tocompile, it's pretty close to what you need to do, but you need toadjust the code to work with the Node data structure. I am talkingabout private vs. public data members and the use of getters andsetters.
Also the following is assuming a dummy node.
Node head = new Node(); Node cursor = head;
for(i = 0; i < n; i++ ) {
int x = a[i]; // this is the number we're working with.
if(x % 2 == 0) // the number is even.
{
// this will insert the even numbers in the reverse order of howthey are in the array.
head.next = new Node(x, head.next);
}else{
cursor.next = new Node(x, null);
cursor = cursor.next
}
}
return head; // this will return the entire list represented by itshead
- Method 2 hints
public static void rotateList(Node head){
// read in the number n to rotate by
// then do the rotation code by manipulating the head and whereit's
// pointing and where the new end of the list isnow.
}