Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Sets and Data Structures


  • Please log in to reply

#1
o0MattE0o

o0MattE0o

    Member

  • Member
  • PipPip
  • 29 posts
:tazz:
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
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
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
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;
}
}

Edited by o0MattE0o, 31 May 2005 - 03:50 PM.

  • 0

Advertisements


#2
o0MattE0o

o0MattE0o

    Member

  • Topic Starter
  • Member
  • PipPip
  • 29 posts
:Note:

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

#3
o0MattE0o

o0MattE0o

    Member

  • Topic Starter
  • Member
  • PipPip
  • 29 posts
I have done this so far but I dont think it works right

List Node Class
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
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
public Competitor(int compNumber, String name, String country)
  {
    this.compNumber = compNumber;
    this.name = name;
    this.country = country;
    result = -1;  // to signify no result yet
  }

Edited by o0MattE0o, 01 June 2005 - 03:35 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP