Question

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 assignmet 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. 

Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in
      it as a guide (you should actually look at them) but you cannot use any of the methods in your 
      solution. Also, you may not modify the Node class in any way, we will assume that you just used 
      it. You should not submit it. 

You only need to submit only your drive program. 

Implementation Details:
^^^^^^^^^^^^^^^^^^^^^^^
The following are the implementation details of this assignment, you must adhere to all of the 
reauirements. 


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:
^^^^^^^^^
Parmeters and return type:
This method shoould 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.

Here's the shell of my code

public static void main(String [] args)
{
    Node<Integer> head;

    head = buildList();
    shiftList(head);
}

public static Node<Integer> buldList()
{
    // declare your scanner in
    // declare the number of integers you're going to read (n)
    // declare your array a

    Node<Integer> head, cursor;

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

        a[i] = in.nextInt();

        // if i == 0 that means that the head and the cursor are both
        // pointing to null and there exists no nodes in the linked list
        // therefore we need to create our first node first, and have the
        // head and the cursor both point to it.
        // After that i will not be equal to zero and we can then start executing
        // the else part of this if statement by checking if the number is odd
        // or even.
        if( i == 0){
            head = new Node<Integer>(a[i], null);
            // insert the code that makes the cursor point to the head

        } else {
            if(a[i] % 2 == 1){   // if the number is odd

                cursor.setNext(new Node(a[i], null));
                // Insert the code that moves the cursor over.

            }else{
                // the number is even, so we will insert at the head and move the head
                // to point to the newly inserted node.
                // Use a code that is highly similar to the listHeadInsert
                // method in the linked list class. Do not call the listHeadInsert
                // method though, but you can use its logic

            }
        }
    }

    return head;
}


public static void shiftList(Node<Integer> head)
{
    // in this code you will need to have some temporary variables starting

    // Ask the user to enter a number by which to shift
    // make the necessary error checking to make sure that the shift is valid
    // call this number x.

    tmp = head;
    cursor = head;

    // now you moved tmp to the location where head needs to be
    for(i = 0; i < x - 1; i++)
        tmp = tmp.getNext();

    // at this point find the end of the list
    while(cursor.getNext() != null)
        cursor = cursor.getNext();

    // make the last node point to the old head
    cursor.setNext(head);

    // Now write the code that moves the head over and set the Next of the node
    // before the new location of the head to null.



}

public class Node<T>
{
   
    private T value;            // this is the data value
    private Node<T> next; // this is pointing to the next node


    // the node constructor
    public Node (T v, Node<T> n)
        {
            value = v;
            next = n;
        }

    // getters and setters for the node's value and next pointer
    public T getValue() {return value;}
    public Node<T> getNext() {return next;}
    public void setValue(T v){value = v;}
    public void setNext(Node<T> n){next = n;}

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class LinkedListDemo {

    public static void main(String [] args)
    {
        Node<Integer> head;

        head = buildList();
        printList(head);

        System.out.println();

        head = shiftList(head);
        printList(head);
    }

    public static void printList(Node<Integer> node) {
        while(node != null) {
            System.out.print(node.getValue() + " ");
            node = node.getNext();
        }
        System.out.println(); 

    }

    public static Node<Integer> buildList()
    {
        // declare your scanner in
        // declare the number of integers you're going to read (n)
        // declare your array a
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int a[] = new int[n];

        int i;

        Node<Integer> head = null, cursor = null;

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

            a[i] = in.nextInt();

            // if i == 0 that means that the head and the cursor are both
            // pointing to null and there exists no nodes in the linked list
            // therefore we need to create our first node first, and have the
            // head and the cursor both point to it.
            // After that i will not be equal to zero and we can then start executing
            // the else part of this if statement by checking if the number is odd
            // or even.
            if( i == 0){
                head = new Node<Integer>(a[i], null);
                // insert the code that makes the cursor point to the head
                cursor = head;
            } else {
                if(a[i] % 2 == 1){   // if the number is odd

                    cursor.setNext(new Node<Integer>(a[i], null));
                    // Insert the code that moves the cursor over.
                    cursor = cursor.getNext();
                }else{
                    // the number is even, so we will insert at the head and move the head
                    // to point to the newly inserted node.
                    // Use a code that is highly similar to the listHeadInsert
                    // method in the linked list class. Do not call the listHeadInsert
                    // method though, but you can use its logic
                    head = new Node<Integer>(a[i], head);
                }
            }
        }

        return head;
    }


    public static Node<Integer> shiftList(Node<Integer> head)
    {
        // in this code you will need to have some temporary variables starting

        // Ask the user to enter a number by which to shift
        // make the necessary error checking to make sure that the shift is valid
        // call this number x.
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        int i;

        Node<Integer> tmp = head;
        Node<Integer> cursor = head;

        // now you moved tmp to the location where head needs to be
        for(i = 0; i < x - 1; i++) {
            tmp = tmp.getNext();
            if(tmp == null) {
                System.out.println("Invalid Shift entered.");
                return head;
            }
        }

        // at this point find the end of the list
        while(cursor.getNext() != null)
            cursor = cursor.getNext();

        // make the last node point to the old head
        cursor.setNext(head);

        // Now write the code that moves the head over and set the Next of the node
        // before the new location of the head to null.
        head = tmp.getNext();
        tmp.setNext(null);

        return head;
    }

}

class Node<T>
{

    private T value;            // this is the data value
    private Node<T> next; // this is pointing to the next node


    // the node constructor
    public Node (T v, Node<T> n)
        {
            value = v;
            next = n;
        }

    // getters and setters for the node's value and next pointer
    public T getValue() {return value;}
    public Node<T> getNext() {return next;}
    public void setValue(T v){value = v;}
    public void setNext(Node<T> n){next = n;}

}

▲ ■ <terminated> LinkedListDemo [Java licatic System.out.println(Invalid Shift entered.); return head; 10 1 2 3 4 56789 10
Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
In this assignment you are to utilize the Node data structure provided on Blackboard. In this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT