java实现单链表及倒置单链表中的元素

要实现单链表,首先要建立链表的节点类:

Java代码 复制代码
  1. /**  
  2.  * 单链表的节点类,单链表的实现。  
  3.  */  
  4. package com.ty.third;   
  5.   
  6. /**  
  7.  * @author liming  
  8.  *  
  9.  */  
  10. public class SLLNode<T> {   
  11.     //你要储存的信息单位   
  12.     public T info;   
  13.        
  14.     //指向下一个元素   
  15.     public SLLNode<T> next;   
  16.        
  17.     public SLLNode(T i)   
  18.     {   
  19.         this(i,null);   
  20.     }   
  21.        
  22.     //第二个参数传入node是为了快速建立链表   
  23.     public SLLNode(T i, SLLNode<T> node)   
  24.     {   
  25.         this.info = i;   
  26.         this.next = node;   
  27.     }   
  28. }  
/**
 * 单链表的节点类,单链表的实现。
 */
package com.ty.third;

/**
 * @author liming
 *
 */
public class SLLNode<T> {
	//你要储存的信息单位
	public T info;
	
	//指向下一个元素
	public SLLNode<T> next;
	
	public SLLNode(T i)
	{
		this(i,null);
	}
	
	//第二个参数传入node是为了快速建立链表
	public SLLNode(T i, SLLNode<T> node)
	{
		this.info = i;
		this.next = node;
	}
}

然后,单链表类:

Java代码 复制代码
  1. package com.ty.third;   
  2.   
  3. public class SLLList<T>   
  4. {   
  5.     protected SLLNode<T> head, tail;   
  6.        
  7.     //当前的节点,用于遍历所有元素   
  8.     private SLLNode<T> cur = null;   
  9.        
  10.     //创建一个空的单链表   
  11.     public SLLList()   
  12.     {   
  13.         head = tail = null;   
  14.     }   
  15.        
  16.     //添加头节点   
  17.     public void addToHead(T info)   
  18.     {   
  19.         SLLNode<T> sllNode = new SLLNode<T>(info);   
  20.         //如果链表为空   
  21.         if(head == null)   
  22.         {   
  23.             head = sllNode;   
  24.             tail = head;   
  25.         }   
  26.         //链表不为空   
  27.         else  
  28.         {   
  29.             sllNode.next = head;   
  30.             head = sllNode;   
  31.         }   
  32.     }   
  33.        
  34.     //打印所有链表节点   
  35.     public void printAllNode()   
  36.     {   
  37.         SLLNode<T> temp = null;   
  38.         for(prepare();hasNext();next())   
  39.         {   
  40.             temp = getNextNode();   
  41.             System.out.println(temp.info);   
  42.         }   
  43.     }   
  44.        
  45.     //添加尾节点   
  46.     public void addToTail(T info)   
  47.     {   
  48.         SLLNode<T> sllNode = new SLLNode<T>(info);   
  49.         //链表为空,则直接创建头节点   
  50.         if(head == null)   
  51.         {   
  52.             addToHead(info);   
  53.         }   
  54.         //链表不为空,加到链表尾部   
  55.         else  
  56.         {   
  57.             tail.next = sllNode;   
  58.             tail = sllNode;   
  59.         }   
  60.     }   
  61.        
  62.     //列表是否为空   
  63.     public boolean isEmpty()   
  64.     {   
  65.         return (head == null);   
  66.     }   
  67.        
  68.     //判断列表是否有下一个元素   
  69.     public boolean hasNext()   
  70.     {   
  71.         if(isEmpty())   
  72.             return false;   
  73.         if(cur.next == null)   
  74.             return false;   
  75.         return true;   
  76.     }   
  77.        
  78.     //获得当前元素   
  79.     public SLLNode<T> getNextNode()   
  80.     {   
  81.         return cur.next;   
  82.     }   
  83.        
  84.     //当前元素下移   
  85.     public void next()   
  86.     {   
  87.         cur = cur.next;   
  88.     }   
  89.        
  90.     //为遍历准备   
  91.     public void prepare()   
  92.     {   
  93.         cur = new SLLNode<T>(null);   
  94.         cur.next = head;   
  95.     }   
  96.        
  97.     public static void main(String []args)   
  98.     {   
  99.         SLLList<String> sllList = new SLLList<String>();   
  100.         sllList.addToHead("Joking");   
  101.         sllList.addToHead("Testing");   
  102.         sllList.addToTail("Lily");   
  103.         sllList.printAllNode();   
  104.            
  105.         SLLList<String> sllList2 = new SLLList<String>();   
  106.         for(sllList.prepare(); sllList.hasNext(); sllList.next())   
  107.         {   
  108.             sllList2.addToHead(sllList.getNextNode().info);   
  109.         }   
  110.         sllList2.printAllNode();   
  111.     }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值