java实现链表增删查改 排序 查找链表中倒数K个节点 单链表的反转 倒序输出链表

添加链表节点

	public void add(HeroNode heroNode) {
		HeroNode temp=head;
		while(true)
		{
			if(temp.next==null){
				break;
			}
			temp=temp.next;
		}
		temp.next=heroNode;
	}

遍历链表

	public void list() {
		if(head.next==null){
			System.out.println("链表为空");
			return;
		}
		//因为头结点不能动,需要一个辅助变量来遍历
		HeroNode temp=head.next;
		while(true){
			if(temp==null){
				break;
			}
		System.out.println(temp);
		temp=temp.next;
		}
	}

按照序号添加节点

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;
			
		}
	}

修改节点

	public void update(HeroNode heroNode) {
		if(head.next ==null){
			System.out.println("链表为空");
			return;
		}
		//定义一个辅助变量
		HeroNode temp=head.next;
		boolean flag=false;//表示是否找到节点
		while(true){
			if(temp == null){
				break;
			}
			if(temp.no==heroNode.no){
				 flag=true;
				 break;
			}
			temp=temp.next;
		}
		if(flag){
			temp.name=heroNode.name;
			temp.nickname=heroNode.nickname;
		}else{
			System.out.println("没有找到编号");
		}
	}
	

删除节点

public void del(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.println("要删除的节点不存在");
	}	
	}

得到有效节点长度

public static int getLength(HeroNode head){
	if(head.next == null){
		return 0;//空链表
	}
	int length=0;
	HeroNode temp=head.next;
	while(temp != null){
		length++;
		temp=temp.next;
	}
	return length;
}	

单链表的反转

public static void reversePrint(HeroNode head) {
	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());
	}
}
public static HeroNode findLastIndexNode(HeroNode head,int index) {
	if(head.next ==  null){
		return null;
	}
	int size=getLength(head);
	System.out.println("链表大小:"+size);
	System.out.println("遍历到:"+(size-index));
	
	//遍历到size-index个位置就是倒数第K个
	
	if(index <= 0|| index > size ){
		return null;
	}
	
	HeroNode cur=head.next;
	for (int i = 0; i < size-index; i++) {
		cur=cur.next;
	}
	return cur;
}

****

package com.sun.linkedlist;

import java.util.Stack;


public class LinkedList {
	public static void main(String[] args) {
		//测试
		HeroNode hero1=new HeroNode(1,"宋江","及时雨");
		HeroNode hero2=new HeroNode(2,"无用","玉麒麟");
		HeroNode hero3=new HeroNode(3,"林冲","豹子头");
		HeroNode hero4=new HeroNode(4,"卢俊义","狮子头");
		
		SingLinkedList linkedList =new SingLinkedList();
		linkedList.addByOrder(hero1);
		linkedList.addByOrder(hero3);
		linkedList.addByOrder(hero2);
		linkedList.addByOrder(hero4);
		
	
		
		linkedList.list();
		linkedList.reversePrint(SingLinkedList.getHead());
	}


	

	
	
}

class SingLinkedList{
	private static  HeroNode head=new HeroNode(0, "", "");
	
	public static HeroNode getHead() {
		return head;
	}


	public void setHead(HeroNode head) {
		this.head = head;
	}

//添加链表节点
	public void add(HeroNode heroNode) {
		HeroNode temp=head;
		while(true)
		{
			if(temp.next==null){
				break;
			}
			temp=temp.next;
		}
		temp.next=heroNode;
	}
	
//	遍历链表
	public void list() {
		if(head.next==null){
			System.out.println("链表为空");
			return;
		}
		//因为头结点不能动,需要一个辅助变量来遍历
		HeroNode temp=head.next;
		while(true){
			if(temp==null){
				break;
			}
		System.out.println(temp);
		temp=temp.next;
		}
	}
	
	
//按照序号添加节点	
	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;
			
		}
	}
//	修改节点
	public void update(HeroNode heroNode) {
		if(head.next ==null){
			System.out.println("链表为空");
			return;
		}
		//定义一个辅助变量
		HeroNode temp=head.next;
		boolean flag=false;//表示是否找到节点
		while(true){
			if(temp == null){
				break;
			}
			if(temp.no==heroNode.no){
				 flag=true;
				 break;
			}
			temp=temp.next;
		}
		if(flag){
			temp.name=heroNode.name;
			temp.nickname=heroNode.nickname;
		}else{
			System.out.println("没有找到编号");
		}
	}
	
	
//删除节点
	
	public void del(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.println("要删除的节点不存在");
	}	
	}
	
//得到有效节点长度	
public static int getLength(HeroNode head){
	if(head.next == null){
		return 0;//空链表
	}
	int length=0;
	HeroNode temp=head.next;
	while(temp != null){
		length++;
		temp=temp.next;
	}
	return length;
}	


//查找链表中倒数K个节点
public static HeroNode findLastIndexNode(HeroNode head,int index) {
	if(head.next ==  null){
		return null;
	}
	int size=getLength(head);
	System.out.println("链表大小:"+size);
	System.out.println("遍历到:"+(size-index));
	
	//遍历到size-index个位置就是倒数第K个
	
	if(index <= 0|| index > size ){
		return null;
	}
	
	HeroNode cur=head.next;
	for (int i = 0; i < size-index; i++) {
		cur=cur.next;
	}
	return cur;
}


//单链表的反转
public static void fanzhuan(HeroNode heroNode) {
	if(head.next ==  null||head ==  null){
		return ;
	}
	//定义一个辅助变量,帮助我们遍历原来的链表    
	HeroNode cur=head.next;
	HeroNode next=null;//指向当前节点下一个节点
	HeroNode reverseHead=new HeroNode(0,"","");

	//遍历原来的链表,每次遍历一个,将其取出,并放在新链表最前端
	while(cur != null)
	{
		next=cur.next;//先保存住当前节点下一个节点
		cur.next=reverseHead.next;//将cur的下一个节点指向新链表的最前端
		//链表head -> 1 -> 2 -> 3 ,next=2,cur指向1,此时应该将2放在1的前面
		//cur.next=reverseHead.next所以cur.next=2 ,在reverseHead.next
		reverseHead.next=cur;
	//????????????????没有理解
		//next.next=cur;
		cur=next;
		
	}
	head.next=reverseHead.next;
}

//倒序输出链表
public static void reversePrint(HeroNode head) {
	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());
	}
}
}



class HeroNode{
	
	public int no;
	public String name;
	public String nickname;
	public HeroNode next;
	public HeroNode(int no, String name, String nickname) {
		super();
		this.no = no;
		this.name = name;
		this.nickname = nickname;
	}
	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", name=" + name + ", nickname="
				+ nickname + "]";
	}
	
	
	
	
}
发布了58 篇原创文章 · 获赞 20 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40709110/article/details/100675458