Question

In: Computer Science

Hi, I would like to test a java program. I am learning linked list and going...

Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes.

For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents

and add 15 to the list again and display the list contents and delete 13 from the list

and display the list contents and lastly delete 12 from the list and display the list contents

In order to test like this, with this reference below how can I make a simple code?

If you can please include comments to catch up the codes, it would help me a lot!

public class IntegerNode {

private int item;

private IntegerNode next;

public IntegerNode(int newItem) {

item = newItem;

next = null;

} // end constructor

public IntegerNode(int newItem, IntegerNode nextNode) {

item = newItem;

next = nextNode;

} // end constructor

public void setItem(int newItem) {

item = newItem;

} // end setItem

public int getItem() {

return item;

} // end getItem

public void setNext(IntegerNode nextNode) {

next = nextNode;

} // end setNext

public IntegerNode getNext() {

return next;

} // end getNext

} // end class IntegerNode

Solutions

Expert Solution

Sample program :

import java.util.LinkedList;

public class IntegerNode {

public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
  
// Use add() method to add elements in the list
list.add("12");
list.add("13");
list.add("16");
// Displaying the linkedlist
System.out.println("LinkedList:" + list);
  
// Using set() method to replace Geeks with GFG
System.out.println("The Object that is replaced is: " + list.add("15"));
  
System.out.println("LinkedList:" + list);
  
// Using set() method to replace 20 with 50
System.out.println("The Object that is replaced is: " + list.remove(1));
  
// Displaying the modified linkedlist
System.out.println("The new LinkedList is:" + list);
  
System.out.println("The Object that is replaced is: " + list.remove(0));
  
System.out.println("The new LinkedList is:" + list);
}
}

As per your question above, this program will help you to understand the linked List follow of work. If you need more clarification from the linkedlist comment with your need full question.


Related Solutions

how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
Hi, I am having a little difficulty understanding how an auditor would test the Inventory and...
Hi, I am having a little difficulty understanding how an auditor would test the Inventory and Warehousing Cycle. What is the difference (Audit Objectives) between substantive tests of transactions for inventory and tests of balances for inventory? How would an auditor use the transaction related objectives to perform Substantive tests of transactions in Inventory? How would that differ from Tests of Details of balances (balance related objectives) in inventory with examples? Thank you so much as I am a little...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT