package linkedlist;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList list1 = new SingleLinkedList();
HeroNode p1 = new HeroNode(1, "宋江", "及时雨");
HeroNode p2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode p3 = new HeroNode(3, "李逵", "黑旋风");
HeroNode p4 = new HeroNode(4, "张顺", "浪里白条");
HeroNode p5 = new HeroNode(5, "吴用", "智多星");
HeroNode p6 = new HeroNode(3, "张飞", "张翼德");
list1.add(p1);
list1.add(p2);
list1.add(p3);
list1.add(p4);
System.out.printf("普通加入链表\n");
list1.show();
list1.reverseList(list1);
System.out.printf("反转链表\n");
list1.show();
list1.reverseList(list1);
System.out.printf("反转链表\n");
list1.show();
System.out.printf("反转输出链表,并不改变原链表\n");
list1.reversePrint(list1);
}
}
//创建HeroNode节点类
class SingleLinkedList {
private 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;
}
//addByOrder()方法,将一个结点按照编号从小到大插入
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp=temp.next;
}
if (flag) {
System.out.printf("要加入的英雄编号:%d,已经存在,无法加入\n", heroNode.no);
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
//update()方法,更新某一个结点
public void update(HeroNode newHeroNode) {
if (head.next == null) {
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
System.out.printf("编号为%d的英雄不存在,无法修改\n",newHeroNode.no);
}
}
//delete()方法,删除标号为no的结点
public void delete(int no) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.printf("编号为%d的英雄不存在,无法删除\n",no);
}
}
//getLength()返回链表的有效长度
public int getLength(SingleLinkedList list) {
int i=0;
if (head.next == null) {
return 0;
}
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
i++;
temp=temp.next;
}
return i;
}
//countDown()方法,返回链表倒数第K个结点
public HeroNode countDown(SingleLinkedList list,int k) {
if (head.next == null) {
System.out.printf("该链表为空,无法查找倒数第K个结点\n");
return null;
}
if (k <= 0 || k > list.getLength(list)) {
System.out.printf("输入的倒数数据超出范围,无法查找\n");
return null;
}
HeroNode temp = head.next;
int length = list.getLength(list)-k;
for (int i = 0; i < length; i++) {
temp = temp.next;
}
return temp;
}
//reverse()方法,输入一个链表,反转链表
public void reverseList(SingleLinkedList list) {
if (head.next == null || head.next.next == null) {
return;
}
HeroNode cur = head.next;
HeroNode next = null;
HeroNode reversehead = new HeroNode(0, "", "");
while (cur != null) {
next = cur.next;
cur.next = reversehead.next;
reversehead.next = cur;
cur = next;
}
head.next = reversehead.next;
}
public void reversePrint(SingleLinkedList list) {
if (head.next == null) {
return;
}
Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode cur = head.next;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
//show()遍历整个链表
public void show() {
if (head.next == null) {
System.out.printf("链表为空\n");
return;
}
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
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;
this.nickname = nickname;
}
public String toString() {
return "HeroNode [no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
}
}
2020.5.24单向链表
最新推荐文章于 2021-07-07 17:34:40 发布