java - Linked List insert method -
In the process of writing a class that represents a linked list and I can not find the result I have written a class in which An internal node class and an insert method include adding a name and score to the list, removing the person with at least scores and limiting the list of ten. I have also created a test GUI program, when I participated and type the command, then the name of my class (gamerlist) is displayed after the number and "@" instead of the name and number in the list. I suspect that my problem is somewhere in my putting method. Any feedback would be greatly appreciated.
linked list class
class GameList {// node class private class node {String name; Int Score; Next node; // node constructor node (string name val, int scroll, node n) {name = nameVal; Score = Scrroll; Next = n; } // constructor node (string name val, int scroll) {it (name val, scrVal, null); }} Private node first; // head private node last; // Last element in the list // Constructor Public GameList () {first = zero; Final = null; } // isEmpty Method: Check if the first empty public boolean is empty () {back before == tap; } Public full size () {int count = 0; Node p = first; While (p! = Null) {count ++; P = p.next; } Calculation of return; } // Override string to strring to stringing () {stringbilder strubilar = new stringbilder (); // Use p to use link node page = first; While (p! = Null) {strbuilder.append (p.name); P = p.next; } Return strBuilder.toString (); } Public Zero Insert (string name, int score) {node node = new node (name, score); Final integer MAX_LIST_LEN = 10; If (IETT)} {first = node; First.next = Final; } And if (first.score & lt; = node.score) {node.next = first; First = node; } Else {node frontNode = first; While (FrontNode .core> node .core & amp; amp; amp; amp; FrontNode.ctx; nact! = Null) {frontNode = frontNode.next; } node.next = frontNode.next; FrontNode.next = node; } If (size ()> MAX_LIST_LEN) {node player = first; For {i.e. ii = 0; i Test Program
import java.awt. *; Import java.awt.event. *; Import javax.swing. *; Import java.util. *; / ** This class is used to display operations in the gameall class. * / Public class gamelistGui JFRM {Private GameList Top-Gamer; See the Private JTextArea list; Private JTextField cmdTextField; Public GameList () {topGamers = New GameList (); ListView = new JTextArea (); CmdTextField = new JTextField (); // Create a panel and label for the result area. JPNL resultspennel = new zpinal (new gridlate (1,2)); Resultspanel.ed (new jlab ("command result"); Add (Resultspanel, Border Layout. North); // add textArea to the center of the frame (listView); ListView.setEditable (wrong); ListView.setBackground (Color.WHITE); // Command text field JPNL CMD Panel = Create a panel and label for new zipinal (new grid layout (1,2)); CmdPanel.add (new jlabel ("command:")); CmdPanel.add (cmdTextField); Joint (CMDPanel, Border Layout. South); CmdTextField.addActionListener (New CmdTextListener ()); // set frame settings ("linked list demo"); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Pack (); SetVisible (true); } The private class applies the CmdTextListener ActionListener {Executed Public Zero Action (ActionEvent evt) {String cmdText = cmdTextField.getText (); Scanner sc = new scanner (CMDtext); String cmd = sc.next (); If (cmd.equals ("insert")) {if (sc.hasNextInt ()) {// Add index element add score = sc.nextInt (); String name = sc.next (); topGamers.insert (name, score); } ListView.setText (topGamers.toString ()); Pack (); Return; }}} Public static zero main (string [] args) {new GameListGui (); }}
I thought you had a call somewhere like this:
topGamers.toString () toString , until overridden, will print the object type and hash code. Both of which they are totally useless, do not consider the object type and hash codes as printable representation. What you want to do is represent a sensible string OK ... there is a slight warning - gamelist has only node , so you must also pull information from each node, that means, for the node , you toString () Are override. I leave this part in the form of an exercise (list), but you want to collect the information of each code in a stringbuilder , then its < Return code> toString () value
Comments
Post a Comment