Demo1
1.创建链表
2.增加结点
3.遍历链表
package test5;
public class Node {
//数据域
public int data;
//指针域,指向下一个节点
public Node next;
//头指针
public static Node head;
public Node() {
}
public Node(int data) {
this.data = data;
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
/**
* 向链表添加数据
*
* @param value 要添加的数据
*/
public static void addData(int value) {
//初始化要加入的节点
Node newNode = new Node(value);
Node p=head;
// 找到尾节点
while (p.next != null) {
p = p.next;
}
// 已经包括了头节点.next为null的情况了~
p.next = newNode;
System.out.println(head.next.data);
}
/**
* 遍历链表
*
* @param head 头节点
*/
public static void list(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("当前值:" + temp.next.data);
//继续下一个
temp = temp.next;
}
}
public static void main(String[] args) {
//只初始化一次头结点
head = new Node();
//加入结点
addData(1);
addData(2);
addData(3);
//遍历结点
list(head);
}
}
Demo2
package test5;
public class Node {
//数据域
public int data;
//指针域,指向下一个节点
public Node next;
//头指针
public static Node head;
public Node() {
}
public Node(int data) {
this.data = data;
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
/**
* 向链表添加数据
*
* @param value 要添加的数据
*/
public static void addData(int value) {
//初始化要加入的节点
Node newNode = new Node(value);
Node p=head;
// 找到尾节点
while (p.next != null) {
p = p.next;
}
// 已经包括了头节点.next为null的情况了~
p.next = newNode;
System.out.println(head.next.data);
}
/**
* 遍历链表
*
* @param head 头节点
*/
public static void list(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("当前值:" + temp.next.data);
//继续下一个
temp = temp.next;
}
}
/**
* 获取链表的长度
* @param head 头指针
*/
public static int linkListLength(Node head) {
int length = 0;
//临时节点,从首节点开始
Node temp = head.next;
// 找到尾节点
while (temp != null) {
length++;
//继续下一个
temp = temp.next;
}
return length;
}
/**
* 插入节点
*
* @param head 头指针
* @param index 要插入的位置
* @param value 要插入的值
*/
public static void insertNode(Node head, int index, int value) {
//首先需要判断指定位置是否合法(有没有越界!不能插头指针前)
if (index < 1 || index > linkListLength(head) + 1) {
System.out.println("插入位置不合法。");
return;
}
//临时节点,从头节点开始
Node p = head;
//记录遍历的当前位置
int currentPos = 0;
//初始化要插入的节点
Node insertNode = new Node(value);
//是否有下一个
while (p.next != null) {
//找到上一个节点的位置了
if ((index - 1) == currentPos) {
//p表示的是上一个节点
//将原本由上一个节点的指向交由插入的节点来指向
insertNode.next = p.next;
//将上一个节点的指针域指向要插入的节点
p.next = insertNode;
return;
}
currentPos++;
//继续下一个
p = p.next;
}
}
public static void main(String[] args) {
//只初始化一次头结点
head = new Node();
//加入结点
addData(1);
addData(2);
addData(3);
//遍历结点
list(head);
//获取链表长度
System.out.println("当前链表长度:"+linkListLength(head));
//插入节点
insertNode(head, 1, 4);
//遍历结点
list(head);
//获取链表长度
System.out.println("插入结点后链表长度:"+linkListLength(head));
}
}
Demo3
package test5;
public class Node {
//数据域
public int data;
//指针域,指向下一个节点
public Node next;
//头指针
public static Node head;
public Node() {
}
public Node(int data) {
this.data = data;
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
/**
* 向链表添加数据
*
* @param value 要添加的数据
*/
public static void addData(int value) {
//初始化要加入的节点
Node newNode = new Node(value);
Node p=head;
// 找到尾节点
while (p.next != null) {
p = p.next;
}
// 已经包括了头节点.next为null的情况了~
p.next = newNode;
System.out.println(head.next.data);
}
/**
* 遍历链表
*
* @param head 头节点
*/
public static void list(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("当前值:" + temp.next.data);
//继续下一个
temp = temp.next;
}
}
/**
* 获取链表的长度
* @param head 头指针
*/
public static int linkListLength(Node head) {
int length = 0;
//临时节点,从首节点开始
Node temp = head.next;
// 找到尾节点
while (temp != null) {
length++;
//继续下一个
temp = temp.next;
}
return length;
}
/**
* 插入节点
*
* @param head 头指针
* @param index 要插入的位置
* @param value 要插入的值
*/
public static void insertNode(Node head, int index, int value) {
//首先需要判断指定位置是否合法(有没有越界!不能插头指针前)
if (index < 1 || index > linkListLength(head) + 1) {
System.out.println("插入位置不合法。");
return;
}
//临时节点,从头节点开始
Node p = head;
//记录遍历的当前位置
int currentPos = 0;
//初始化要插入的节点
Node insertNode = new Node(value);
//是否有下一个
while (p.next != null) {
//找到上一个节点的位置了
if ((index - 1) == currentPos) {
//p表示的是上一个节点
//将原本由上一个节点的指向交由插入的节点来指向
insertNode.next = p.next;
//将上一个节点的指针域指向要插入的节点
p.next = insertNode;
return;
}
currentPos++;
//继续下一个
p = p.next;
}
}
/**
* 根据位置删除节点
*
* @param head 头指针
* @param index 要删除的位置
*/
public static void deleteNode(Node head, int index) {
//首先需要判断指定位置是否合法,
if (index < 1 || index > linkListLength(head) + 1) {
System.out.println("删除位置不合法。");
return;
}
//临时节点,从头节点开始
Node p = head;
//记录遍历的当前位置
int currentPos = 0;
while (p.next != null) {
//找到上一个节点的位置了
if ((index - 1) == currentPos) {
//p表示的是上一个节点,p.next表示的是想要删除的节点
//将想要删除的节点存储一下
Node deleteNode = p.next;
//想要删除节点的下一个节点交由上一个节点来控制
p.next = deleteNode.next;
return;
}
currentPos++;
//继续下一个
p = p.next;
}
}
/**
* 对链表进行排序
*
* @param head
*
*/
public static void sortLinkList(Node head) {
Node currentNode;
Node nextNode;
for (currentNode = head.next; currentNode.next != null; currentNode = currentNode.next) {
for (nextNode = head.next; nextNode.next != null; nextNode = nextNode.next) {
if (nextNode.data > nextNode.next.data) {
int temp = nextNode.data;
nextNode.data = nextNode.next.data;
nextNode.next.data = temp;
}
}
}
}
public static void main(String[] args) {
//只初始化一次头结点
head = new Node();
//加入结点
addData(1);
addData(2);
addData(3);
//遍历结点
list(head);
//获取链表长度
System.out.println("当前链表长度:"+linkListLength(head));
//插入节点
insertNode(head, 1, 4);
//遍历结点
list(head);
//获取链表长度
System.out.println("插入结点后链表长度:"+linkListLength(head));
deleteNode(head, 3);
//遍历结点
System.out.println("删除值为2的结点后的链表:");
list(head);
sortLinkList(head);
//遍历结点
System.out.println("冒泡排序后的链表:");
list(head);
}
}
Demo4
当前链表中倒数第2个元素的值
当前链表中的中间节点的值
删除重复元素后输出单链表
通过递归从尾到头输出单链表
链表反转后输出
package test5;
public class Node {
//数据域
public int data;
//指针域,指向下一个节点
public Node next;
//头指针
public static Node head;
public Node() {
}
public Node(int data) {
this.data = data;
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
/**
* 向链表添加数据
*
* @param value 要添加的数据
*/
public static void addData(int value) {
//初始化要加入的节点
Node newNode = new Node(value);
Node p=head;
// 找到尾节点
while (p.next != null) {
p = p.next;
}
// 已经包括了头节点.next为null的情况了~
p.next = newNode;
}
/**
* 遍历链表
*
* @param head 头节点
*/
public static void list(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("当前值:" + temp.next.data);
//继续下一个
temp = temp.next;
}
}
/**
* 获取链表的长度
* @param head 头指针
*/
public static int linkListLength(Node head) {
int length = 0;
//临时节点,从首节点开始
Node temp = head.next;
// 找到尾节点
while (temp != null) {
length++;
//继续下一个
temp = temp.next;
}
return length;
}
/**
* 插入节点
*
* @param head 头指针
* @param index 要插入的位置
* @param value 要插入的值
*/
public static void insertNode(Node head, int index, int value) {
//首先需要判断指定位置是否合法(有没有越界!不能插头指针前)
if (index < 1 || index > linkListLength(head) + 1) {
System.out.println("插入位置不合法。");
return;
}
//临时节点,从头节点开始
Node p = head;
//记录遍历的当前位置
int currentPos = 0;
//初始化要插入的节点
Node insertNode = new Node(value);
//是否有下一个
while (p.next != null) {
//找到上一个节点的位置了
if ((index - 1) == currentPos) {
//p表示的是上一个节点
//将原本由上一个节点的指向交由插入的节点来指向
insertNode.next = p.next;
//将上一个节点的指针域指向要插入的节点
p.next = insertNode;
return;
}
currentPos++;
//继续下一个
p = p.next;
}
}
/**
* 根据位置删除节点
*
* @param head 头指针
* @param index 要删除的位置
*/
public static void deleteNode(Node head, int index) {
//首先需要判断指定位置是否合法,
if (index < 1 || index > linkListLength(head) + 1) {
System.out.println("删除位置不合法。");
return;
}
//临时节点,从头节点开始
Node p = head;
//记录遍历的当前位置
int currentPos = 0;
while (p.next != null) {
//找到上一个节点的位置了
if ((index - 1) == currentPos) {
//p表示的是上一个节点,p.next表示的是想要删除的节点
//将想要删除的节点存储一下
Node deleteNode = p.next;
//想要删除节点的下一个节点交由上一个节点来控制
p.next = deleteNode.next;
return;
}
currentPos++;
//继续下一个
p = p.next;
}
}
/**
* 对链表进行排序
*
* @param head
*
*/
public static void sortLinkList(Node head) {
Node currentNode;
Node nextNode;
for (currentNode = head.next; currentNode.next != null; currentNode = currentNode.next) {
for (nextNode = head.next; nextNode.next != null; nextNode = nextNode.next) {
if (nextNode.data > nextNode.next.data) {
int temp = nextNode.data;
nextNode.data = nextNode.next.data;
nextNode.next.data = temp;
}
}
}
}
/**
* 找到链表中倒数第k个节点(设置两个指针p1、p2,让p2比p1快k个节点,同时向后遍历,当p2为空,则p1为倒数第k个节点
*
* @param head
* @param k 倒数第k个节点
*/
public static Node findKNode(Node head, int k) {
//由头指针确定一个链表,得到该链表的长度,倒数第一个就是最后一个
if (k < 1 || k > linkListLength(head))
return null;
Node p1 = head;
Node p2 = head;
// p2比怕p1快k个节点
for (int i = 0; i < k - 1; i++)
p2 = p2.next;
// 只要p2为null,那么p1就是倒数第k个节点了
while (p2.next != null) {
p2 = p2.next;
p1 = p1.next;
}
return p1;
}
/**
* 查询单链表的中间节点(头结点也算)
*/
public static Node searchMid(Node head) {
Node p1 = head;
Node p2 = head;
// 一个走一步,一个走两步,直到为null,走一步的到达的就是中间节点
while (p2 != null && p2.next != null && p2.next.next != null) {
p1 = p1.next;
p2 = p2.next.next;
}
return p1;
}
//删除重复节点。:外循环当前遍历的结点为p,内循环从表头开始遍历至p
public static void delete_v3(Node head){
Node p=head;
while(p!=null){
Node q=head;
while(q.next!=p && q.next!=null){
if(q.next.data==p.data){
q.next=q.next.next;
}else
q=q.next;
}
p=p.next;
}
}
/**
* 通过递归从尾到头输出单链表
*
* @param node 首元节点
*/
public static void printListReversely(Node node) {
if (node != null) {
printListReversely(node.next);
System.out.println(node.data);
}
}
/**
* 实现链表的反转
*
* @param node 链表的首元节点
*/
public static Node reverseLinkList(Node node) { //传入的是首元结点非头结点
Node pre = null;
Node next = null;
while (node != null) {
next = node.next;
node.next = pre;//交换指针位置
pre = node;
node = next; //作用:指向原链表中的下一个结点
}
return pre;
}
public static void main(String[] args) {
//只初始化一次头结点
head = new Node();
//加入结点
addData(1);
addData(2);
addData(3);
//遍历结点
list(head);
//获取链表长度
System.out.println("当前链表长度:"+linkListLength(head));
//插入节点
insertNode(head, 1, 4);
//遍历结点
list(head);
//获取链表长度
System.out.println("插入结点后链表长度:"+linkListLength(head));
deleteNode(head, 3);
//遍历结点
System.out.println("删除值为2的结点后的链表:");
list(head);
sortLinkList(head);
//遍历结点
System.out.println("冒泡排序后的链表:");
list(head);
addData(1);
addData(2);
addData(1);
//遍历结点
System.out.println("加入新元素后的链表:");
list(head);
System.out.println("当前链表中倒数第2个元素的值是:"+findKNode(head, 2).data);
System.out.println("当前链表中的中间节点的值是:"+searchMid(head).data);
Node.delete_v3(head);
System.out.println("删除重复元素后输出单链表:");
list(head);
System.out.println("通过递归从尾到头输出单链表:");
printListReversely(head.next);
System.out.println("链表反转后输出:");
Node node1=reverseLinkList(head.next);
while (node1!=null)
{
System.out.println(node1.data);
node1=node1.next;
}
}
}
喜欢的朋友可以关注我的个人公众号,后台回复java资料可免费领取资源。