设为首页 加入收藏

TOP

Java实现链表(二)
2017-03-03 08:15:44 】 浏览:419
Tags:Java 实现
n;
? ? ? ? }
? ? ? ?
? ? ? ? //如果索引为0,表示删除头节点
? ? ? ? if( index == 0){
? ? ? ? ? ? this.deleteByHead(); //调用删除头节点方法
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ?
? ? ? ? //遍历链表
? ? ? ? while(temp != null){
? ? ? ? ? ? //找到目标节点的上一个节点
? ? ? ? ? ? if(index-1 == i){
? ? ? ? ? ? ? ? System.out.println("正在删除节点:" + this.head.next.getData());
? ? ? ? ? ? ? ? temp.next = temp.next.next;
? ? ? ? ? ? ? ? System.out.println("删除成功");
? ? ? ? ? ? ? ? size --; //节点数减1
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? temp = temp.next; //继续遍历
? ? ? ? ? ? i ++;
? ? ? ? }
? ? ? ?
? ? }
? ?
? ? //更新指定位置的节点
? ? public void updateByIndex(int index, E e){
? ? ? ? if(index < 0 || index > this.size){
? ? ? ? ? ? System.err.println("索引小于0或者大于链表长度:" + index);
? ? ? ? }
? ? ? ?
//? ? ? ? if(index == 0){
//? ? ? ? ? ? this.updateByHead(e); //如果index==0,更新头节点
//? ? ? ? }
? ? ? ?
? ? ? ? //遍历链表
? ? ? ? int i = 0;
? ? ? ? Node temp = this.head;
? ? ? ? while(temp != null){
? ? ? ? ? ? if(index == i){
? ? ? ? ? ? ? ? temp.data = e;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? temp = temp.next;
? ? ? ? ? ? i ++;
? ? ? ? }


? ? }
? ?
? ? //更新头节点
? ? public void updateByHead(E e){
? ? ? ? this.head.data = e; //为头节点重新赋值
? ? }
? ?
? ? //打印链表中的所有数据 从头节点一直到尾节点。 (1).head? (2).head.next (3).head.next.next (n).head.next.n.n
? ? public void display(){
? ? ? ? Node temp = this.head;
? ? ? ? //从头节点开始遍历到为尾节点
? ? ? ? while(temp != null){
? ? ? ? ? ? System.out.println(temp.getData());
? ? ? ? ? ? temp = temp.next;? //指向下一节点。
? ? ? ? }
? ? }
? ?
? ? //返回链表list
? ? public List> findAll(){
? ? ? ? List> list = new ArrayList>();
? ? ? ? Node temp = this.head;
? ? ? ? while(temp != null){
? ? ? ? ? ? list.add(temp);
? ? ? ? ? ? temp = temp.next;
? ? ? ? }
? ? ? ? return list;
? ? }
? ?
? ? //查找指定位置结点
? ? public Node findByIndex(int index){
? ? ? ? Node temp = this.head;
? ? ? ? int i = 0;
? ? ? ?
? ? ? ? //参数校验,返回null
? ? ? ? if(index < i || index > this.size){
? ? ? ? ? ? System.err.println("参数大于链表长度或者小于0:" + index);
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ?
? ? ? ? //如果index == 0,返回头节点
? ? ? ? if(index == 0){
? ? ? ? ? ? return this.head;? //如果下标为1,直接返回头节点
? ? ? ? }
? ? ? ?
? ? ? ? //遍历链表进行匹配
? ? ? ? while(temp != null){
? ? ? ? ? ? if(i == index){
? ? ? ? ? ? ? ? return temp; //匹配节点
? ? ? ? ? ? }
? ? ? ? ? ? temp = temp.next; //继续遍历
? ? ? ? ? ? i ++;
? ? ? ? }
? ? ? ? return null;
? ? }
? ?
? ? //获得链表节点数量
? ? public int getSize(){
? ? ? ? return this.size;
? ? }
? ?
? ? //获取当前头节点
? ? public Node getHead(){
? ? ? ? return this.head;
? ? }
? ?
? ? //测试我的链表
? ? public static void main(String[] args) {
? ? ? ? ? ? LinkedList linkedList = new LinkedList();
? ? ? ? ? ?
? ? ? ? ? ? //添加节点
? ? ? ? ? ? linkedList.addNode(1);
? ? ? ? ? ? linkedList.addNode(2);
? ? ? ? ? ? linkedList.addNode(3);
? ? ? ? ? ? linkedList.addNode(4);
? ? ? ? ? ? linkedList.addNode(5);
? ? ? ? ? ? linkedList.addNode(6);
? ? ? ? ? ?
? ? ? ? ? ? linkedList.insert(6, 9);
? ? ? ? ? ? linkedList.updateByIndex(6, 9);
? ? ? ? ? ? linkedList.display();


? ? }


}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Hibernate分页模糊查询 下一篇Java实现顺序表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目