- 数据域data:存储数据元素信息的域称为数据域;
- 指针域:存储直接后继位置的域称为指针域;指针域中存储的信息称为指针或链。
- 数据域和指针域组成数据元素ai的存储印象,称为节点(Node).
- 每个节点中包含一个指针域,称为单链表。
- 链表中第一个节点的存储的位置称为头指针。
- 链表的最后一个节点指针为“空”。
- 单链表的第一个节点前附设一个节点,称为头结点。
- 头结点和头指针的异同
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/db598555abcdd7641a0781f3c51d4f2b.png)
- 头插入:
class TestLink {
class Entry {
int data;
Entry next;
public Entry() {
this.data = -1;
this.next = null;
}
public Entry(int val) {
this.data = val;
this.next = null;
}
}
private Entry head;
public TestLink() {
this.head = new Entry();
}
public void insertHead(int val) {
Entry cur = new Entry();
cur.next = this.head.next;
this.head.next = cur;
}
public void insertTail(int val) {
Entry cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
Entry entry = new Entry(val);
cur.next = entry;
}
public int getLength() {
int count = 0;
Entry cur = this.head.next;
while(cur != null) {
count ++;
cur = cur.next;
}
return count;
}
public void insertPos(int val, int pos) {
if (pos < 0 || pos > getLength()) {
return;
}
Entry cur = this.head;
for (int i = 0; i < pos - 1;i ++) {
cur = cur.next;
}
Entry entry = new Entry(val);
entry.next = cur.next;
cur.next = entry;
}
public void show() {
Entry cur = this.head.next;
while(cur != null) {
System.out.println(cur.data + " ");
}
System.out.println();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7a05164175396b2ae4da5d980a39f039.png)