Java实现链表的增删排序

package 二叉查找树;



public class linkedlist {


	
	class Node{
		
		Node next=null;
		int data;
		public Node(int data){
			
			this.data=data;
		}
	}
	public class mylinklist{
		Node head=null;
		
		public void addnode(int i) {
			Node newNode=new Node(i);
			if (head==null){
				head=newNode;
				return;
			}
			Node tmp=head;
			while(tmp.next!=null)
			{
				tmp=tmp.next;
			}
			tmp.next=newNode;
						
		}
		public Boolean deleteNode(int index){
			if(index<0||index>length()){
				return false;
			}
			if(index==1){
				head=head.next;
				return true;
			}
			int i=2;
			Node tmp=head;
			while(i!=index){
				tmp=tmp.next;
				i++;
				
			}
			tmp.next=(tmp.next).next;			
			
			return true;
			
		}
		public int length(){
			int length=0;
			Node tmp=head;
			while(tmp!=null){
				length++;
				tmp=tmp.next;
			}
			return length;
		}
		
		public Node orderlist(){
			Node nextNode=null;
			Node curNode=head;
			int tmp=0;
			while(curNode.next!=null){
				nextNode=curNode.next;
				while(nextNode!=null){
					tmp=curNode.data;
					if(curNode.data>nextNode.data){
						curNode.data=nextNode.data;
						nextNode.data=tmp;
					}
					nextNode=nextNode.next;
				}
				curNode=curNode.next;
			}
			return head;
		}
			
		
		public void printlist(){
			
			Node tmp=head;
			while(tmp!=null){
				
				System.out.println(tmp.data);
				tmp=tmp.next;
			}
		}
		
		
	}
    public static void main(String[] args){


    	linkedlist test1=new linkedlist();
    	mylinklist test=test1.new mylinklist();
    	test.addnode(5);
    	test.addnode(4);
    	test.addnode(3);
    	test.addnode(2);
    	test.addnode(1);
    	System.out.println(test.orderlist().data);
    	System.out.println("listLen:"+test.length());
    	test.deleteNode(3);
    	System.out.println(test.length());
    	test.printlist();
    	
    	
    	
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值