java创建单链表的两种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37817685/article/details/83305972

/**
 * 
 */
/**   
 * @author jueying:   
 * @version 创建时间:2018-10-23 下午01:26:47   
 * 类说明   
 */
/**
 * @author jueying
 *
 */
public class Test {
	
	int size=0;//大小
	static Node headNode;
	
    int i=0;
	class Node{
		private Node next;//指针
		
		private int data;//数据域
		
	}
	
	//尾插法创建单链表  队列形式先进先出
	public void fun(Node node,int data){
		if(i<=10){
			Node newNode=new Node();//创建新的结点
			newNode.data=i;//设置数据域
			newNode.next=null;
			node.next=newNode;
			fun(newNode,++i);
		}
	}
	
	//头插法创建单链表  栈形式先进后出
	public void afterInsert(Node node,int data){
		
		if(i<=10){
			Node newNode=new Node();//创建新的结点
			newNode.data=i;//设置数据域
			newNode.next=node.next;
			node.next=newNode;
			afterInsert(node,++i);
		}
		
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		   Test test=new Test();
		   headNode=test.new Node();//头指针
           //new Test6().fun(headNode,0);前插法
		   new Test().afterInsert(headNode,0);//后插法
           System.out.println("创建后的链表是:");//0 1 2 3 4 5 6 7 8 9 10
           while(headNode.next!=null){
        	   headNode=headNode.next;
        	   System.out.print(headNode.data+" ");
           }
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_37817685/article/details/83305972