单链表反转
思路:
1.先定义一个空的节点reverseHead=new Herolist();
2.从头到尾遍历原来的链表,没遍历一个节点,就将其取出,并放在新的链表reserveHead的最前面,每次来一个节点都换一下reserveHead.next
3.原来的链表的head.next = reserveHead.next
从尾到头打印单链表
思路:
1.要求逆序打印单链表
-
先将单链表进行反转操作,再遍历即可,但是这样会破坏原来的单链表的结构,不建议
-
可以先利用栈这个数据结构,将各个节点压入到栈中,然后再利用先进后出的特点,实现从尾打印(运用栈的技术Stack())
里面还要其他一些链表的功能:如增删改查~~~
package com.number_solution;
import java.util.Scanner;
import java.util.Stack;
public class SingleLinkedListDemo1 {
public static void main(String[] args) {
HeroList hero1 = new HeroList(1,"松江","及时雨");
HeroList hero2 = new HeroList(2,"灵宠","豹子头");
HeroList hero3 = new HeroList(4,"李逵","黑旋风");
HeroList hero4 = new HeroList(5,"吴用","智多星");
HeroList hero5 = new HeroList(3,"aaa", "AAA");
//添加数据
SingleLinkedList si = new SingleLinkedList();
si.add_list(hero1);
si.add_list(hero2);
si.add_list(hero3);
si.add_list(hero4);
si.add_list1(hero5);
//打印
si.show_list();
//定义变量
int a = 0;
String name = null;
String nicename = null;
System.out.println("请输入需要修改的节点信息");
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要修改的节点编号");
a = sc.nextInt();
System.out.println("请输入需要修改的节点名字");
name = sc.next();
System.out.println("请输入需要修改的节点昵称");
nicename = sc.next();
HeroList hero = new HeroList(a,name,nicename);
si.change_list(hero);
si.show_list();
//删除节点
si.delete_list(5);
si.show_list();
System.out.println("-----------------------");
//转换列表
si.reserve_list();
si.show_list();
//反向打印链表
System.out.println("反向打印链表");
si.reserve_show();
}
}
//创建链表
class SingleLinkedList {
//创建头节点
HeroList heo = new HeroList(0,"","");
//添加链表数据
public void add_list(HeroList list){
HeroList temp = heo;
while (true){
//找到链表最后
if(temp.next == null){
break;
}
//如果没有找到最后,将temp向后移
temp = temp.next;
}
//当退出循环时,temp就指向链表最后
temp.next = list;
}
//按照编号顺序添加
public void add_list1(HeroList list){
HeroList temp = heo;
boolean flag = false;//判断新编号是否重复
while (true){
//遍历结束
if(temp.next == null){
break;
}
//找到位置
if(temp.next.no > list.no){
break;
}
if(temp.next.no == list.no){
flag = true;
}
//循环成立基础
temp = temp.next;
}
if (flag == true){
System.out.println("您输入的编号%d已经存在" + list.no);
return;
}
//插入数据
list.next = temp.next;
temp.next = list;
}
//修改节点的信息
public void change_list(HeroList list){
ifnull();
HeroList temp = heo.next;
while (true){
if (temp == null){
System.out.println("没有找到该链表");
break;
}
if(temp.no == list.no){
temp.name = list.name;
temp.nicename = list.nicename;
break;
}
temp = temp.next;
}
}
//删除链表
public void delete_list(int no) {
//判断链表是否为空
ifnull();
HeroList temp = heo;
//循环遍历出链表
while (true) {
if (temp.next == null) {
System.out.println("没有找到该链表");
break;
}
if (temp.next.no == no) {
temp.next = temp.next.next;
System.out.println("已删除~~~");
break;
}
temp = temp.next;
}
}
//打印链表
public void show_list() {
//判断链表是否为空
ifnull();
HeroList temp = heo.next;
//结束遍历操作
while (true) {
if (temp == null){
break;
}
//输出节点信息
System.out.println(temp);
temp = temp.next;
}
}
//反转链表
public void reserve_list() {
HeroList temp = heo.next;
HeroList pre = null; //头
while (temp != null){
HeroList next = temp.next;
temp.next = pre;
pre = temp; //向前移
temp = next;
}
heo.next = pre;
}
//反向打印链表
public void reserve_show() {
HeroList temp = heo.next;
//创建链表
Stack<HeroList> stack = new Stack<>();
//入栈
while (temp != null){
stack.add(temp);
temp = temp.next;
}
//出栈
while (stack.size() != 0){
System.out.println(stack.pop());
}
}
public void ifnull(){
//判断链表是否为空
if (heo.next == null) {
System.out.println("链表为空~~");
}
}
}
//构造节点
class HeroList {
public int no;
public String name;
public String nicename;
public HeroList next; //指向下一个节点
//构造器
public HeroList(int no, String name, String nicename) {
this.no = no;
this.name = name;
this.nicename = nicename;
}
//重写string方法
@Override
public String toString() {
return "HeroList [no=" + no + ", name=" + name + ", nicename=" + nicename + "]";
}
}