package com.linkedlist.oneWay;
public class Node<E> {
private E data;
//下一个元素
private Node<E> next;
public Node(E data, Node<E> next) {
this.data = data;
if (next == null) {
} else {
this.next = next;
}
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
/**
* 获取上下一个元素
* @return
*/
public Node<E> getNext() {
return next;
}
/**
* 设置下一个元素
* @param next
*/
public void setNext(Node<E> next) {
this.next = next;
}
}
/**OneWayLinkedList类**/
package com.linkedlist.oneWay;
/**
* 根据数据结构单向链表原理,简单实现单向链表。
* 单向链表主要是记录链表中的第i个元素a,及a的下一个元素(第i+1个元素)b。
* a与b组成的数据称为单向链表的一个节点。
* 链表有N个节点组成。
* @author dev1
*
* @param <E>
*/
public class OneWayLinkedList<E> {
// 第一个元素
private Node<E> first;
// 最后一个元素
private Node<E> last;
private int size = 0;
public OneWayLinkedList() {
}
/**
* 向列表最后追加元素
*
* @param data
* @return
*/
public boolean add(E data) {
if (this.last == null) {
if (this.first == null) {
// 第一个元素
this.first = new Node<E>(data, null);
} else {
// 第二个元素
this.last = new Node<E>(data, null);
this.first.setNext(this.last);
}
} else {
Node<E> node = this.last;
Node<E> newNode = new Node<E>(data, null);
node.setNext(newNode);
this.last = newNode;
}
this.size++;
return true;
}
/**
* 获取第i个位置的元素
*
* @param i
* @return
*/
public E getEbyIndex(int i) {
Node<E> node = getNodeByIndex(i);
return node.getData();
}
/**
* 向i节点插入一个元素
* @param i
* @param newE
*/
public void addEByIndex(int i, E newE){
Node<E> ni = getNodeByIndex(i-1);
Node<E> newNode = new Node<E>(newE, ni.getNext());
ni.setNext(newNode);
this.size++;
}
/**
* 获得某一个节点
* @param i
* @return
*/
public Node<E> getNodeByIndex(int i) {
Node<E> node = this.first;
int j = 1;
if (j < i) {
while (j < i) {
node = node.getNext();
j++;
}
}
return node;
}
public void deleteEByIndex(int i) {
Node<E> node1 = getNodeByIndex(i);
Node<E> node2 = getNodeByIndex(i-1);
node2.setNext(node1.getNext());
this.size--;
}
/**
* 获取第一个元素
*
* @return
*/
public Node<E> getFirst() {
return first;
}
/**
* 获取最后一个元素
*
* @return
*/
public Node<E> getLast() {
return last;
}
/**
* 获取链表的元素个数
*
* @return
*/
public int getSize() {
return size;
}
}
/**TestMain类**/
package com.linkedlist.oneWay;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
String str1 = "123";
String str2 = "456";
String str3 = "789";
String str4 = "qwe";
OneWayLinkedList<String> owlls = new OneWayLinkedList<String>();
owlls.add(str1);
owlls.add(str2);
owlls.add(str3);
owlls.add(str4);
Node<String> firstNode = owlls.getFirst();
System.out.println(firstNode + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(1) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(2) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(3) + "\t" + owlls.getSize());
owlls.addEByIndex(2, "mkf");
System.out.println(owlls.getEbyIndex(1) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(2) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(3) + "\t" + owlls.getSize());
owlls.deleteEByIndex(2);
System.out.println(owlls.getEbyIndex(1) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(2) + "\t" + owlls.getSize());
System.out.println(owlls.getEbyIndex(3) + "\t" + owlls.getSize());
}
}