Question

In: Computer Science

I need this in java using textpad. I am missing a few lines where I added...

I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. I need it to work with the generic version of SLLNode. It is posted at the bottom.

public class ListDemoHw {

        public static void printLinkedList(SLLNode node) {
                // display all elements in the linked list
                while(node != null) {
                        System.out.print(node.info + " ");
                        node = node.next; // move to the next node
                }       
                System.out.println();
        }
        static SLLNode generateLL1() {
                // Create/return a linked list that has {3, 4, 1, 2}
                // Note that this is not quite a useful function. Just for practice purpose
        }
        static SLLNode generateLL2(int a, int b) {
                // Create/return a linked list that has {a, b, a, b}
                // eg) generateLL2(10,20) returns a list {10,20,10,20}
        }
        static SLLNode generateLL_with_array(int[] nums) {
                // Creat/return a linked list using the given int array
                // Return null if the array is empty (size is zero).
                // eg) generateLL3(new int[]{2,3,4}) returns a list {2,3,4}
        }
        static void attach(SLLNode ls1, SLLNode ls2) {
                // Given two linked lists, attach the second list at the end of the first list
                // eg) Suppose ls1={1,2,3}, ls2={50,60} as lists, attach(ls1, ls2) makes ls1 = {1,2,3,50,60}
                // Assume ls1 is not empty.
                // Hint: You need to go to the last node of ls1 and make a connection from it to the ls2
        }
        public static void main(String[] args) {
                printLinkedList(generateLL1()); // 3 4 1 2
                printLinkedList(generateLL2(20,30)); // 20 30 20 30
                printLinkedList(generateLL_with_array(new int[] {2})); // 2
                printLinkedList(generateLL_with_array(new int[] {2,3,4,5})); // 2 3 4 5
                SLLNode ls1 = generateLL1();
                attach(ls1,generateLL2(20,30));
                printLinkedList(ls1); // 3 4 1 2 20 30 20 30
        }
}

---------------------------------------------------------------------------------------------------

public class SLLNode<E> {
        E info;
        SLLNode<E> next;
        public SLLNode(E val) {
                info = val;
                next = null;
        }
}

Solutions

Expert Solution

Solution:

ListDemoHw.java

public class ListDemoHw {

    public static void printLinkedList(SLLNode node) {
        // display all elements in the linked list
        while (node != null) {
            System.out.print(node.info + " ");
            node = node.next; // move to the next node
        }
        System.out.println();
    }

    static SLLNode generateLL1() {
        // Create/return a linked list that has {3, 4, 1, 2}
        // Note that this is not quite a useful function. Just for practice purpose
        SLLNode head = new SLLNode(3); // 3 at head

        // Nodes for 4, 1, 2
        SLLNode node2 = new SLLNode(4);
        SLLNode node3 = new SLLNode(1);
        SLLNode node4 = new SLLNode(2);

        // Connect nodes
        head.next = node2;
        node2.next = node3;
        node3.next = node4;

        return head;
    }

    static SLLNode generateLL2(int a, int b) {
        // Create/return a linked list that has {a, b, a, b}
        // eg) generateLL2(10,20) returns a list {10,20,10,20}
        int[] nums = { a, b, a, b };
        return generateLL_with_array(nums);
    }

    static SLLNode generateLL_with_array(int[] nums) {
        // Create/return a linked list using the given int array
        // Return null if the array is empty (size is zero).
        // eg) generateLL3(new int[]{2,3,4}) returns a list {2,3,4}

        // Head
        SLLNode head = new SLLNode(nums[0]);

        SLLNode temp = head; // temp at head
        for (int i = 1; i < nums.length; i++) {
            SLLNode newNode = new SLLNode(nums[i]); // new node
            temp.next = newNode; // connecting newnode to temp
            temp = newNode; // changing temp
        }

        return head;
    }

    static void attach(SLLNode ls1, SLLNode ls2) {
        // Given two linked lists, attach the second list at the end of the first list
        // eg) Suppose ls1={1,2,3}, ls2={50,60} as lists, attach(ls1, ls2) makes ls1=
        // {1,2,3,50,60}
        // Assume ls1 is not empty.
        // Hint: You need to go to the last node of ls1 and make a connection from it to
        // the ls2

        // Get the tail of first linked list
        SLLNode temp = ls1;
        while (temp.next != null) {
            temp = temp.next;
        }

        // Connect tail of ls1 with head of ls2
        temp.next = ls2;
    }

    public static void main(String[] args) {
        printLinkedList(generateLL1()); // 3 4 1 2
        printLinkedList(generateLL2(20, 30)); // 20 30 20 30
        printLinkedList(generateLL_with_array(new int[] { 2 })); // 2
        printLinkedList(generateLL_with_array(new int[] { 2, 3, 4, 5 })); // 2 3 4 5
        SLLNode ls1 = generateLL1();
        attach(ls1, generateLL2(20, 30));
        printLinkedList(ls1); // 3 4 1 2 20 30 20 30
    }
}

SLLNode.java

public class SLLNode<E> {
  E info;
  SLLNode<E> next;

  public SLLNode(E val) {
    info = val;
    next = null;
  }
}

Output:

Screenshots:


Related Solutions

I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
I am happy to post this in two different posts! I just need the first few...
I am happy to post this in two different posts! I just need the first few parts in order to ask the rest. If possible, please answer all On January 1, 2018, Entity A issued 8% bonds dated January 1, 2018, with a face amount of $10 million. The bonds mature in 2022 (5 years). For bonds of similar risk and maturity, the market yield is 10%. Interest is paid semiannually on June 30 and December 31. A. What was...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I am working on an assignment using SQL Server Management and I need to print an...
I am working on an assignment using SQL Server Management and I need to print an ERD to a single page as a PDF file. I am not sure how to do this especially because the diagram is rather large... I am using SQL Server Management Studio I have created an Entity relationship diagram for AdventureWorks that includes all product tables. There are many tables.How do I print it to a single page?
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
I need to draw my first name using Turtle. I am struggling with "P" and "A"...
I need to draw my first name using Turtle. I am struggling with "P" and "A" this is what I have import turtle turtle.setup(800, 700) window = turtle.Screen() window.reset() window.bgcolor('cornsilk') t = turtle.Turtle() t.color('blue') t.pencolor('red') t.turtlesize(6) t.speed(2) t.up() t.setposition(-50, 0) t.pendown()#Drawing letter T t.forward(40) t.back(20) t.right(90) t.forward(50) t.left(90) t.penup() t.forward(70) t.left(90) t.forward(25) t.pendown() t.circle(25)# Drawing letter O t.penup() t.left(180) t.forward(25) t.left(90) t.forward(10) t.pendown()#Drawing letter N t.left(90) t.forward(50) t.right(150) t.forward(60) t.left(150) t.forward(53) t.back(53) t.right(90) turtle.done()
Original scenario, I am going to place you in a situation where you need to advise...
Original scenario, I am going to place you in a situation where you need to advise a client on an advertising decision and provide a rationale for your decision. Personally a simple and tried advertising system is lawn signs. Lawn signs is an extremely inexpensive form of advertising that allows for a great deal of selectivity. A manager can order about 100 Yard signs for about $300.00 , they can be fully decked out and have a great design. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT