SLinkedList et Nœud en Java

Pour commencer, oui, c'est pour une affectation en classe, mais mon manque de compréhension sur la façon dont il fonctionne est plus élevé que ce que je veux qu'il soit.

Nous a été donné 3 classes, ils sont les suivants:

SLinkedList.java

package chapter3.linkedList;
public class SLinkedList<V> {
//instance variables.  Add the tail reference.
protected Node<V> head, tail;
protected long size;
//methods, empty list constructor first
public SLinkedList () {
head = null;
tail = null;
size = 0;
}  //end constructor of a SLinkedList
//method to add nodes to the list.  Storage space for the node
//is already allocated in the calling method
public void addFirst (Node<V> node) {
//set the tail only if this is the very first node
if (tail == null)
tail = node;
node.setNext (head);    //make next of the new node refer to the head
head = node;            //give head a new value
//change our size
size++;
}  //end method addFirst
//addAfter - add new node after current node, checking to see if we are at the tail
public void addAfter (Node<V>currentNode, Node<V>newNode) {
if (currentNode == tail)
tail = newNode;
newNode.setNext (currentNode.getNext ());
currentNode.setNext (newNode);
//change our size
size++;
}  //end method addAfter
//addLast - add new node after the tail node.  Adapted from Code Fragment 3.15, p. 118.
//Mike Qualls
public void addLast (Node<V> node) {
node.setNext (null);
tail.setNext (node);
tail = node;
size++;     
}  //end method addLast
//methods to remove nodes from the list.  (Unfortunately, with a single linked list
//there is no way to remove last.  Need a previous reference to do that.  (See
//Double Linked Lists and the code below.)
public Node<V> removeFirst () {
if (head == null)
System.err.println("Error:  Attempt to remove from an empty list");
//save the one to return
Node<V> temp = head;
//do reference manipulation
head = head.getNext ();
temp.setNext(null);
size--;
return temp;
}  //end method removeFirst
//remove the node at the end of the list.  tail refers to this node, but
//since the list is single linked, there is no way to refer to the node
//before the tail node.  Need to traverse the list.
public Node<V> removeLast () {
////declare local variables/objects
Node<V> nodeBefore;
Node<V> nodeToRemove;
//make sure we have something to remove
if (size == 0)
System.err.println("Error:  Attempt to remove fron an empty list");
//traverse through the list, getting a reference to the node before
//the trailer.  Since there is no previous reference.
nodeBefore = getFirst ();
//potential error  ??  See an analysis and drawing that indicates the number of iterations
//9/21/10.  size - 2 to account for the head and tail nodes.  We want to refer to the one before the
//tail.
for (int count = 0; count < size - 2; count++)
nodeBefore = nodeBefore.getNext ();
//save the last node
nodeToRemove = tail;
//now, do the pointer manipulation
nodeBefore.setNext (null);
tail = nodeBefore;
size--;
return nodeToRemove;
}  //end method removeLast
//method remove.  Remove a known node from the list.  No need to search or return a value.  This method
//makes use of a 'before' reference in order to allow list manipulation.
public void remove (Node<V> nodeToRemove) {
//declare local variables/references
Node<V> nodeBefore, currentNode;
//make sure we have something to remove
if (size == 0)
System.err.println("Error:  Attempt to remove fron an empty list");
//starting at the beginning check for removal
currentNode = getFirst ();
if (currentNode == nodeToRemove)
removeFirst ();
currentNode = getLast ();
if (currentNode == nodeToRemove)
removeLast ();
//we've already check two nodes, check the rest
if (size - 2 > 0) {
nodeBefore = getFirst ();
currentNode = getFirst ().getNext ();
for (int count = 0; count < size - 2; count++) {
if (currentNode == nodeToRemove) {
//remove current node
nodeBefore.setNext (currentNode.getNext ());
size--;
break;
}  //end if node found
//change references
nodeBefore = currentNode;
currentNode = currentNode.getNext ();
}  //end loop to process elements
}  //end if size - 2 > 0
}  //end method remove
//the gets to return the head and/or tail nodes and size of the list
public Node<V> getFirst () { return head; }
public Node<V> getLast () { return tail; }  
public long getSize () { return size; }
}  //end class SLinkedList

