链表
链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。
链表的结构有8种。
其中分为单向/双向、带头/不带头、循环/非循环八种。
无头单向链表的增删改查
创建了一个类,用来对链表的增删改查。
//无头单向链表插入
class Node{//创建节点
public int value;//节点中的数据
public Node next;//节点下一个数据的地址
public Node(int value){
this.value = value;
}
}
public class SingleLinkedList {
public Node head = null;
//头插
public void addFirst(int data){
Node node = new Node(data);
node.next = head;
this.head = node;
}
//尾插
public void addLast(int data){
Node cur = this.head;
Node node = new Node(data);
if(this.head == null){
this.head = node;
}else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public Node searchIndexSbuOne(int index) {
Node cur = this.head;
int count = 0;
while (count != index - 1) {
count++;
cur = cur.next;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标,空链表添加数据
public void addIndex(int index,int data){
Node cur = this.head;
Node node = new Node(data);
if(index < 0 || index > this.size()){
System.out.println("数据插入的位置不合法!");
return;
}
if(index == 0){
addFirst(data);
return;
}
if(index == size()){
addLast(data);
return;
}
Node ret = searchIndexSbuOne(index);
node.next = ret.next;
ret.next = node;
}
//查找是否包含关键字key是否在单链表中
public boolean contains(int key){
Node cur = this.head;
Node node = new Node(key);
while(cur != null){
if(cur.value == key){
return true;
}
cur = cur.next;
}
return false;
}
public Node searchPrev(int key){
Node cur = this.head;
while(cur.next != null){
if(cur.next.value == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head.value == 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 cur = this.head.next;
Node prev = this.head;
while(cur != null){
if(cur.value == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur =cur.next;
}
}
if(prev.value == key){
prev = prev.next;
}
}
//得到单链表的长度
public int size(){
int length = 0;
Node cur = this.head;
while(cur != null){
length++;
cur = cur.next;
}
return length;
}
//打印链表数据
public void display(){
Node cur = this.head;
if(cur == null){
System.out.println();
}
while(cur != null){
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println();
}
//清空链表
public void clear(){
Node cur = this.head;
while(cur != null){
Node curNext = cur.next;
cur.next = null;
cur = curNext;
}
this.head = null;
System.out.println("链表被清空");
}
}
顺序表和链表的区别和联系
顺序表
优点:空间连续、支持随机访问
缺点:(1)中间或前面部分的插入删除时间复杂度O(N);(2)增容的代价比较大。
链表
优点:(1)任意位置插入删除时间复杂度为O(1);(2)不会浪费空间,用一个开辟一个空间。
缺点:以节点为存储单位,不支持随机访问。