今天看了数据结构的链式存储,写了一个简单的例子:
package com.test;
public class ImitateLinkedList<T> {
private Node head ;
private Node last;
//定义一个内部的节点类
private class Node{
private T element;
private Node next;
public Node(T element,Node next){
this.element = element;
this.next = next;
}
}
//元素的个数
private int size;
public ImitateLinkedList(){
head = last = null;
}
public void addElement(T element){
Node node = new Node(element,null);
if(size == 0){
head = last = node;
}else {
last.next = node;
last = node;
}
size++;
}
/**
* 输出链式列表的元素
*/
public void show(){
Node current = head;
while(current != null){
System.out.println(current.element);
current = current.next;
}
}
/**
* 在指定的位置加入元素
* @param index 索引位置
* @param element
*/
public void addElement(int index,T element){
//
if(index < 0){
index = 0;
}
Node node = new Node(element,null);
if(index == 0){
node.next = head;
head = node;
if(size == 0)
last = node;
}else if(index >= size -1){
last.next = node;
last = node;
}else {
Node previous = getNode(index -1);
Node next = getNode(index + 1);
node.next = previous.next;
previous.next = node;
}
size ++;
}
//得到索引位置的节点
private Node getNode(int index){
if(index < 0 || index >= size){
return null;
}
Node current = head;
for(int i = 0 ;i < index ;i++){
current = current.next;
}
return current;
}
/*
* 得到索引位置的元素
*/
public T get(int index){
checkIndex(index);
return getNode(index).element;
}
/**
* 删除索引位置的元素
* @param index
*/
public void removeElement(int index){
if(size == 0){
throw new RuntimeException("没有可删除的元素");
}
checkIndex(index);
if(index == 0){
head = head.next;
if(size == 1){
last = head;
}
}else if(index == size -1){
Node node = getNode(index - 1);
node.next = null;
last = node;
}else {
Node previous = getNode(index - 1);
Node removeNode = getNode(index);
previous.next = removeNode.next;
}
size --;
}
/**
*
* @param index 要删除的元素的索引
*/
private void checkIndex(int index) {
// TODO Auto-generated method stub
if(index < 0 || index >= size){
throw new IllegalArgumentException("invalid argument "+index);
}
}
/**
*
* @return 得到列表中最后一个元素
*/
public T getLast(){
return last == null ? null : last.element;
}
/**
* 得到列表中第一个元素
*
*/
public T getFirst(){
return head == null ? null : head.element;
}
public void removeFirst(){
if(size == 0){
throw new RuntimeException("列表为空,不能删除元素");
}
this.removeElement(0);
}
public void removeLast(){
if(size == 0){
throw new RuntimeException("列表为空,不能删除元素");
}
this.removeElement(size - 1);
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
}
下面是测试代码
package com.test;
import org.junit.Before;
import org.junit.Test;
public class ImitateLinkedListTest {
private ImitateLinkedList<String> linkedList ;
@Before
public void setUp(){
linkedList = new ImitateLinkedList<String>();
}
@Test
public void testAddElement(){
linkedList.addElement("asdf");
linkedList.addElement("bdf");
linkedList.addElement("cfsd");
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
}
@Test
public void testShow(){
linkedList.addElement("asdf");
linkedList.addElement("bdf");
linkedList.addElement("cfsd");
linkedList.show();
}
@Test
public void testAddElementoverride(){
linkedList.addElement(0,"asdf");
linkedList.addElement(0,"bdf");
linkedList.addElement(0,"cfsd");
linkedList.show();
}
@Test
public void testRemoveElement(){
linkedList.addElement(0,"asdf");
linkedList.addElement(1,"bdf");
linkedList.addElement(2,"cfsd");
linkedList.removeElement(0);
linkedList.removeElement(0);
linkedList.removeElement(0);
linkedList.show();
}
@Test
public void testGetFirst(){
linkedList.addElement(0,"asdf");
linkedList.addElement(1,"bdf");
linkedList.addElement(2,"ahi");
linkedList.addElement(1,"ghf");
//linkedList.addElement("hello");
//linkedList.addElement(0,"cfsd");
///linkedList.removeElement(0);
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
linkedList.show();
}
@Test
public void testremoveFirst(){
linkedList.addElement(0,"asdf");
linkedList.addElement(1,"ghf");
linkedList.addElement("ccc");
linkedList.removeFirst();
linkedList.removeLast();
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
linkedList.show();
}
}