1 Star 0 Fork 0

besti1923/JAVA

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
shiyan6.java 6.78 KB
一键复制 编辑 原始数据 按行查看 历史
yifei 提交于 5年前 . shiyan6
package text.java;
import java.io.*;
import java.util.Scanner;
public class shiyan6 {
public static void main(String[] args) throws Exception {
String transition;
Scanner a=new Scanner(System.in); ;
//第一次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 one=new shiyan6_1(transition);
//第二次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 two=new shiyan6_1(transition);
//第三次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 three=new shiyan6_1(transition);
//第四次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 four=new shiyan6_1(transition);
//第五次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 five=new shiyan6_1(transition);
//第六次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 six=new shiyan6_1(transition);
//第七次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 seven=new shiyan6_1(transition);
//第八次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 eight=new shiyan6_1(transition);
//第九次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 nine=new shiyan6_1(transition);
//第十次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 ten=new shiyan6_1(transition);
//第十一次输入
System.out.println("please enter the number");
transition= a.nextLine();
shiyan6_1 eleven=new shiyan6_1(transition);
shiyan6_1 head =one;
one.next =two;
two.next=three;
three.next=four;
four.next=five;
five.next=six;
six.next=seven;
seven.next=eight;
eight.next=nine;
nine.next=ten;
ten.next=eleven;
printlinkedlist(head);
Scanner sc=new Scanner(new File("C:\\Users\\fei\\IdeaProjects\\test\\src\\text\\java\\2.txt"));
System.out.println("读取的第一个数字:");
sc.hasNextLine();
transition=sc.nextLine();
System.out.println(transition);
shiyan6_1 insert_1=new shiyan6_1(transition);
System.out.println("读取的第二个数字:");
sc.hasNextLine();
transition=sc.nextLine();
System.out.println(transition);
shiyan6_1 insert_2=new shiyan6_1(transition);
//读入数字1,插入到链表第 5 位,并打印所有数字,和元素的总数
five.next=insert_1;
insert_1.next=six;
printlinkedlist(head);
//从文件中读入数字2, 插入到链表第 0 位,并打印所有数字,和元素的总数
insert_2.next=head;
head=insert_2;
printlinkedlist(head);
//从链表中删除刚才的数字1. 并打印所有数字和元素的总数。
shiyan6_1 point =head;
shiyan6_1 temp=new shiyan6_1("0") ;
int njinyifei=0;
while(point!= null){
five.next=six;
insert_1.next=null;
System.out.print(point.number+", ");
njinyifei++;
temp=point;
point=point.next;
}
System.out.println("");
System.out.println("元素总数为:"+njinyifei);
System.out.println(bubbleSort1(head, 12));
}
public static void printlinkedlist(shiyan6_1 head) {
shiyan6_1 point =head;
int njinyifei=0;
while(point != null){
System.out.print(point.number+", ");
njinyifei++;
point=point.next;
}
System.out.println("");
System.out.println("元素总数为:"+njinyifei);
}
public static void delate(shiyan6_1 head,shiyan6_1 a) {
shiyan6_1 point =head;
int njinyifei=0;
while(point!= null){
if(point.number==a.number){
point=point.next;
}
System.out.print(point.number+", ");
njinyifei++;
point=point.next;
}
System.out.println("");
System.out.println("元素总数为:"+njinyifei);
}
public static shiyan6_1 bubbleSort1(shiyan6_1 head, int njinyifei) {
for (int i = 0; i < njinyifei - 1; i++) {
shiyan6_1 curNode = head;
for (int j = 0; j < njinyifei - 1 - i; j++) {
if (curNode.number.compareTo(curNode.next.number)>0 ) {
String temp = curNode.number;
curNode.number = curNode.next.number;
curNode.next.number = temp;
}
curNode = curNode.next;
}
System.out.println(head+" 元素个数为:"+njinyifei);
}
return head;
}
/**
* 交换两个节点的指针
*/
/* public static shiyan6_1 bubbleSort2(shiyan6_1 head, int len) {
// 缓存新链表头结点
shiyan6_1 newHead = head;
for (int i = 0; i < len - 1; i++) {
// 前一个元素指针
shiyan6_1 preNode = null;
// 当前处理的元素
shiyan6_1 curNode = newHead;
for (int j = 0; j < len - 1 - i; j++) {
if (curNode.number.compareTo(curNode.next.number) >0 ) {
// 1、交换两个节点的引用,此时curNode的指针交换后会前移,只需要更新preNode指向即可
// 1.1、缓存下下个节点
shiyan6_1 tmpNode = curNode.next.next;
curNode.next.next = curNode;
// 1.2、前驱节点指向nextNode
if (preNode != null) {
preNode.next = curNode.next;
// 1.3、没有前驱节点证明当前节点为头结点,更新头结点
} else {
newHead = curNode.next;
}
// 因为需要把preNode指向原来的下个节点,所以此处赋值preNode,preNode后移
preNode = curNode.next;
// 1.4、curNode指向下下个节点
curNode.next = tmpNode;
// 更新preNode & curNode指针
} else {
preNode = curNode;
curNode = curNode.next;
}
}
}
return newHead;
}
*/
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/besti1923/java.git
git@gitee.com:besti1923/java.git
besti1923
java
JAVA
master

搜索帮助