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

Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
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...
Hi there, I am going to plan of a start-up "Fruits and vegetables organic " store...
Hi there, I am going to plan of a start-up "Fruits and vegetables organic " store So Can you help me to plan "Business Model Canvas" (understanding of BMC will not be assessed in this assessment.) If it is possible can you give me 1 reference as well please? ex: Heidarkhani, A., & khomami, A.A, & Jahanbazi, Q.,& Alipoor, H. (2013). The Role of Management Information Systems ( MIS ) in Decision-Making and Problems of its Implementation, Universal Journal of...
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT