反序链表java 表示

节点的java表示:

package suanfa;

public class Node {
	
	private Object value;
	private Node next;
	public Object getValue() {
		return value;
	}
	public Node getNext() {
		return next;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	

}

 具体实现:

package suanfa;

import java.util.TreeMap;

public class Nodelist {
	
	public void setNodelist(Node head,int n,char obj){
		head.setValue("Head");
		Node node=new Node();
		head.setNext(node);
		node.setValue(obj);
		if(n>0){
			for(int i=1;i<n;i++){
				
				Node nodenew=new Node();
				char b=(char) (obj+i);
				nodenew.setValue(b);
				node.setNext(nodenew);
				node=node.getNext();
				
			}
		}
		
		
		
	}
	public static void main(String[] args) {
		
		Nodelist nodelist=new Nodelist();
		Node head=new Node();
		nodelist.setNodelist(head,5, 'A');
		nodelist.print_list(head);
		Node b=nodelist.inverse(head);
		System.out.println("the inverse result is");
		nodelist.print_list(b);
		System.exit(0);
	}
	
	public Node inverse(Node r){
		Node n=r.getNext();
		while(n!=null){
			Node temp=n.getNext();
			n.setNext(r);
			r=n;
			n=temp;
		}
		return r;
	}
	public void print_list(Node node){
		
		while(node.getValue()!=null){
			
		System.out.println(node.getValue()+"->");
		node=node.getNext();
		 if(node.getValue()=="Head"||node.getNext()==null)
			 break;
			
		}
	}

}

 

你可能感兴趣的:(java)