Question

In: Computer Science

The file has some functions incomplete/empty. Please read the description from each of those functions and...

The file has some functions incomplete/empty. Please read the description from each of those functions and add your code to complete.
You also need to test those functions in the main().

ListDemoHw.java

package hw;

public class ListDemoHw {

   public static void printLinkedList(SLLNode<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> ls1, SLLNode<Integer> 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<Integer> ls1 = generateLL1();
       attach(ls1,generateLL2(20,30));
       printLinkedList(ls1); // 3 4 1 2 20 30 20 30
   }
}

SLLNode.java to test

package hw;

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

Solutions

Expert Solution

code

package hw;

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

ListDemoHw.java

package hw;
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() {
SLLNode head=new SLLNode(3);
head.next=new SLLNode(4);
head.next.next=new SLLNode(1);
head.next.next.next=new SLLNode(2);
return head;
}
static SLLNode generateLL2(int a, int b) {
SLLNode head=new SLLNode(a);
head.next=new SLLNode(b);
head.next.next=new SLLNode(a);
head.next.next.next=new SLLNode(b);
return head;
}
static SLLNode generateLL_with_array(int[] nums) {
SLLNode head=null;
for(int i=0;i<nums.length;i++)
{
SLLNode newNode=new SLLNode(nums[i]);
if(head==null)
head=newNode;
else
{
SLLNode temp=head;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=newNode;
}
}
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
SLLNode temp=ls1;
while(temp.next!=null)
{
temp=temp.next;
}
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
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
B. Please read the brief description and respond to the questions. Please be clear and specific....
B. Please read the brief description and respond to the questions. Please be clear and specific. Check your work as this is not an easy problem. Situation: A small Canadian company has contracted to purchase 200,000 toys for 3.50 British pounds each, from a British company. The Canadians have agreed to pay for the toys in British pound sterling. The Canadians have also agreed to sell the toys to a U.S. company for USD$ 5.50 per toy. The Canadian company...
//Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT...
//Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void evenCount(const int[], int); void insertItem(int[], int &,...
Java Programming - please read the description I need this description. Inside a do/ while loop...
Java Programming - please read the description I need this description. Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods. One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result. Thanks for your help! Thanks for asking....
simple Java project// please explain every statement with reasoning. Thank you Read from a file that...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line...
EXPLANATION: Hello Tutor: please read below point 1 and 2. Those are 2 different answers from...
EXPLANATION: Hello Tutor: please read below point 1 and 2. Those are 2 different answers from 2 different people to the following question: What is the main determinant of capital structure? Explain using example from your readings or from current events’? CAN YOU READ THE BELOW 1 AND 2 ANSWERS AND MAKE ANY COMMENTS, IF YOU AGREE OR NOT AND WHY. Why you agree or disagree, an make any other contribution to the topic. The idea is to have a...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT