直接上代码,手撕单链表
package List;
import java.util.Stack;
import static List.SingleLinkedList.reverse;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "吴用", "智多星");
HeroNode heroNode3 = new HeroNode(3, "林冲", "豹子头");
HeroNode heroNode4 = new HeroNode(4, "李逵", "黑旋风");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList.addByOrder(heroNode2);
singleLinkedList.addByOrder(heroNode4);
singleLinkedList.list();
System.out.println("===============");
singleLinkedList.del(1);
singleLinkedList.list();
System.out.println(singleLinkedList.getNode(singleLinkedList.getHead()));
HeroNode test=singleLinkedList.sle(singleLinkedList.getHead(),2);
System.out.println(test);
System.out.println("反转前");
singleLinkedList.list();
System.out.println("反转后");
reverse(singleLinkedList.getHead());
singleLinkedList.list();
System.out.println("逆序打印单链表:");
singleLinkedList.reversePrint(singleLinkedList.getHead());
}
}
class SingleLinkedList{
private HeroNode head=new HeroNode(0,"","" );
public HeroNode getHead() {
return head;
}
public void add(HeroNode heroNode){
HeroNode temp=head;
while(true){
if (temp.next==null){
break;
}
temp=temp.next;
}
temp.next=heroNode;
}
public void addByOrder(HeroNode heroNode){
HeroNode temp=head;
while(true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
break;
}
temp=temp.next;
}
heroNode.next=temp.next;
temp.next=heroNode;
}
public void del(int no){
HeroNode temp= head;
while(true){
if (temp.next==null){
break;
}
if (temp.next.no==no){
temp.next=temp.next.next;
}
else{
break;
}
}
}
public void list(){
if (head.next==null){
System.out.println("链表为空");
}
HeroNode temp=head.next;
while (temp!=null){
System.out.println(temp);
temp=temp.next;
}
}
public static int getNode(HeroNode head){
while (true) {
if (head.next == null) {
return 0;
}
int length = 0;
HeroNode cur=head.next;
while (cur!=null){
length++;
cur=cur.next;
}
return length;
}
}
public static HeroNode sle(HeroNode head,int index ){
if (head.next==null){
return null;
}
int size=getNode(head);
if (index<0||index>size){
return null;
}
HeroNode cur=head.next;
for (int i=0;i<size-index;i++){
cur=cur.next;
System.out.println();
}
return cur;
}
public static void reverse(HeroNode head){
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 static void reversePrint(HeroNode head){
HeroNode cur=head.next;
Stack<HeroNode> stack = new Stack<>();
while(cur!=null){
stack.add(cur);
cur=cur.next;
}
while (stack!=null){
System.out.println(stack.pop());
}
}
}
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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}