1.链表是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。
使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。
2.单链表是链表中结构最简单的。一个单链表的节点(Node)分为两个部分,第一个部分(data)保存或者显示关于节点的信息,另一个部分存储下一个节点的地址。最后一个节点存储地址的部分指向空值。
3.链表实现
package signle; /** * @Author lizhilong * @create 2019/11/7 17:12 * @desc */ public class SignleLinkList { private int size; private Node head; public SignleLinkList() { this.size = 0; this.head = null; } /** * 链表头后面增加节点 * * @param object * @return */ public Object addAfterHead(Object object) { Node newNode = new Node(object); if (size == 0) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } size++; return object; } /** * 在链表头前增加节点 * * @param object * @return */ public Object addbeforeHead(Object object) { Node newNode = new Node(object); if (size == 0) { head = newNode; } else { newNode.next = head; head = newNode; } size++; return object; } /** * 从头删除 * * @return */ public Object remove() { Object data = head.data; head = head.next; size--; return data; } /** * 查找位置 * * @param object * @return */ public int find(Object object) { Node h = head; int index = -1; while (h.data != object) { index++; h = h.next; } return index + 1; } /** * 删除指定元素 * * @param object * @return */ public boolean delete(Object object) { if (size == 0) { return false; } Node current = head; Node preNode = head; while (current.data != object) { if (current.next == null) { return false; } else { preNode = current; current = current.next; } } //目标元素是头节点 if (current == head) { head = current.next; size--; } else { preNode.next = current.next; size--; } return true; } /** * 打印链表所有元素 */ public void display() { Node current = head; StringBuilder builder = new StringBuilder(); builder.append("["); while (current.next != null) { builder.append(current.data.toString() + "->"); current = current.next; } builder.append(current.data.toString()); builder.append("]"); System.out.println(builder.toString()); } /** * 单链表反转 */ public SignleLinkList reverse() { SignleLinkList rev = new SignleLinkList(); Node h = head; while (h.next != null){ rev.addbeforeHead(h.data); h = h.next; } rev.addbeforeHead(h.data); return rev; } public int getSize(){ return size; } /** * @Author lizhilong * @create 2019/11/7 17:10 * @desc */ public class Node { private Object data; private Node next; public Node(Object data) { this.data = data; } } }
4.链表应用之实现栈
package signle; /** * @Author lizhilong * @create 2019/11/8 15:32 * @desc */ public class Stack { private SignleLinkList linkList; public Stack(){ linkList = new SignleLinkList(); } /** * 入栈 * @param data */ public void push(Object data){ linkList.addbeforeHead(data); } public Object pop(){ return linkList.remove(); } public void display(){ linkList.display(); } }
5.链表应用之实现队列
package signle; /** * @Author lizhilong * @create 2019/11/8 16:00 * @desc */ public class Queue { private SignleLinkList linkList; public Queue(){ linkList = new SignleLinkList(); } public void put(Object data){ linkList.addAfterHead(data); } public Object take(){ return linkList.remove(); } public void display(){ linkList.display(); } }