学习笔记-链表查询优化

来源:互联网 发布:淘宝联盟鹊桥手机登录 编辑:程序博客网 时间:2024/06/03 17:07

已测试

public Node node(int index){   //按索引找到相应节点的方法,实现List接口必须有的          Node temp = null;          if(first!=null){              if (index < (size >> 1)) {   //如果索引号是前半部分的则从头结点开始遍历查找                  temp = first;                  for(int i=0;i<index;i++){                      temp = temp.next;                  }              }else{                  temp = last;         //如果索引号是后半部分的则从尾结点开始遍历查找                  for (int i = size - 1; i > index; i--){                      temp = temp.previous;                  }              }          }          return temp;      }  


原创粉丝点击