05链表
介绍:链表是物理[存储单元]上非连续的,非顺序的存储结构,数据元素的逻辑顺序是通过链表的指针地址实现,每个元素包含两个节点,一个是存储元素的数据域(内存空间),另一个是指向下一个节点地址的指针域。
相关操作:
1. 链表的创建
public class Node{
private int data;
private Node next;
public Node(){}
public Node(int data){this.data = data}
public Node(int data,Node next){this.data = data;this.next = next}
}
2. 相关操作
Node n1 = new Node(2);
head.next = n1;
输出结果:head = [1,2];
Node left = head;
Node right = head.next;
while(right != null){
if(right.data == 2){
left.next = right.next;
}
left = right;
right = right.next;
}
输出结果:head = [1];
Node key = new Node(3);
Node left = head;
Node right = head.next;
while(left != null){
if(left.data == 1){
key.next = left.next;
left.next = key;
}
left = right;
right = right.next;
}
输出结果:head = [1,3,2];
int count = 0;
Node cur = head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
输出结果:count = 3;
Node head1 = new Node(4);
head1.next = head;
输出结果:head1 = [4,1,3,2];
Node left = head;
Node right = head.next;
Node front = null,bank = null;
while(right != null){
if(right.data = 3){
front = left;
bank = right.next;
}
left = right;
right = right.next;
}
输出结果:left = 1,right = 2;