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...
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
This much like the single linked list assignment. I am giving you the majority of the...
This much like the single linked list assignment. I am giving you the majority of the code and left a couple of functions for you to complete. I had intended to try to do some video clips of my own lecture but I am not going to have time to get those completed (at least not at a quality I want). You might take a look at these sites for more help outside just zyBooks. #include <stdio.h> #include <stdlib.h> struct...
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, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
Q. In java, I would like to know, especially how the program detect there is an...
Q. In java, I would like to know, especially how the program detect there is an error which is user should put only numerical data but if user put letter data (five, sixty instead of 5,60), *I want the program display "Invalid data. Please try again" instead of long error message(from the IDE)* when user put letter data(five, sixty stuff...) and GO BACK TO THE BEGINNING SO USER CAN PUT THE DATA AGAIN! Write a program that counts change. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT