Acetamide Acetamide - 19 days ago 12
Java Question

How to store multiple values/data into single node in linked list using Java?

I am creating a system which store data using linked list in GUI form.
I have encountered some problem when it comes to store multiple data in single node in linked list.
What I am doing is a library system, so what I need is store the book name, author name, ISBN number and number of copies of book into a single node in linked list. The information are come from user input such as jTextField1. My lecturer had taught me how to insert single data but now I need to insert multiple data.

I have recreate the constructor and the getter method in Node class and change it in the LinkedList class as well. But for some reason the data seems not to add into the LinkedList, when I display the list in TextArea, all it shows was NULL but not the data I just input. Why is that?

This is my Node class:

public class Node
{
String bookname;
String author;
int isbn;
int number;
Node next;

Node()
{
bookname = null;
author = null;
isbn = 0;
number = 0;
next = null;
}

Node(String bookname, String author, int isbn, int number)
{
this.bookname = bookname;
this.author = author;
this.isbn = isbn;
this.number = number;
}

String getName()
{
return bookname;
}

String getAuthor()
{
return author;
}

int getisbn()
{
return isbn;
}

int getnumber()
{
return number;
}

Node getNext()
{
return next;
}

void setNext(Node next)
{
this.next=next;
}
}


This is my Linked list class:

public class LinkedList
{
Node node = new Node();
Node head;

LinkedList()
{
head=null;
}

Node getHead()
{
return head;
}

public void addNode(String bookname, String author, int isbn, int number)
{
Node newNode = new Node(bookname, author, isbn, number);
newNode.next = head;
head = newNode;
JOptionPane.showMessageDialog(null,"Node added to the list.");
   }
}

public String displayNode()
{
Node current = head;
String output = "";

while(current!= null)
{
output+="Book Name: "+current.getName()+" Author: "+current.getAuthor()+" ISBN Number: "+current.getisbn()+" Number of copies: "+current.getnumber();
current=current.getNext();
}

return(output+"NULL");
}


And this is what I have in my JFrame:

public class AddBook extends javax.swing.JFrame {
static String name;
static String author;
static int isbn;
static int number;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

LinkedList list = new LinkedList();
list.addNode(jTextField2.getText(), jTextField3.getText(), Integer.parseInt(jTextField4.getText()), Integer.parseInt(jTextField1.getText()));
}
}


Another JFrame with display:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
LinkedList list = new LinkedList();
jTextArea1.setText(list.displayNode());
}


Anyone can help me with this? Thank you.

Answer

Think of a node like about a bucket that can contain any data, not only a simple number (as a majority of tutorials describe).

Instead of the int num field in your Node class, you could use any reference type you want to, for example, a List<Integer>:

public class Node  {
    List<Integer> data;
    ...
}

In that case, you need to rewrite the constructor and the getter as well (by changing a parameter type and a return type respectively).

Another good solution that doesn't break the single responsibility principle would be using a wrapper class (e.g. Data):

class Data {
    private Integer i;
    private String s;
}
Comments