java 实现的单向链表

学java一年多了,暑假跟着老师做了一个项目,发现自己的基础确实不行啊,就会点SSH,想认真的去看看他们的源码,发现一点也看不懂,开学了,给自己定了一些目标,so最近在看thinkinginjava、深入java虚拟机这两本书,打算好好学学java基础,刚刚做完了一个练习,用java实现单向链表,回想起自己在java上基本没有用过数据结构的东西,只有以前用C写过一些数据结构的小东西,如,单向链表,双向链表,一些排序算法等,一开始还真不知道怎么写勒,于是乎想起了LinkedList,然后又在网上查了查资料,还算是完成了这个练习,写写博客,加深自己的映像。

package com.lsw.first;

import java.util.Iterator;
/**
 * 表头为空的单向链表,支持基本的crud操作
 * @author  lsww
 * @E-mail  [email protected]
 * @createtime 2012-9-16 下午08:58:35
 * @version 1.0
 * @since  JDK 1.6 
 * @param <E>
 */
public class MyLinkList<E> {
	private Entry<E> header = new Entry<E>(null);	//表头为空
	private Entry<E> tail = null;	//表尾
	private Entry<E> another = null;	//用于操作各节点的节点变量
	private int size = 0;	//节点数目
	
	public void add(E element) {	//添加一个元素(在表尾添加新元素)
		Entry<E> newEntry = new Entry<E>(element);
		if(size == 0) { 
			tail = newEntry;
			header.next = tail;
		} else {
			tail.next = newEntry;
			tail = newEntry;
		}
		size++;
	}
	
	public void clear() {	//清空链表
		header = new Entry<E>(null);
		tail = null;
		another = null;
		size = 0;
	}
	
	public boolean insert(E element, E insertElement) {	//在指定的元素前插入一个元素
		another = header.next;
		Entry<E> previous = header;
		while(another != null) {
			if(another.element.equals(element)) {
				Entry<E> newEntry = new Entry<E>(insertElement);
				previous.next = newEntry;
				newEntry.next = another;
				size++;
				return true;
			}
			previous = another;
			another = another.next;
		}
		return false;
	}
	
	public boolean isEmpty() {	//判断链表是否为空
		return size > 0 ? false : true;
	}
	
	public boolean set(E oldElement, E newElement) {	//用新值替换旧值
		another = header.next;
		while(another != null) {
			if(another.element.equals(oldElement)) {
				another.element = newElement;
				return true;
			}
			another = another.next;
		}
		return false;
	}
	
	public E getFirst() {	//得到链表的第一个元素
		return header.next.element;
	}
	
	public E getLast() {	//得到链表的最后一个元素
		return tail.element;
	}
	
	public boolean contains(E element) {	//链表中是否包含某个元素
		another = header.next;
		while(another != null) {
			if(another.element.equals(element)) {
				return true;
			}
			another = another.next;
		}
		return false;
	}
	
	public boolean remove(E element) {	//移除指定的元素
		another = header.next;
		Entry<E> previous = header;	//被移除元素的上一个元素(如果此元素存在)
		while(another != null) {
			if(another.element.equals(element)) {
				previous.next = another.next;
				size--;
				return true;
			}
			previous = another;
			another = another.next;
		}
		return false;
	}
	
	public boolean removeFirst() {	//移除第一个元素
		if(size <= 0) {
			return false;
		}
		header.next = header.next.next;
		return true;
	}
	
	public boolean removeLast() {	//移除最后一个元素
		if(size <= 0) {
			return false;
		}
		another = header.next;
		Entry<E> previous = header;
		while(another.next != null) {
			previous = another;
			another = another.next;
		}
		previous.next = another.next;
		size--;
		return true;
	}
	
	public int size() {
		return size;
	}
	
	public Iterator<E> iterator() {
		return new Itr();
	}
	
	private class Itr implements Iterator<E> {	//实现的迭代器
		private Entry<E> current = header.next;
		@Override
		public boolean hasNext() {
			if(current != null) {
				return true;
			}
			return false;
		}

		@Override
		public E next() {
			E element = current.element;
			current = current.next;
			return element;
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
		
	}
	
	private static class Entry<E> {	//链表上的节点
		E element;
		Entry<E> next;
		Entry(E element) {
			this.element = element;
			next = null;
		}
	}
	
	public void test() {	//测试函数
		another = header.next;
		while(another != null) {
			System.out.println(another.element);
			another = another.next;
		}
 	}
}

你可能感兴趣的:(java,单向链表)