变量:
List中的变量
头结点
思路:
(1)Temp = head:::::
head是头结点
addTail
循环至最后添加!
del(int no)
找到编号没有,找到编号就flag = True!
modefy(HeroNode)
找到编号没有,找到编号就flag = True!
show()
定位到head.getnext()循环
addByOrder
如果说找到了相等的我们就退出,flag = true
其他情况(head+null链表,no < node.no)都是需要进行添加
length 循环
findLastindex() 倒数第几个位置上 i = size - index
reverseSetList() cur next两个变量,revHead一个新的头结点
逆序打印 采用栈(压栈、出栈便可)
公式:
倒数第几个位置:i = size - index
代码:
package linkedList;import java.util.Stack;public class SingleLinkedList {public static void main(String[] args) {HeroNode h1 = new HeroNode(1,"宋江","及时雨");HeroNode h2 = new HeroNode(2, "卢俊义", "玉麒麟");HeroNode h3 = new HeroNode(3, "吴用", "智多星");HeroNode h4 = new HeroNode(4, "豹子头", "林冲");List list = new List();list.addByOrder(h4);list.addByOrder(h2);list.addByOrder(h3);list.addByOrder(h1);list.show();
// list.addByOrder(h2);
// System.out.println(list.length(list.getHead()));
// System.out.print(list.findLastindex(list.getHead(), 2));
// list.reversetList(list.getHead());
// list.show();list.revPrint(list.getHead());}}class List{//头结点HeroNode head = new HeroNode(0, " ", " ");public HeroNode getHead() {return head;}public static int length(HeroNode head) {if(head.getNext() == null) {return 0;}HeroNode cur = head;int length = 0;while(true) {if(cur.getNext() == null) {break;}else {cur = cur.getNext();length++;}}return length;}public static HeroNode findLastindex(HeroNode head, int index) {if(head.getNext() == null) {return null;}int size = length(head);if(index <= 0 || index > size) {return null;}HeroNode temp = head.getNext();for(int i = 0; i < size - index; i++) {temp = temp.getNext();}return temp;}public void add(HeroNode heroNode) {HeroNode temp = head;while(true) {if(temp.getNext() == null) {break;}temp = temp.getNext();}temp.setNext(heroNode);}public void addByOrder(HeroNode heroNode) {HeroNode temp = head;boolean flag = false;while(true) {if(temp.getNext() == null) {break;}if (temp.getNext().getNo() > heroNode.getNo()) {break;}else if(temp.getNext().getNo() == heroNode.getNo()) {flag = true;break;}temp = temp.getNext();}if(flag) {System.out.println("待插入的英雄编号已经存在!");}else {heroNode.setNext(temp.getNext());temp.setNext(heroNode);}}public void show() {if(head.getNext() == null) {System.out.println("链表为空!");return;}HeroNode temp = head.getNext();while(true) {if(temp == null) {break;}System.out.println(temp);temp = temp.getNext();}}public void update(HeroNode heroNode) {if(head.getNext() == null) {System.out.println("链表为空!");return;}boolean flag = false;HeroNode temp = head;while(true) {if(temp.getNext() == null) {break;}else if(temp.getNo() == heroNode.getNo()) {flag = true;break;}temp = temp.getNext();}if(!flag) {System.out.println("没有找到该对象!");}else {temp.setNo(heroNode.getNo());temp.setName(heroNode.getName());}}public void del(int no) {HeroNode temp = head;boolean flag = false;while(true) {if(temp.getNext() == null) {break;}if(temp.getNext().getNo() == no) {flag = true;}temp = temp.getNext();}if(flag) {temp.setNext(temp.getNext().getNext());}else {System.out.println("没有找到该对象!");}}public void reversetList(HeroNode head) {if(head.getNext() == null || head.getNext().getNext() == null) {return;}HeroNode cur = head.getNext();HeroNode next = null;HeroNode revHead = new HeroNode(0,"","");while(cur != null) {next = cur.getNext();cur.setNext(revHead.getNext());revHead.setNext(cur);cur = next;}head.setNext(revHead.getNext());}public void revPrint(HeroNode head) {if(head.getNext() == null) {return;}Stack<HeroNode> stack = new Stack<HeroNode>();HeroNode cur = head.getNext();while(cur != null) {stack.push(cur);cur = cur.getNext();}while(stack.size() > 0) {System.out.println(stack.pop());}}
}
class HeroNode{private int no;private String name;private String nickName;private HeroNode next;public HeroNode(int no, String name, String nickName){this.no = no;this.nickName = nickName;this.name = name;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNickName() {return nickName;}public void setNickName(String nickName) {this.nickName = nickName;}public HeroNode getNext() {return next;}public void setNext(HeroNode next) {this.next = next;}@Overridepublic String toString() {return "Hero[no = " + this.no +" name = " + this.name + " nickName = " + this.nickName +"next=" + this.next + "]";}
}