java - The data is not added into linked list and display correctly. Why? -
i creating system store data using linked list in gui form. have encountered problem when comes store , display linked list. looks after input data, did not stored linked list
what doing library system, need store book name, author name, isbn number , number of copies of book single node in linked list. information come user input such jtextfield1. lecturer had taught me how insert single data need insert multiple data.
i have recreate constructor , getter method in node class , change in linkedlist class well. reason data seems not add linkedlist, when display list in textarea, shows null not data input. why that?
this node class:
public class node { string name; string author; int isbn; int number; node next; node() { name = null; author = null; isbn = 0; number = 0; next = null; } node(string name, string author, int isbn, int number) { this.name = name; this.author = author; this.isbn = isbn; this.number = number; } string getname() { return name; } string getauthor() { return author; } int getisbn() { return isbn; } int getnumber() { return number; } node getnext() { return next; } void setnext(node next) { this.next=next; } }
this linked list class:
public class linkedlist { node node = new node(); node head; linkedlist() { head=null; } node gethead() { return head; } public void addnode(string name, string author, int isbn, int number) { node newnode = new node(name, author, isbn, number); newnode.next = head; head = newnode; joptionpane.showmessagedialog(null,"node added 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 have in addbook 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 display:
private void jbutton1actionperformed(java.awt.event.actionevent evt) { linkedlist list = new linkedlist(); jtextarea1.settext(list.displaynode()); }
the data class:
public class data { string name; string author; int isbn; int number; public data(string name, string author, int isbn, int number) { } }
anyone can me this? thank you.
think of node bucket can contain data, not simple number (as majority of tutorials describe).
instead of int num
field in node
class, use reference type want to, example, list<integer>
:
public class node { list<integer> data; ... }
in case, need rewrite constructor , getter (by changing parameter type , return type respectively).
another solution doesn't break the single responsibility principle using wrapper class (e.g. data
):
class data { private integer i; private string s; }
Comments
Post a Comment