标签:span public linked 头部 ISE list 设定 指针 添加
JAVA单链表实现
#单链表的数据结构 public singleLinkedList(){ private int size; private Node head; public singleLInkedList(){ this.size = 0; this.head = Null; } private class Node(Object data){ Object data; Node next; public Node(Object data){ this.data = data; } } //添加头节点 public Object addHeadNode(Object data){ Node newHead = new Node(data); if(size == 0) head = newHead; else{ newHead.next = head; head = neaHead; } size++; return data; } //在头部删除元素 public Object deleteHeadNode(){ Object data = head.data; head = head.next; size--; return data; } //查找指定元素,找到了返回Node,找不到返回Null public Node find(Object data){ //设定指针遍历 Node current = head; while(current != null){ if(current.next.data == data) return curren else current = current.next; } return null; } //删除指定的元素,删除成功返回true public boolean delete(Object data){ Node curren = head; while(current.next.data!=data&¤t.next!=null){ current = current.next; } if(current.next != null){ current.next = current.next.next; size--; return true; } else return false; } //判断链表是否未空 public boolean isEmpty(){ if(size>0) return false; else return true; } }
标签:span public linked 头部 ISE list 设定 指针 添加
原文地址:https://www.cnblogs.com/jellyj/p/9876668.html