单链表概念及结构
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。
链表中的数据是以结点来表示的,每个节点又分为数据data和下一节点的next引用
ListNode{
value=111,
next=ListNode{
value=222,
next=ListNode{
value=333,
next=ListNode{
value=444,
next=ListNode{
value=555,
next=null
}
}
}
}
}
Node节点
public class Node {
private int value;
private Node next;
public Node() {
}
public Node(int value){
this.value=value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return "ListNode{" +
"value=" + value +
", next=" + next +
'}';
}
}
单链表接口方法
public interface LinkList {
//头插法
public void insertHead(int value);
//任意插
public void insert(int n,int value);
//尾插法
public void insertTail( int value);
//更新元素
public void update(int value,int newValue);
//删除元素
public void remove(int value);
//判断是否含有
public boolean contains(int value);
//单链表长度
public int size();
//打印
public void display();
//清理
public void clear();
}
接口方法的实现
链表初始化
public class IList implements LinkList{
private Node head; //头结点
private int size; //链表长度
/**
* 链表实现的具体方法
*/
}
头插法-insertHead
@Override
public void insertHead(int value) {
if (head==null){
head = new Node();
head.setValue(value);
size++;
return;
}
Node node = new Node(value);
node.setNext(head);
this.head=node;
size++;
}
任意插-insert
@Override
public void insert(int n,int value) {
if (n<0 || n>size){
System.out.println("插入数据索引 "+n+" 不合法!");
return;
}
if (n==0){
insertHead(value);
return;
}
if (n==size){
insertTail(value);
return;
}
Node newNode = new Node(value);
Node node = head;
for (int index=1;index<=n;index++){
if (index==n){
newNode.setNext(node.getNext());
node.setNext(newNode);
size++;
}
node=node.getNext();
}
}
尾插法-insertTail
@Override
public void insertTail( int value) {
if (head==null){
head = new Node();
head.setValue(value);
size++;
return;
}
Node node = head;
while(node.getNext()!=null){
node=node.getNext();
}
Node nodeTail = new Node(value);
node.setNext(nodeTail);
size++;
}
节点更新-update
@Override
public void update(int value,int newValue) {
Node node=head;
for (int count=0;count<size;count++){
if (node.getValue()==value){
node.setValue(newValue);
System.out.println(String.format("您需要更新的元素%s已更新",value));
return;
}
node=node.getNext();
}
System.out.println(String.format("您需要更新的元素%s不存在",value));
}
节点删除-remove
@Override
public void remove(int value) {
if (head.getValue()==value){
head=head.getNext();
size--;
System.out.println(String.format("已删除元素%s",value));
return;
}
Node node=head;
for (int count=1;count<size;count++){
if (node.getNext().getValue()==value){
node.setNext(node.getNext().getNext());
size--;
System.out.println(String.format("已删除元素%s",value));
return;
}
node=node.getNext();
}
System.out.println(String.format("您需要删除的元素%s不存在",value));
}
判断节点是否存在-contains
@Override
public boolean contains(int value) {
Node node=head;
for (int count=0 ;count<size;count++){
if (node.getValue()==value){
return true;
}
node=node.getNext();
}
return false;
}
链表长度-size
@Override
public int size() {
return size;
}
链表打印-display
@Override
public void display() {
Node node=head;
if (node==null){
System.out.println("当前单链表为空!");
}
for (int count=0;count<size;count++){
System.out.println(node.getValue()+"-->"+node.getNext());
node=node.getNext();
}
}
链表清理-clear
@Override
public void clear() {
this.head=null;
this.size=0;
}
Demo演示
public class Demo {
public static void main(String[] args) {
IList iList = new IList();
iList.insertTail(111);
iList.insert(1,222);
iList.insert(2,333);
iList.insert(3,444);
iList.insertTail(555);
iList.display();
System.out.println("当前单链表长度为:"+iList.size());
iList.insertHead(666);
iList.insert(0,777);
iList.insertHead(888);
iList.display();
System.out.println("当前单链表长度为:"+iList.size());
iList.insert(3,999);
iList.insert(4,1010);
iList.insert(111,111);
System.out.println("当前单链表长度为:"+iList.size());
iList.display();
System.out.println("==========contains==========");
System.out.println("1010存在于单链表中吗? " + iList.contains(1010));
System.out.println("10存在于单链表中吗? " + iList.contains(10));
System.out.println("111存在于单链表中吗? " + iList.contains(111));
System.out.println("==========update==========");
iList.update(1010,1011);
iList.update(9090,1111);
System.out.println("当前单链表长度为:"+iList.size());
iList.display();
System.out.println("==========remove==========");
iList.remove(888);
iList.remove(555);
iList.remove(1011);
iList.remove(1010);
System.out.println("当前单链表长度为:"+iList.size());
iList.display();
System.out.println("==========clear==========");
iList.clear();
System.out.println("当前单链表长度为:"+iList.size());
iList.display();
}
}
结果
111-->ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}
222-->ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}
333-->ListNode{value=444, next=ListNode{value=555, next=null}}
444-->ListNode{value=555, next=null}
555-->null
当前单链表长度为:5
888-->ListNode{value=777, next=ListNode{value=666, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}
777-->ListNode{value=666, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}
666-->ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}
111-->ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}
222-->ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}
333-->ListNode{value=444, next=ListNode{value=555, next=null}}
444-->ListNode{value=555, next=null}
555-->null
当前单链表长度为:8
插入数据索引 111 不合法!
当前单链表长度为:10
888-->ListNode{value=777, next=ListNode{value=666, next=ListNode{value=999, next=ListNode{value=1010, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}}}
777-->ListNode{value=666, next=ListNode{value=999, next=ListNode{value=1010, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}}
666-->ListNode{value=999, next=ListNode{value=1010, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}
999-->ListNode{value=1010, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}
1010-->ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}
111-->ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}
222-->ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}
333-->ListNode{value=444, next=ListNode{value=555, next=null}}
444-->ListNode{value=555, next=null}
555-->null
==========contains==========
1010存在于单链表中吗? true
10存在于单链表中吗? false
111存在于单链表中吗? true
==========update==========
您需要更新的元素1010已更新
您需要更新的元素9090不存在
当前单链表长度为:10
888-->ListNode{value=777, next=ListNode{value=666, next=ListNode{value=999, next=ListNode{value=1011, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}}}
777-->ListNode{value=666, next=ListNode{value=999, next=ListNode{value=1011, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}}
666-->ListNode{value=999, next=ListNode{value=1011, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}}
999-->ListNode{value=1011, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}}
1011-->ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}}
111-->ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}}
222-->ListNode{value=333, next=ListNode{value=444, next=ListNode{value=555, next=null}}}
333-->ListNode{value=444, next=ListNode{value=555, next=null}}
444-->ListNode{value=555, next=null}
555-->null
==========remove==========
已删除元素888
已删除元素555
已删除元素1011
您需要删除的元素1010不存在
当前单链表长度为:7
777-->ListNode{value=666, next=ListNode{value=999, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=null}}}}}}
666-->ListNode{value=999, next=ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=null}}}}}
999-->ListNode{value=111, next=ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=null}}}}
111-->ListNode{value=222, next=ListNode{value=333, next=ListNode{value=444, next=null}}}
222-->ListNode{value=333, next=ListNode{value=444, next=null}}
333-->ListNode{value=444, next=null}
444-->null
==========clear==========
当前单链表长度为:0
当前单链表为空!
Process finished with exit code 0
链表和线性表都是为了存储数据而设计的数据结构,只不过线性表查询快,链表增删快。线性表物理存储单元上是连续的,长度固定且使用时可能需要动态扩容。链表由一系列节点组成,节点可以在运行时动态生成,链表长度不固定。