什么是单链表
一种链式存取的数据结构,单链表中的数据是以结点的形式存在,每一个结点是由数据元素和下一个结点的存储的位置组成。单链表与数组相比的最大差别是:单链表的数据元素存放在内存空间的地址是不连续的,而数组的数据元素存放的地址在内存空间中是连续的,这也是为什么根据索引无法像数组那样直接就能查询到数据元素。
即一个节点指向下一个节点
示例图解(一):
示例图解(二):
链表存储的结点
public class SingleLinkTable<T> {
public class Node {
private T t;
private Node next;
public Node(T t, Node next) {
this.t = t;
this.next = next;
}
public Node(T t) {
this.t = t;
}
}
public Node head;//头结点
public int size;
//构造函数
public SingleLinkTable(Node head, int size) {
this.head = null;
this.size = 0;
}
public SingleLinkTable() {
}
}
基础操作办法
添加头部
//链表头部添加元素
public void addHead(T t) {
Node note = new Node(t);
note.next = this.head;
this.head = note;
this.size++;
}
添加尾部
//向链表尾部插入元素
public void addLast(T t) {
this.add(t, this.size);
}
//向链表中添加元素
public void add(T t, int index) {
if (index < 0 || index > this.size) {
throw new IllegalArgumentException("index is error");
}
if (index == 0) {
this.addHead(t);
return;
}
Node nodehead = this.head;//找到头节点
//找到要插入节点的前一个节点
for (int i = 0; i < index - 1; i++) {
nodehead = nodehead.next;
}
Node node = new Node(t);//要插入的节点
//要插入的节点的下一个节点指向nodehead节点的下一个节点
node.next = nodehead.next;
//nodehead的下一个节点指向要插入的节点node
nodehead.next = node;
this.size++;
}
删除第一个元素
//删除链表的第一个元素
public T removeFirst() {
if (this.head == null) {
Log.i("SingleLinkTable==", "无可删除元素");
return null;
}
Node delNode = this.head;
this.head = this.head.next;
delNode.next = null;//不太懂呀
this.size--;
return delNode.t;
}
删除某个元素
//删除链表元素
public void remove(T t) {
if (this.head == null) {
Log.i("SingleLinkTable==", "无可删除元素");
return;
}
//要删除的元素与头结点的元素相同
while (head != null && head.t.equals(t)) {
head = head.next;
this.size--;
}
//要删除的元素和除头结点外的其他节点元素相同
Node currentNode = this.head;
while (currentNode != null && currentNode.next != null) {
if (currentNode.next.t.equals(t)) {
currentNode.next = currentNode.next.next;
this.size--;
} else {
currentNode = currentNode.next;
}
}
}
是否包含
//判断链表中是否包含某个元素
public boolean isContains(T t) {
Node current = this.head;
while (current != null) {
if (current.t.equals(t)) {
return true;
} else {
current = current.next;
}
}
return false;
}
完整代码:
/**
* 链表
*/
class LinkTableActivity : AppCompatActivity(), View.OnClickListener {
//单链表
var linkTable: SingleLinkTable<Int>? = SingleLinkTable()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_link_table)
init_Listener()
initSingleLinkTable()
}
fun init_Listener() {
bt_single_addfirst.setOnClickListener(this)
bt_single_addend.setOnClickListener(this)
bt_single_remove.setOnClickListener(this)
bt_single_removefirst.setOnClickListener(this)
bt_single_iscontions.setOnClickListener(this)
}
/**
* 单链表
*/
fun initSingleLinkTable() {
//往链表内添加数据
for (i in 0..9) {
linkTable!!.addHead(i)
}
linkTable!!.logMess()
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.bt_single_addfirst -> {//添加头部元素
linkTable!!.addHead(100)
linkTable!!.logMess()
}
R.id.bt_single_addend -> {
linkTable!!.addLast(-1)
linkTable!!.logMess()
}
R.id.bt_single_remove -> {//删除元素
linkTable!!.remove(3)
linkTable!!.logMess()
}
R.id.bt_single_removefirst -> {//删除第一个元素
linkTable!!.removeFirst()
linkTable!!.logMess()
}
R.id.bt_single_iscontions -> {//是否包含
linkTable!!.isContains(1)
Log.i("SingleLinkTable==", ""+linkTable!!.isContains(1))
}
}
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".javaknowledge.LinkTableActivity">
<Button
android:id="@+id/bt_single_addfirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/unPass_color"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="添加头部元素"
android:textAllCaps="false"
android:textColor="@android:color/white" />
<Button
android:id="@+id/bt_single_addend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/unPass_color"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="添加尾部元素"
android:textAllCaps="false"
android:textColor="@android:color/white" />
<Button
android:id="@+id/bt_single_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/unPass_color"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="删除元素"
android:textAllCaps="false"
android:textColor="@android:color/white" />
<Button
android:id="@+id/bt_single_removefirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/unPass_color"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="删除第一个元素"
android:textAllCaps="false"
android:textColor="@android:color/white" />
<Button
android:id="@+id/bt_single_iscontions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/unPass_color"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="是否包含"
android:textAllCaps="false"
android:textColor="@android:color/white" />
</LinearLayout>
SingleLinkTable.class
public class SingleLinkTable<T> {
public class Node {
private T t;
private Node next;
public Node(T t, Node next) {
this.t = t;
this.next = next;
}
public Node(T t) {
this.t = t;
}
}
public Node head;//头结点
public int size;
//构造函数
public SingleLinkTable(Node head, int size) {
this.head = null;
this.size = 0;
}
public SingleLinkTable() {
}
/**
* 操作方法
*
* @return
*/
//获取链表元素的个数
public int getSize() {
return this.size;
}
//判断链表是否为空
public boolean isEmpty() {
return this.size == 0;
}
//链表头部添加元素
public void addHead(T t) {
Node note = new Node(t);
note.next = this.head;
this.head = note;
this.size++;
}
//向链表尾部插入元素
public void addLast(T t) {
this.add(t, this.size);
}
//向链表中添加元素
public void add(T t, int index) {
if (index < 0 || index > this.size) {
throw new IllegalArgumentException("index is error");
}
if (index == 0) {
this.addHead(t);
return;
}
Node nodehead = this.head;//找到头节点
//找到要插入节点的前一个节点
for (int i = 0; i < index - 1; i++) {
nodehead = nodehead.next;
}
Node node = new Node(t);//要插入的节点
//要插入的节点的下一个节点指向nodehead节点的下一个节点
node.next = nodehead.next;
//nodehead的下一个节点指向要插入的节点node
nodehead.next = node;
this.size++;
}
//删除链表元素
public void remove(T t) {
if (this.head == null) {
Log.i("SingleLinkTable==", "无可删除元素");
return;
}
//要删除的元素与头结点的元素相同
while (head != null && head.t.equals(t)) {
head = head.next;
this.size--;
}
//要删除的元素和除头结点外的其他节点元素相同
Node currentNode = this.head;
while (currentNode != null && currentNode.next != null) {
if (currentNode.next.t.equals(t)) {
currentNode.next = currentNode.next.next;
this.size--;
} else {
currentNode = currentNode.next;
}
}
}
//删除链表的第一个元素
public T removeFirst() {
if (this.head == null) {
Log.i("SingleLinkTable==", "无可删除元素");
return null;
}
Node delNode = this.head;
this.head = this.head.next;
delNode.next = null;//不太懂呀
this.size--;
return delNode.t;
}
//判断链表中是否包含某个元素
public boolean isContains(T t) {
Node current = this.head;
while (current != null) {
if (current.t.equals(t)) {
return true;
} else {
current = current.next;
}
}
return false;
}
public String ToSting() {
StringBuffer sb = new StringBuffer("");
Node current = this.head;
while (current != null) {
sb.append(current.t + ",");
current = current.next;
}
sb.append("NULL");
return sb.toString();
}
/**
* 打印
*/
public void logMess() {
Log.i("SingleLinkTable==", this.ToSting());
}
}
运行效果
单链表数据: 9,8,7,6,5,4,3,2,1,0,NULL
添加头部元素: 100,9,8,7,6,5,4,3,2,1,0,NULL
添加尾部元素: 100,9,8,7,6,5,4,3,2,1,0,-1,NULL
删除元素: 100,9,8,7,6,5,4,2,1,0,-1,NULL
删除第一个元素: 9,8,7,6,5,4,2,1,0,-1,NULL
是否包含1: true
单链表的缺陷
单链表的查询方式,只能通过顺序遍历一遍才能查询