简单链表实现

package app01;


import java.util.Collection;


@SuppressWarnings("rawtypes")
public class MyLinkedList <E>{

public Node<E> first;
public Node<E> last;
public int size;



@SuppressWarnings("hiding")
private class Node<E>{
E value;
Node<E> next;
Node<E> prev;
 
Node(E value, Node<E> prev){
this.value = value; 
this.prev = prev;
}
}

public Node getLast() {
return last;
}


public void setLast(Node<E> last) {
this.last = last;
}


/**

* <p>Discription:[b为true,正序遍历,否则逆序]</p>
* Created on 2017年5月4日
* @param flag
* @author:[风陵渡口]
*/
@SuppressWarnings("unchecked")
public void iterator(boolean flag){
E value;
if(flag){
Node it = first;
while(null != it){
value = (E) it.value;
System.out.println(value);
it = it.next; 
}
}else{
Node it = last;
while(null != it){
value = (E) it.value;
System.out.println(value);
it = it.prev; 
}
}
}

/**

* <p>Discription:[链表中添加节点]</p>
* Created on 2017年5月4日
* @param e
* @author:[风陵渡口]
*/
public void add(E e){
Node<E> node = new Node<E>(e, last);

if(null != last){
last.next = node;
last = node;
}else{
last = node;
first = node;
}
size++;
}

/**

* <p>Discription:[删除节点]</p>
* Created on 2017年5月4日
* @param e
* @author:[风凌渡口]
*/
public void delete(E e){
Node<E> toDel = getNode(e);

if(null == toDel){
System.out.println("该节点不存在");
return;
}

//中间节点
if(!e.equals(first.value) && !e.equals(last.value)){
toDel.prev.next = toDel.next;
toDel.next.prev = toDel.prev;
return;
}else if(e.equals(first.value)){     //第一个节点
first = first.next;
return;
}
last = last.prev;
last.next = null;
}

/**

* <p>Discription:[获取等于当前值的元素节点]</p>
* Created on 2017年5月4日
* @param e
* @return
* @author:[风凌渡口]
*/
public Node<E> getNode(E e){
if(null == e){
return null;
}

Node<E> tmp = first;
while(!e.equals(tmp)){
tmp = tmp.next;
if(null == tmp){
return null;
}else if(e.equals(tmp.value)){
return tmp;
}
}
return tmp; //返回第一个
}

/**

* <p>Discription:[获取指定位置元素的值]</p>
* Created on 2017年5月4日
* @param index
* @return
* @author:[风凌渡口]
*/
public E getByIndex(int index){
checkIndex(index);
Node<E> node = getNode(index);
return  node.value;
}

private Node<E> getNode(int index){
Node<E> node = null;
int tmp;

if(index < (size >> 1)){
node = first;
tmp = 0;
while(tmp < index){
node = node.next;
tmp++;
}
}else{
node = last;
tmp = size-1;
while(tmp > index){
node = node.prev;
tmp--;
}
}
return node;
}


/**

* <p>Discription:[元素位置检验]</p>
* Created on 2017年5月4日
* @param index
* @author:[风凌渡口]
*/
private void checkIndex(int index) {
if(!isElementIndex(index))
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}


/**

* <p>Discription:[判定索引的正确性]</p>
* Created on 2017年5月4日
* @param index
* @return
* @author:[风凌渡口]
*/
private boolean isElementIndex(int index) {
return index >= 0 && index <= size;
}

/**

* <p>Discription:[在指定位置插入元素]</p>
* Created on 2017年5月4日
* @param index
* @param e
* @author:[风凌渡口]
*/
public void add(int index, E e){
Node<E> before = getNode(index-1);
Node<E> after = before.next;
Node<E> newNode = new Node<E>(e, before);

before.next = newNode;
newNode.next = after;
after.prev = newNode;
size++;
}

/**

* <p>Discription:[指定位置插入集合]</p>
* Created on 2017年5月4日
* @param index
* @param collection
* @author:[风凌渡口]
*/
@SuppressWarnings("unchecked")
public void add(int index, Collection<E> collection){
Node<E> before = getNode(index-1);
Node<E> after = before.next;
E[] arr = (E[]) collection.toArray();

for(int i = 0; i < arr.length; i++){
Node newNode = new Node<E>(arr[i], before);
before.next = newNode;
before = before.next;

//处理尾部
if(i == (arr.length-1)){
before.next = after;
after.prev = before;
}
}
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值