标签:void ecif span public valueof turn seq i+1 for
顺序表
public class SequenceList {
/*
* content,节点内容
* location,节点在表中的位置(序号)
* */
private String content;
private int location;
SequenceList(String content, int location){
this.content = content;
this.location = location;
}
SequenceList(){
this.content = "-1";
this.location = -1;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
}
顺序表的各个操作
遍历
/*
* 遍历顺序表
* */
public static void showList(SequenceList[] list){
for(SequenceList one:list){
if(one!=null)
System.out.println("location = "+one.getLocation()+",content = "+one.getContent());
else
break;
}
}
插入
/*
* 插入节点
* */
public static void insertItem(SequenceList[] list, SequenceList item, int currentListLength, int insertLocation){
list[currentListLength] = new SequenceList();
if(currentListLength<insertLocation){
System.out.println("插入位置不在线性表内");
}else if(currentListLength==insertLocation+1){
System.out.println("线性表已满");
}else {
for(int i = currentListLength;i>insertLocation;i--){
list[i] = list[i-1];
list[i].setLocation(i);//重新设置节点位置
}
list[insertLocation] = item;
currentListLength++;
}
}
获得特定位置的节点
/*
* 获得特定位置的节点
* */
public static SequenceList getSpecificItem(SequenceList[] list, int location){
for(SequenceList one:list){
if(one.getLocation()==location){
return one;
}
}
return null;
}
获得某个节点的位置
/*
* 获得某个节点的位置
* */
public static int getItemLocation(SequenceList[] list, String content){
for(SequenceList one:list){
if(one.getContent().equals(content)){
return one.getLocation();
}
}
return -1;
}
删除某个位置的节点
/*
* 删除节点
* */
public static void deleteItem(SequenceList[] list, int location){
for(int i = location;;i++){
if(list[i+1]==null){
list[i] = null;
break;
}
list[i] = list[i+1];
list[i].setLocation(i);//重新设置节点位置
}
}
测试
public static void main(String[] args) {
SequenceList[] lists = new SequenceList[20];
for(int i = 0;i < 6;i++){
lists[i] = new SequenceList();
lists[i].setContent(String.valueOf(i));
lists[i].setLocation(i);
}
insertItem(lists,new SequenceList("a",5),6,5);
showList(lists);
deleteItem(lists,5);
showList(lists);
System.out.println("5号位的内容是"+getSpecificItem(lists,5).getContent());
System.out.println("内容为2的位置是"+getItemLocation(lists,"2"));
}
结果
location = 0,content = 0 location = 1,content = 1 location = 2,content = 2 location = 3,content = 3 location = 4,content = 4 location = 5,content = a location = 6,content = 5 location = 0,content = 0 location = 1,content = 1 location = 2,content = 2 location = 3,content = 3 location = 4,content = 4 location = 5,content = 5 5号位的内容是5 内容为2的位置是2
链表
持续更新
标签:void ecif span public valueof turn seq i+1 for
原文地址:https://www.cnblogs.com/Yintianhao/p/9787987.html