单向链表反转(java)

<img src="http://my.csdn.net/my/album/detail/1772499" alt="" /><a target=_blank href="http://my.csdn.net/my/album/detail/1772499">http://my.csdn.net/my/album/detail/1772499</a>
注:除reverse()函数外,其他代码大部分来自http://www.oschina.net/code/snippet_137649_15800,部分修改


package com.ytt.datastructure;

import android.util.Log;

public class SingleList<T> implements LinkList<T> {
	protected Node<T> head;
	private String tag="SingleList";
	public SingleList() {
		head=new Node<T>();
	}
	public SingleList(T[] element) {
		this();
		Node<T> rear=this.head;
		for (int i = 0; i < element.length; i++) {
			rear.next=new Node<T>(element[i],null);
			rear=rear.next;
		}
	}

	@Override
	public boolean isEmpty() {
		return this.head.next==null;
	}

	@Override
	public int length() {
		int count=0;
		Node<T> p=this.head.next;
		while (p!=null) {
			count++;
			p=p.next;			
		}
		return count;
	}

	@Override
	public T get(int i) {
		if (i<0||i>this.length()) {
			return null;
		}else {
			Node<T> pNode=this.head;
			for (int j = 0; j <= i; j++) {
				pNode=pNode.next;
			}
			return pNode.data;
		}
		
	}

	@Override
	public void set(int i, T data) {
		if (i<0||i>=this.length()){
			throw new IndexOutOfBoundsException("指定i="+i+"越界");
		}else {
			Node<T> p=this.head;
			for (int j = 0; j <=i; j++) {
				p=p.next;
			}
			p.data=data;
		}
		
	}

	@Override
	//index range from 0 to list.length()
	public void insert(int i, T data) {
		if (data==null) {
			return;
		}
		if (i<0||i>this.length()) {
			throw new IndexOutOfBoundsException("指定i="+i+"越界");
		}
		if (i==0) {
			this.head.next=new Node<T>(data,this.head.next);
		}else {
			Node<T> p=this.head;			
				for (int j = 0; j <i; j++) {
					p=p.next;
				}
				p.next=new Node<T>(data, p.next);
			}
			
		}
	//to be tested

	@Override
	public void append(T data) {
		insert(this.length(), data);
	}

	@Override
	public T remove(int i) {
		if (i<=0||i>this.length()) {
			return null;
		}else {
			Node<T> p=this.head;
			for (int j = 0; j < i-1; j++) {
				p=p.next;
			}
			T data=p.next.data;
			p.next=p.next.next;
			return data;
		}
	}

	@Override
	public void removeAll() {
		this.head=null;		
	}

	@Override
	public int search(T key) {
		if (key==null||this.length()==0) {
			return -1;
		}else {
			int i=0;
			boolean searchFlag=false;
			Node<T> p=this.head;
			while(i<this.length()){
				i++;
				p = p.next;
				if(p.data.equals(key)){
					searchFlag=true;
					break;
				}					
			}
			if (searchFlag) {
				return i;
			}else {
				return -1;
			}			
		}
	}
	public SingleList<T> reverse() {
			Node<T> tmp = new Node<T>();	
			for (int i = 0; i < this.length()/2; i++) {
					tmp.data=this.get(i);
					int index=this.length()-1-i;
					this.set(i, this.get(index));
					this.set(index, tmp.data);
				}			
			return this;
		}
public void reverse3() {
  Node<T> temp = this.head.next;
        Node<T> header = this.head.next;
        Node<T> tail = this.head.next;
        if (this.head == null || this.head.next == null)
        {
            return;
        }
        header = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = header;
            header = tail;
            tail = temp;
        }
        this.head.next = header;
 }		
	}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值