我正在尝试使用数组作为基础结构在Java中实现链接列表。但是,我不确定如何在元素之后插入数组中的元素,然后将数组向下移动一个
class linkedList{
char data[];
int next;
//constructor
public linkedList(int MAX){
data = new char[MAX];
}
public void insertFirst(char d){
if(data[next]==0){
data[next] = d;
next++;
}
else{
System.out.println("list is full");
}
}
public void insertAfter (char after ,char value){
next=0;
while(data[next] !=after){
next++;
}
char temp = data[next+1];
data[next+1] = value;
}
public void printList(){
for(int i=0;i<data.length;i++){
System.out.print(data[i]);
}
}
}
public class myLinkedList {
public static void main(String args[]) {
linkedList list = new linkedList(9);
list.insertFirst('T');
list.insertFirst('H');
list.insertFirst('L');
list.insertAfter('H', 'z');
list.printList();
}
}
这也将被视为链接列表吗?
这不是链接列表。您所拥有的类似于阵列列表,因为数组被用作基础数据结构。链接列表由一系列节点组成,每个节点链接到下一个节点。链接列表通过在当前节点上调用node.next()之类的内容,直到达到列表的末尾为止。
。如果要在达到尺寸限制后将另一个元素插入列表结构中,则需要创建一个新数组,复制旧数组的内容,然后将新元素插入数组中。您可以使用System.ArrayCopy()执行复制或在数组中移动项目。