Question

In: Computer Science

In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

In this assignment you are to utilize the Node data structure provided on Blackboard.
In this assignment you are to write a main program that implements two 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 of integer (.length is acceptable also)
and it should return a Node that is supposed to represent the head of a linked list.

Note: You may also choose to make it a void method and pass 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 its head and populate it with the
numbers stored in the array by placing the even numbers in the array first, followed by the odd
numbers. You may not change the order of the numbers inside of the array, 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 be 20->14->10->2->6->4->0->1->7->9->11-13->19

So return back from the function the head of this linked list.

Method 2:
^^^^^^^^^
Parameters and return type:
This method should take as a parameter the linked list generated in method 1 represented by
its head.

Method logic:
The method should start by reading in one integer from standard input, 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 0 and 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 error asking 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 an array of integers. Once that's
done, the program should read these integers and store them into an array.

2. Once the array has been populated and its size is known, 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 linked list 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 method 2. The rotated linked list.

- method 1 hints
The following should happen after reading an array a of size n Keep in mind that this is closer to pseudocode, so you need to fix it to compile, it's pretty close to what you need to do, but you need to adjust the code to work with the Node data structure. I am talking about private vs. public data members and the use of getters and setters.
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 how they 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 its head

- 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 where it's

// pointing and where the new end of the list is now.
}

Solutions

Expert Solution

Program:

class Node<T> {

//define left and right node

private Node<T> left;

private Node<T> right;

private T value;

Node(T v){ //Constructor to initialize the values

this.value=v;

}

//getter setters method

public void setValue(T t){

this.value=t;

}

public T getValue(){

return value;

}

public void setLeft(Node<T> t){

this.left=t;

}

public Node<T> getLeft(){

return left;

}

public void setRight(Node<T> t){

this.right=t;

}

public Node<T> getRight(){

return right;

}

}

public class Tester1<T>{

Node<T> root;

public void preorder(Node<T> root){ //function to test node class , this pre-order travarsal

if(root==null)

return;

System.out.print(root.getValue()+" ");

preorder(root.getLeft());

preorder(root.getRight());

}

public static void main(String args[]){ //test with Integer values

Tester1<Integer> t=new Tester1<Integer>();

Node<Integer> root=new Node<>(6);

root.setLeft(new Node<Integer>(7));

root.setRight(new Node<Integer>(8));

t.preorder(root);

}

}

Output:

Please give me thumbs up,if you like the answer .
Please let me know if you don't understand any step before giving Down rating directly :). Happy to help....


Related Solutions

In this assignment you are to utilize the Node data structure provided on Blackboard. In this...
In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignment you are to write a main program that implements two 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 of integer (.length is acceptable also) and it should return a Node that is supposed to...
For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment....
For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment. Considering the specific requirements of your scenario, complete the following steps using Excel. The accuracy of formulas and calculations will be assessed. Select the appropriate discrete probability distribution. If using a binomial distribution, use the constant probability from the collected data and assume a fixed number of events of 20. If using a Poisson distribution, use the applicable mean from the collected data. Identify...
Using the financial statement provided (an Excel spreadsheet with the data is available on Blackboard), prepare...
Using the financial statement provided (an Excel spreadsheet with the data is available on Blackboard), prepare a complete Statement of Cash Flows that shows both the direct method and the indirect method for summarizing cash flow from operating activities. Note to complete the assignment you will need to calculate items such as dividends paid (Retained Earnings & AOCI2019 – Net income2019 – Retained Earnings & AOCI2018) and capital expenditures (Net PP&E2018 – Book Value of PP&E Sold2019 – Depreciation Expense2019...
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
Assignment #2 (JAVA) In assignment 1 you had used the data structure called Stack to evaluate...
Assignment #2 (JAVA) In assignment 1 you had used the data structure called Stack to evaluate arithmetic expressions by first converting the given infixed expressions to postfixed expressions, and then evaluated the post fixed expression. Repeat the exercise for this assignment (assignment 2) by using the data structure called Binary Trees. Your output must display the original expression, the postfixed expression representing the Binary tree, and the evaluated result. Please bear in mind that a given expression could result in...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
If x is a node in a hierarchically organized tree structure, then it is ________________________________ for...
If x is a node in a hierarchically organized tree structure, then it is ________________________________ for all nodes in the tree structure higher than it.
Week 8 Assignment 4 Submission If you are using the Blackboard Mobile Learn iOS App, please...
Week 8 Assignment 4 Submission If you are using the Blackboard Mobile Learn iOS App, please click "View in Browser”. Click the link above to submit your assignment. Students, please view the "Submit a Clickable Rubric Assignment" in the Student Center. Instructors, training on how to grade is within the Instructor Center. Assignment 4: Win the Contract Due Week 8 and worth 120 points Imagine your small business produces tiny remote control aircraft capable of long sustained flights. You are...
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT