1、概念和结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向
带头、不带头
循环、非循环
只需要重点掌握两种
无头单向非循环链表:结构简单,一般不会单独用来存数据。
无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。
123、234等为节点地址,本应是16进制,这里简写。
2、链表的实现
无头单向非循环链表的实现
class Node {
public int data;
public Node next;
//构造方法,需要向node中传入数据
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class MyLinkedList {
public Node head;//保存单链表的头结点的引用
//头插法
public void addFirst(int data) {
Node node = new Node(data);
//第一次插入
if(this.head == null) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
//尾插法
public void addLast(int data){
Node node = new Node(data);
Node cur = this.head;
if(this.head == null) {
this.head = node;
return;
}
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
};
private Node searchIndex(int index) {
if (index < 0 || index > this.size()) {
throw new RuntimeException("index位置不合法!");
}
Node cur = this.head;
for (int i = 0; i < index - 1; i++) {
cur = cur.next;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index == 0){
addFirst(data);
return;
}
if(index == this.size()) {
addLast(data);
return;
}
Node node = new Node(data);
Node cur = searchIndex(index);
node.next = cur.next;
cur.next = node;
};
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while(cur != null) {
if(cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
//找要删除的关键字的前驱
private Node searchPrev(int key) {
Node prev = this.head;
while(prev.next != null) {
if(prev.next.data == key){
return prev;
}else{
prev = prev.next;
}
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if (this.head == null) {
return;
}
if(this.head.data == key) {
this.head = this.head.next;
return;
}
Node prev = searchPrev(key);//前驱
if(prev == null) {
System.out.println("根本没有这个节点!");
return;
}
Node del = prev.next;
prev.next = del.next;
}
//删除所有值为key的节点
public void removeAllKey(int key){
Node prev = this.head;
Node cur = this.head.next;
while(cur != null) {
if (cur.data == key) {
prev.next = cur.next;
cur = cur.next;
}else {
prev = cur;
cur = cur.next;
}
}
if(this.head.data == key) {
this.head = this.head.next;
}
}
//得到单链表的长度
public int size(){
Node cur = this.head;
int count = 0;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
//打印单链表
public void display(){
Node cur = this.head;
while(cur != null) {
System.out.print(cur.data + " ")
cur = cur.next;
}
System.out.println();
}
public void clear(){
this.head = null;
}
}
无头双向链表的实现
class Node {
public int data;//数据
public Node next;//后继信息
public Node prev;//前驱信息
public Node (int data) {
this.data = data;
}
}
public class MyLinkedList {
public Node head;//双向链表的头
public Node tail;//尾节点
//头插法
public void addFirst(int data){
Node node = new Node(data);
if(head == null) {
this.head = node;
this.tail = node;
}else{
node.next = this.head;
this.head.prev = node;
this.head = node;
}
};
//尾插法
public void addLast(int data){
Node node = new Node(data);
if(this.head == null) {
this.head = node;
this.tail = node;
}else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
};
private void checkIndex(int index) {
if(index < 0 || index > size()) {
throw new RuntimeException("index不合法");
}
}
private Node searchIndex (int index) {
Node cur = this.head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
checkIndex(index);
if(index == 0) {
addFirst(data);
return ;
}
if(index == size()){
addLast(data);
return;
}
Node cur = searchIndex(index);
Node node = new Node(data);
node.next = cur;
node.prev = cur.prev;
cur.prev.next = node;
cur.prev = node;
};
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while(cur != null) {
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
};
//删除第一次出现关键字为key的节点
public int remove (int key){
Node cur = this.head;
while(cur != null) {
if(cur.data == key){
int oldData = cur.data;
if(cur == this.head) {
this.head = this.head.next;
this.head.prev = null;
}else{
cur.prev.next = cur.next;
if(cur.next != null){
cur.next.prev = cur.prev;
}else{
//删除尾巴节点,只需要移动tail
this.tail = cur.prev;
}
}
return oldData;
}
cur = cur.next;
}
return -1;
};
//删除所有值为key的节点
public void removeAllKey(int key){
Node cur = this.head;
while(cur != null) {
if(cur.data == key){
if(cur == this.head) {
this.head = this.head.next;
if(this.head != null) {
this.head.prev = null;
}
}else{
cur.prev.next = cur.next;
if(cur.next != null){
cur.next.prev = cur.prev;
}else{
//删除尾巴节点,只需要移动tail
this.tail = cur.prev;
}
}
}
cur = cur.next;
}
};
//得到单链表的长度
public int size(){
int count = 0;
Node cur = this.head;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
};
public void display(){
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
};
//需要一个一个节点进行释放
public void clear(){
//this.head = null;
while(this.head != null) {
Node cur = head.next;
head.prev = null;
head.next = null;
head = cur;
}
this.tail = null;
};
}