Node.java

paquet chapitre3.linkedList;

public class Node<V> {
//instance variables
private V element;
private Node<V> next;
//methods, constructor first
public Node () {
this (null, null);      //call the constructor with two args
}  //end no argument constructor
public Node (V element, Node<V> next) {
this.element = element;
this.next = next;
}  //end constructor with arguments
//set/get methods
public V getElement () { return element; }
public Node<V> getNext () { return next; }
public void setElement (V element) { this.element = element; }
public void setNext (Node<V> next) { this.next = next; }
}  //end class Node

et GameEntry.java

package Project_1;
public class GameEntry 
{
protected String name;  //name of the person earning this score
protected int score;    //the score value
/** Constructor to create a game entry */
public GameEntry(String name, int score) 
{
this.name = name;
this.score = score;
}
/** Retrieves the name field */
public String getName() 
{ 
return name; 
}
/** Retrieves the score field */
public int getScore() 
{ 
return score; 
}
/** Returns a string representation of this entry */
public String toString() 
{ 
return "(" + name + ", " + score + ")"; 
}
}

J'ai passé 3 heures à écouter sa conférence, la lecture du texte (Structures de Données et Algorithmes 5e Édition), et à la recherche par le biais de forums internet et les vidéos de youtube, mais je n'arrive pas à saisir la moindre compréhension de la façon d'utiliser le node/slinkedlist classe.

L'objet de la mission est "d'Écrire une classe qui maintient le top 10 des scores ou une application de jeu, la mise en œuvre de l'ajouter et de supprimer des méthodes, mais à l'aide d'une simple liste liée au lieu d'un tableau.

Je ne veux pas quelqu'un pour le faire pour moi, mais je veux savoir comment faire la liste chaînée. Je sais que ce ne sont PAS que dur, mais le faire avec ce code il est devenu douloureusement difficile, toute aide serait vraiment appréciée.

Vous en remercie d'avance.

Edit:

Ma fonction principale: ScoresTest.java

package Project_1;
public class ScoresTest {
/**
* @param args
*/
public static void main(String[] args) 
{
GameEntry entry;
Scores highScores = new Scores();     
entry = new GameEntry("Anna", 600);       
highScores.add(entry);
entry = new GameEntry("Paul", 720);
highScores.add(entry); 
System.out.println("The Original High Scores");
System.out.println(highScores);
entry = new GameEntry("Jill", 1150);
highScores.add(entry);
System.out.println("Scores after adding Jill");
System.out.println(highScores);
}
}

C'est, pour la plupart, exactement comment il devrait se retrouver à la recherche, mais c'est tout ce qui fait de cet ouvrage qui me jetant hors...eh bien...tout ce qui concerne les 3 catégories mentionnées ci-dessus, je pourrais le faire si ils n'ont pas été un facteur, sans trop de problème, ils sont la cause de mon vide.

  • c'est l'instanciation de la classe linkedlist étant donné que vous êtes d'avoir des ennuis avec?
  • C'est tout cela, apparemment, dans le les scores pilote nous sommes de créer nous avons à noeud d'appel, gameentry, et slinkedlist pour le faire fonctionner, et je ne suis pas sûr de la façon de le faire. Je suis sûr que ça peut pas être si difficile que ça, mais pour une raison que je ne peux pas le comprendre.
  • Vous comprenez le concept de la méthode main, et les Génériques ou est-ce votre première classe java? Êtes-vous d'utiliser un IDE?
  • Intermédiaire de Java, mais je n'ai pas fait de java pour assez un certain temps. Nous sommes à l'aide d'eclipse et je comprends le concept de la méthode main, je vais ajouter ma principale méthode que vous lisez ceci.
  • OK, je vois votre modifier maintenant, vous allez avoir à obtenir le GameEntry sur le LL c'est ce que vous voulez de votre LL pour stocker.
InformationsquelleAutor Soully | 2011-09-13