- Node的初始化
//基于java良好的面向对象的属性,我们可以创建Node类,用来作为指针和数据
public class Node {
public Node next;
public int date;
public Node(Node next) {
this.next = next;
}
public Node(int date) {
this.date=date;
}
public Node(Node next, int date) {
this.next = next;
this.date = date;
}
public Node() {
} -
单链表初始化
public class listNode {
// 长度
public int size;
// 头节点
public static Node head;
public listNode() {
}
- 单链表的添加数据
public void insert(int x,int index) {
if(index<1||index>size){
System.out.println("插入位置不合法。");
return;
}
int i = 1; //记录我们遍历到第几个结点了,也就是记录位置。
Node p= head; //可移动的指针
Node newNode = new Node(x); //创建存储x数据的Node节点
while(p.next != null){//遍历单链表
if(index == i++){ //判断是否到达指定位置。
//注意,我们的p代表的是当前位置的前一个结点。
//前一个结点 当前位置 后一个结点
//temp temp.next temp.next.next
//插入操作。
newNode.next=p.next;
p.next=newNode;
return;
}
p = p.next;
size++;
}
} - 单链表删除数据
public void delete(int index) {
if(index<1||index>size){
System.out.println("插入位置不合法。");
return;
} //步骤跟insert是一样的,只是操作不一样。
int length=1;
Node temp = head;
while(temp.next != null){
if(index == length++){
//删除操作。
/*
* 1->2->3->4->5->6->7->8->null
* 比如我们从里面找到第四个位置(从0位开始)
* p p.next p.next.next
* 1->2->3->4->5(index)-> 6 ->7->8(null)
* 变为:
* 1->2->3->4->6->7->8(null)
*
*
*
*/
temp.next = temp.next.next;
return;
}
temp = temp.next;
}
} - 单链表修改数据
public void modify(int val,int key) {
if (isEmpty()) {
System.out.println("链表为空");
}
else {
if (key<0||key>size) {
System.out.println("key值不合法");
}
}
Node p = head;
if(key>=1) {
for (int i = 0; i < key; i++) {
p=p.next;
}
p.date=val;
}
else {
p.date=val;
}
} - 单链表数据的排序(寻找最大值)
// 链表的排序
public Node sortList() {
//记录每次循环的最小值.冒泡排序
int temp ;
Node curNode = head;
while (curNode != null) {
/**
* 内重循环从当前节点的下一个节点循环到尾节点,
* 找到和外重循环的值比较最小的那个,然后与外重循环进行交换
*/
Node nextNode = curNode.next;
while (nextNode != null) {
//比外重循环的小值放到前面
if (nextNode.date < curNode.date) {
// 类似交换排序
temp = nextNode.date;
nextNode.date = curNode.date;
curNode.date = temp;
}
nextNode = nextNode.next;
}
curNode = curNode.next;
}
return head;
}
-
查找数据(可以先把链表转化成数组,在这里直接查找)
// 查找下标对应的数据
public int find(int key) {
if (isEmpty()) {
System.out.println("链表为空");
}
else {
if (key<1||key>size) {
System.out.println("key值不合法");
}}
Node cur = head;
//为什么是position - 1,因为要使用遍历,让cur指向下一个,
//所以position - 1的下个node就是要找的值
for (int i = 0; i <key; i++) {
cur=cur.next;
}
return cur.date;
}
-
清空,查询长度,找最值,
public void clear() {
size=0;
head.next=null;
}
public int getSize() {
System.out.println(+size);
return size;
}
public int findmax() {
Node p = head;
while (p.next!=null) {
if(p.date>p.next.date) {
int temp = p.date;
p.date=p.next.date;
p.next.date=temp;
}
p=p.next;
}
return p.date;
}
public void display() {
Node p = head;
if (p.next==null) {
return ;
}
System.out.print("[");
while(p!= null)//注意判断条件
{
System.out.print(p.date+" ");
p = p.next;//下一个结点
}
System.out.print("]");
System.out.println();
}打印数据
- 单链表反转的两种算法
- 迭代算法:
-
public void reverse2() {
// 设置一个前驱指针,后继指针
// 前驱:作为空指针
// 后继:循环条件
Node pre = null,nextNode=null;
Node cur = head;
while (cur!=null) {
// 创建后继指针,用来进行循环
nextNode=cur.next;
// 当前指针下一位指向空指针
cur.next=pre;
// 反转结束,将cur结点数据传给pre结点,此时,当前指针应为pre
pre = cur;
// 在pre结点后进行以上的循环
cur=nextNode;
}
//传参
if (pre!=null) {
head=pre;
}
}
2.递归算法:
//反转图解:
// 1->2->3->4->5(null)
// 第一步:遍历这一串链表,直到找到空,或者空的前一位即【h位为5】
// 第二步:h.next为null即h为4,故h.next为5
// 初始化: h h.next
// 1->2->3->4->5
// 第一轮: h.next指向h(h.next.next=h);
// 1->2->3->4 <-5
// 断开【4->5】的环 h.next=null
// 1->2->3->4(指向null)<-5
// 第二轮:
// 1->2->3<-4(null) 出5
// 第三轮:
// 1->2<-3(null) 出4
//
public Node reverse3(Node h) {
if (h==null || h.next==null)
return h;
Node p = reverse3(h.next);
h.next.next=h;
h.next=null;
if (p!=null) {
head=p;
}
return p;
}