I am trying to implement an array-based linked list which has to be orderer alphabetically. I have the code to insert and node so far but wanted to check if i that is correct and if someone can help me write a main method to display elements of the list. Below is the code i have so far.
package listpackage;
import java.io.IOException;
public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
public ArrayLL(){
list = new ANode[MAX_CAP];
list[list.length-1] = new ANode(null, -1);
for(int i = 0; i < list.length-1; i++){
list[i] = new ANode(null, i+1);
}
size = 0;
}
public void addElem(String s) throws IOException{
if(size == 0){
ANode a = new ANode(s, -1);
list[0] = a;
size++;
}else if(size == MAX_CAP + 1){
throw new IOException("List is full");
}else{
ANode a = list[0];
for(int i = 0; i< size; i++){
if(a.getData().compareTo(s)>0){
ANode b = new ANode(s,i+1);
for(int j = size; j > i; j--){
list[j] = list[j+1];
}
list[i] = b;
size++;
break;
}else{
ANode c = new ANode(s,-1);
list[size+1] = c;
}
}
}
}
public int getSize(){
return size;
}
}
class ANode{
private String data;
private int link;
public ANode(String d, int l){
data = d;
link = l;
}
public String getData(){
return data;
}
public int getLink(){
return link;
}
}