Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

85 users online



Java Linked List Help

Java Linked List Help

Currently viewing this thread: 1 (0 members and 1 guests)


o0MattE0o

o0MattE0o

West London
Status: Offline!

Java Linked List Help

I have the Array class that contains an object and I need to make one that is similar but using a Linked List counting an Object, and both use the interface...

Can some one please help me out as I cant find any thing on the internet, even if its a example of something similar, it just I cant find any help on the matter...

Current coding listed below of the storage and interface classes

Interface

Code:

package sets_and_data_structures;

public interface CompetitionInterface {
public void addCompetitor(Competitor comp);
public void addResult(int compNum, int result);
public int[] listCompNum();
public void startListingByResult();
public void startListingByCompNumber();
public boolean hasNext();
public boolean hasNextResult();
public Competitor next();
}

Arrary Class

Code:

package sets_and_data_structures;

import java.io.*;

public class ArrayCompetition implements Serializable, CompetitionInterface
{
private Competitor list[];
private int numOfComp;
private int nextPointer = -1;

public ArrayCompetition()
{
list = new Competitor[100];
numOfComp=0;
}

public void addCompetitor(Competitor comp)
{
list[numOfComp] = comp;
System.out.println(numOfComp+comp.toString());
numOfComp++;
nextPointer = -1; // to prevent continuing iteration after an add with restarting
}

public void addResult(int compNum, int result)
{
for (int i=0;i<numOfComp;i++)
{
if (list[i].getCompNumber()==compNum)
{
list[i].setResult(result);
}
}
}

public int[] listCompNum()
{
sortByCompNumber();
int numList[] = new int[numOfComp];
for (int i=0;i<numOfComp;i++)
{ System.out.println("listing "+i);
numList[i]=list[i].getCompNumber();
System.out.println("listed "+i);
}
return numList;

}

private void sortByResult()
{
int arrayLen = numOfComp;
for (int pass=0; pass<arrayLen-1;pass++)
{
for (int i=0; i<arrayLen -pass -1; i++)
{
if (list[i+1].hasResult()
&& (!list[i].hasResult()
|| (list[i+1].hasResult() && list[i].getResult()>list[i+1].getResult())
) )
{
Competitor t = list[i];
list[i] = list[i+1];
list[i+1] = t;
}
}
}
}//sortByResult

private void sortByCompNumber()
{
int arrayLen = numOfComp;
for (int pass=0; pass<arrayLen-1;pass++)
{
for (int i=0; i<arrayLen -pass -1; i++)
{
if (list[i].getCompNumber()>list[i+1].getCompNumber())
{
Competitor t = list[i];
list[i] = list[i+1];
list[i+1] = t;
}
}
}
}//sortByCompNumber

public void startListingByResult()
{
if (numOfComp>0)
{
sortByResult();
nextPointer = 0;
}
}

public void startListingByCompNumber()
{
if (numOfComp>0)
{
sortByCompNumber();
nextPointer = 0;
}
}

public boolean hasNext()
{
return nextPointer!=-1;
}

public boolean hasNextResult()
{
return nextPointer!=-1 && list[nextPointer].hasResult();
}

public Competitor next()
{
if (nextPointer!=-1)
{
Competitor comp = list[nextPointer];
nextPointer++;
if (nextPointer==numOfComp)
{
nextPointer = -1;
}
return comp;
}
else
{
return null;
}
}

public void listCompetitors()
{
System.out.println("Competition: number of Competitors is "+numOfComp);
for (int i=0;i<numOfComp;i++)
{
System.out.println(list[i].toString());
}
}

} // end of class ArrayCompetition

Linked List Class

Code:

package sets_and_data_structures;

import java.util.LinkedList;

public class LLCompetition implements CompetitionInterface
{
Competitor comp;
LinkedList competitors = new LinkedList();

public LLCompetition() {

}

public void addCompetitor(Competitor comp) {
competitors.add(comp);
}

public void addResult(int compNum, int result) {
}

public int[] listCompNum() {
return null;
}

public void startListingByResult() {
}

public void startListingByCompNumber() {
}

public boolean hasNext() {
return false;
}

public boolean hasNextResult() {
return false;
}

public Competitor next() {
return null;
}
}

___________________

Microsoft Technical Beta Tester
My Web Site | Windows Live Space

o0MattE0o

o0MattE0o

West London
Status: Offline!

:note:

I'm not ment to use the java.util.LinkedList

___________________

Microsoft Technical Beta Tester
My Web Site | Windows Live Space

burrito

burrito

' OR 1=1--
Status: Offline!

Why wouldn't you be able to use it? Doesn't that defeat the point?

___________________

Neverside Sucks.

o0MattE0o

o0MattE0o

West London
Status: Offline!

no the point is to make one I have done this so far but I dont think it works right

List Node Class

Code:

package sets_and_data_structures;

//Class to represent one node in a list
class ListNode
{
//Package access members; List can access these directly
Competitor data;
ListNode nextNode;

//constructor creates a ListNode that refers to competitor
ListNode(Competitor competitor) {
this(competitor, null);
}//End List Node one-argument constructor

//Constructor creates ListNode that refers to
//Object and to next ListNode
ListNode(Competitor competitor, ListNode node) {
data = competitor;
nextNode = node;
}//End ListNode two-argument constructor

//Return reference to data in node
Competitor getCompetitor(){
return data; //return Competitor in this node
}//End method getCompetitor

//Return reference to next node in list
ListNode getNext() {
return nextNode;
}//End method getNext
}//End class ListNode

List

Code:

package sets_and_data_structures;

public class List {
private ListNode firstNode;
private ListNode lastNode;
private String name; //string like "list" used in printing

//constructor creates an empty List with "list" as the name
public List() {
this("list");
}//end List no-argumnet constructor

//constructor creates an empty List with a name
public List(String listName) {
name= listName;
firstNode = lastNode = null;
}//end List one-argumnet constructor

// Insert Object at front of List
public void insertAtFront(Competitor insertItem){
if ( isEmpty() ) // firstNode and lastNode refer to same competitor
firstNode = lastNode = new ListNode( insertItem );
else // firstNode refers to new node
firstNode = new ListNode(insertItem);
} // end method insertAtFront

// Insert Object at Back of List
public void insertAtBack(Competitor insertItem){
if ( isEmpty() ) // firstNode and lastNode refer to same competitor
firstNode = lastNode = new ListNode( insertItem );
else // lastNode's nextNode refers to new node
lastNode = new ListNode(insertItem);
} // end method insertAtBack

// remove firstnode from list
public Competitor removeFromFront() throws EmptyListException {
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );

Competitor removedItem = firstNode.data; // retrieve data being removed

// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem; // return removed node data
}//end method removeFromFront

// remove firstnode from list
public Competitor removeFromBack() throws EmptyListException {
if (isEmpty()) // throw exception if List is empty
throw new EmptyListException(name);

Competitor removedItem = lastNode.data; // retrieve data being removed

// update references firstNode and lastNode
if (firstNode == lastNode)
firstNode = lastNode = null;
else {
ListNode current = firstNode;

// loop while current node does not refer to lastNode
while ( current.nextNode != lastNode )
current = current.nextNode;

lastNode = current;//current is new lastNode
current.nextNode = null;
}//end else

return removedItem;//return removed node data
}

//determine wheater list is empty
public boolean isEmpty() {
return firstNode == null; //return true if List is empty
}//end method isEmpty

//output List contents
public void print(){
if ( isEmpty() ){
//System.out.printf("Empty %s\n", name);
return;
}//end if
//System.out.printf("The %s is: ", name);
ListNode current = firstNode;

//while not at end of list, output current node's data
while(current != null){
//System.out.printf("%s ", current.data);
current = current.nextNode;
}//end while
}
}
[code]

[b]test[/b]
[code]
package sets_and_data_structures;

public class ListTest {

public static void main(String[] args) {
List list = new List();//create the List Container
Competitor comp = new Competitor(123, "Matthew", "GB");
//insert integers in list
list.insertAtFront(comp);
list.print();
}

}

competitor constructor calls the competitor class constructor

Code:

public Competitor(int compNumber, String name, String country)
{
this.compNumber = compNumber;
this.name = name;
this.country = country;
result = -1; // to signify no result yet
}

___________________

Microsoft Technical Beta Tester
My Web Site | Windows Live Space

zium

zium

Neverside Newbie
Status: Offline!

theres a lot to read here, but taking a look at

public void insertAtFront(Competitor insertItem){
if ( isEmpty() ) // firstNode and lastNode refer to same competitor
firstNode = lastNode = new ListNode( insertItem );
else // firstNode refers to new node
firstNode = new ListNode(insertItem);
} // end

the else section here makes firstNode point to the newly constructed ListNode, but fails to consider what firstNode had been pointing to previously.

o0MattE0o

o0MattE0o

West London
Status: Offline!

sorry should of took some of the stuff out thanx.

I have a look at that section

___________________

Microsoft Technical Beta Tester
My Web Site | Windows Live Space

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0098 seconds.