public class Linked_list {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(-3, "ww", "w1");
HeroNode heroNode2 = new HeroNode(5, "ww", "w2");
HeroNode heroNode3 = new HeroNode(-99, "ww", "w3");
HeroNode newherNode=new HeroNode(-3,"rr","r1");
Linked linked = new Linked();
linked.add(heroNode1);
linked.add(heroNode2);
linked.add(heroNode3);
linked.Updata(newherNode);
linked.show();
}
}
//链表类
class Linked {
//先创建一个头结点
//头结点没有数据,也不能动,所以需要一个辅助变量
HeroNode head = new HeroNode(0, "", "");
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;//往后移动一位
}
//找到最后一个节点后,在指向新的空结点
temp.next = heroNode;
}
//遍历
public void show() {
if (head.next == null) {
System.out.println("为空");
}
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
//链表的长度(算了头结点,但是没有算最后一个结点)
public int Lenth() {
int count = 0;
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
count++;
temp = temp.next;
}
return count;
}
public int lenth2() {
int count = 0;
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
count++;
temp = temp.next;
}
return count;
}
//删除
public void Delete(int a) {
if (head.next == null) {
System.out.println("为空,没有数据");
} else {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
} else {
if (temp.next.no == a) {//已经找到了要删除的结点
temp.next = temp.next.next;
} else {
System.out.println("没有该节点");
}
}
temp = temp.next;
}
}
}
//排序
public void Short() {
HeroNode temp = head;
for (int i = 0; i < Lenth() - 1; i++) {
for (int j = 0; j < Lenth() - 1 - i; j++) {
if (temp.next.no < temp.next.next.no) {
int a = temp.next.no;
temp.next.no = temp.next.next.no;
temp.next.next.no = a;
}
}
}
}
//查看又没有目标数据
public boolean istrue(int n) {
boolean flag = false;
if (head.next == null) {
System.out.println("为空");
}
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == n) {
flag = true;
System.out.println(temp.next);
}
temp = temp.next;
}
return flag;
}
//如果有重复数据,删除重复数据
public void deleteSet() {
if (head.next == null) {
System.out.println("为空");
}
HeroNode temp = head;
while (temp != null) {
while (temp.next != null) {
}
}
}
//反转链表
public void Revers() {
if (head.next == null || head.next.next == null) {
return;
}
HeroNode cur = head.next;
HeroNode next = null;//当前结点的下一个结点
HeroNode res = new HeroNode(0, "", "");
while (true) {
if (cur == null) {
break;
} else {
next = cur.next;
cur.next = res.next;
res.next = cur;
cur = next;
}
head.next = res.next;
}
}
//利用栈从尾到头遍历
public void stackShow() {
Stack<HeroNode> stack = new Stack<>();
HeroNode temp = head.next;
if (head.next == null) {
System.out.println("为空");
} else {
while (true) {
if (temp == null) {
break;
} else {
stack.push(temp);
}
temp = temp.next;
}
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
//修改链表(根据编号查找)
public void Updata(HeroNode heroNode) {
if (head.next == null) {
System.out.println("链表为空");
} else {
while (true) {
HeroNode temp = head.next;
if (temp == null) {
break;
} else {
if (temp.no == heroNode.no) {
temp.name = head.name;
temp.Nickname = head.Nickname;
}
}
temp = temp.next;
}
}
}
}
class HeroNode {
public int no;
public String name;
public String Nickname;
public HeroNode next;
//构造方法
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
Nickname = nickname;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", Nickname='" + Nickname + '\'' +
'}';
}
}
JAVA单链表
最新推荐文章于 2023-07-15 18:10:01 发布