1.java(jvm)是如何构造链表的
链表可以形象的表示为
上图中的每一个节点可以用java中的一个类来表示,这个类包含两个成员变量,为别是数据域和指针域,数据域负责存储数据,指针域负责存储下一个节点的地址,为了方便,可以将成员变量设置为public
public class LinkedNode {
public int data;
public LinkedNode next;
public LinkedNode(int data) {
this.data = data;
}
}
jvm包含栈区和堆区,栈区存储对象的引用(地址),堆区存储真正的对象,如下图
2.单链表增加节点(分为头部、中间、尾部)
public static int getLength(LinkedNode head) {
int count = 0;
LinkedNode node = head;
while(node != null){
count++;
node = node.next;
}
return count;
}
public static LinkedNode insert(LinkedNode head, LinkedNode node,int insertPosition){
if(head==null){
head = node;
return head;
}
if (insertPosition<1 || insertPosition>getLength(head)+1){
System.out.println("插入位置越界");
return head;
}
if (insertPosition==1){
node.next = head;
head = node;
return head;
}
int iter = 1;
LinkedNode iterNode = head;
while (iter < insertPosition - 1){
iterNode = iterNode.next;
iter++;
}
node.next = iterNode.next;
iterNode.next = node;
return head;
}
3.单链表删除节点(分为头部、中间、尾部)
public LinkedNode delete(LinkedNode head, int deletePosition){
if (head == null){
return head;
}
if (deletePosition<1||deletePosition>getLength(head)){
System.out.println("删除位置越界");
return head;
}
if (deletePosition == 1){
head = head.next;
return head;
}
int iter = 1;
LinkedNode iterNode = head;
while (iter < deletePosition-1){
iter++;
iterNode = iterNode.next;
}
iterNode.next = iterNode.next.next;
return head;
}
4.双向链表的构造、插入和删除节点
构造本身也可以看做插入,直接看插入代码,选择头部插入或者尾部插入构造双向链表
双向链表节点
public class DoubleNode {
public int data;
public DoubleNode pre;
public DoubleNode next;
public DoubleNode(int data) {
this.data = data;
}
}
双向链表
public class DoubleLinkedList {
public DoubleNode first;
public DoubleNode last;
public DoubleLinkedList() {
this.first = null;
last = first;
}
}
双向链表是否为空
public static boolean isEmpty(DoubleLinkedList doubleLinkedList) {
return doubleLinkedList.first == null;
}
头部插入
public static DoubleLinkedList insertFirst(DoubleLinkedList doubleLinkedList, DoubleNode node) {
if (isEmpty(doubleLinkedList)) {
doubleLinkedList.first = node;
doubleLinkedList.last = node;
return doubleLinkedList;
}
doubleLinkedList.first.pre = node;
node.next = doubleLinkedList.first;
doubleLinkedList.first = node;
return doubleLinkedList;
}
在尾部插入
public static DoubleLinkedList insertLast(DoubleLinkedList doubleLinkedList, DoubleNode node) {
if (isEmpty(doubleLinkedList)) {
doubleLinkedList.first = node;
doubleLinkedList.last = node;
return doubleLinkedList;
}
doubleLinkedList.last.next = node;
node.pre = doubleLinkedList.last;
doubleLinkedList.last = node;
return doubleLinkedList;
}
在中间插入
public DoubleLinkedList insertAfterKey(DoubleLinkedList doubleLinkedList, int key, DoubleNode node) {
if (isEmpty(doubleLinkedList)) {
doubleLinkedList.first = node;
doubleLinkedList.last = node;
return doubleLinkedList;
}
DoubleNode iterNode = doubleLinkedList.first;
while (iterNode != null && iterNode.data != key) {
iterNode = iterNode.next;
}
if (iterNode == null || iterNode == doubleLinkedList.last) {
doubleLinkedList.last.next = node;
node.pre = doubleLinkedList.last;
doubleLinkedList.last = node;
}else{
iterNode.next.pre = node;
node.next = iterNode.next;
iterNode.next = node;
node.pre = iterNode;
}
return doubleLinkedList;
}
头部删除
public DoubleLinkedList deleteFirst(DoubleLinkedList doubleLinkedList){
if(isEmpty(doubleLinkedList)){
return doubleLinkedList;
}
if(doubleLinkedList.first.next == null){
doubleLinkedList.first = null;
doubleLinkedList.last = null;
return doubleLinkedList;
}
doubleLinkedList.first.next.pre = null;
doubleLinkedList.first = doubleLinkedList.first.next;
return doubleLinkedList;
}
从尾部删除
public DoubleLinkedList deleteLast(DoubleLinkedList doubleLinkedList){
if(isEmpty(doubleLinkedList)){
return doubleLinkedList;
}
if(doubleLinkedList.first.next == null){
doubleLinkedList.first = null;
doubleLinkedList.last = null;
return doubleLinkedList;
}
doubleLinkedList.last.pre.next = null;
doubleLinkedList.last = doubleLinkedList.last.pre;
return doubleLinkedList;
}
从中间删除
public DoubleLinkedList deleteByKey(DoubleLinkedList doubleLinkedList, int key) {
if (doubleLinkedList == null) {
return null;
}
DoubleNode iterNode = doubleLinkedList.first;
while (iterNode != null && iterNode.data != key) {
iterNode = iterNode.next;
}
if (iterNode == null){
return doubleLinkedList;
}
if (iterNode == doubleLinkedList.first){
doubleLinkedList.first.next.pre = null;
doubleLinkedList.first = doubleLinkedList.first.next;
return doubleLinkedList;
}
if(iterNode == doubleLinkedList.last){
doubleLinkedList.last.pre.next = null;
doubleLinkedList.last = doubleLinkedList.last.pre;
return doubleLinkedList;
}
iterNode.pre.next = iterNode.next;
iterNode.next.pre = iterNode.pre;
return doubleLinkedList;
}