68838

How to link a node with a next node in Java [closed]

Question:

I will begin by creating the head node:

Node head = new Node();

To link head node to the next node. I will assign to the field of type Node in node object

The zero is to represent the number of the node. This node is number zero.

Node node = new Node(0,head); public class Node { private Object data; private Node next; public Node() { data = null; next = null; } public Node(Object x) { data = x; next = null; } public Node(Object x, Node nextNode) { data = x; next = nextNode; } }

Is this the proper way to link nodes together?

Answer1:

The way I normally see is using a LinkedList.

public class Node { public Object data; public Node next = null; Node(data) { this.data = data; } } class LinkedList{ public Node head = null; public Node end = null; void insert(Object data) { if(head == null) { head = new Node(data); end = head; } else { end.next = new Node(data); end = end.next; } } }

This is used as follows:

LinkedList= new LinkedList(); list.insert(2); list.insert(3); list.head;

Answer2:

In Java you refer to all objects via references (i.e., pointers). The only time you deal with actual values is with primitive types.

So doing next = nextNode causes next to point to the same location that nextNode points to.

<strong>TL;DR;</strong> Yes. :)

Recommend

  • Recursively add a node at a certain index on Linked List
  • Workaround for generic arrays in java
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • How to add learning rate to summaries?
  • d3.js selection conditional rendering
  • pyodbc doesn't report sql server error
  • NSScanner Loop Question
  • Display java JPanel in a JFrame
  • Avoid links criss cross / overlap in d3.js using force layout
  • how to adjust image in a panel in Java swing?
  • Is there a javascript serializer for JSON.Net?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • JTable with a ScrollPane misbehaving
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • LevelDB C iterator
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?