单链表-链式存储-Java实现
package com.wrh.lab.dataStructure.linearList;
public class SingleListNode<E> {
private SingleListNode<E> next;
private E data;
public SingleListNode(E data) {
this.data = data;
next = null;
}
public SingleListNode(E data, SingleListNode<E> nextNode) {
this.data = data;
next = nextNode;
}
/**
*
* @param data
*/
public void setData(E data) {
this.data = data;
}
/**
*
* @param next
*/
public void setNext(SingleListNode<E> next) {
this.next = next;
}
/**
* get the data
* @return the data
*/
public E getData() {
return data;
}
/**
*
* @return next node
*/
public SingleListNode<E> getNext() {
return next;
}
}
package com.wrh.lab.dataStructure.linearList;
/**
*
* @author wrh
* the interface of the linked list
*
*/
public interface LinkedList<E> {
/**
* insert the item at prior
* @param item
*/
public void insertAtPrior(E item);
/**
* insert the item at the end of the list
* @param item
*/
public void insertAtLast(E item);
/**
* insert the node item after the index
* @param index
* @param item
*/
public void insert(int index, E item);
/**
* remove the first node from the list
*/
public void removeFromFront();
/**
* remove the last node from the list
* @return the removed node
*/
public void removeFromLast();
/**
* remove the node in the index
* @param index
*/
public void remove(int index);
/**
* return true for empty and false for not empty
* @return true : empty; false : not empty
*/
public boolean isEmpty();
/**
*
* @return the length of the list
*/
public int getLength();
/**
* display all the nodes
*/
public void display();
}
package com.wrh.lab.dataStructure.linearList.impl;
import com.wrh.lab.dataStructure.linearList.LinkedList;
import com.wrh.lab.dataStructure.linearList.SingleListNode;
public class SingleLinkedListImpl<E> implements LinkedList<E> {
private SingleListNode<E> head;
private int length; //the length of the list
public SingleLinkedListImpl() {
head = new SingleListNode(null, null);
length = 0;
}
@Override
public void insertAtPrior(E item) {
SingleListNode<E> node = new SingleListNode<E>(item, null); //encpsule the item to an node
node.setNext(head.getNext()); //node.next = head.next
head.setNext(node); //head.next = node
length ++;
}
@Override
public void insertAtLast(E item) {
SingleListNode<E> node = new SingleListNode<E>(item, null);
SingleListNode<E> tmp = head;
// find the end node of the list
while (null != tmp.getNext()) {
tmp = tmp.getNext();
}
tmp.setNext(node);
length++;
}
@Override
public void insert(int index, E item) {
SingleListNode<E> node = new SingleListNode<E>(item, null);
SingleListNode<E> tmp = head;
int i = 1;
if (index > length || index < 0) {
System.out.println("the index is out of bounds");
} else {
//find the node index
while (null != tmp.getNext() && i <= index) {
tmp = tmp.getNext();
i++;
}
node.setNext(tmp.getNext());
tmp.setNext(node);
length++;
}
}
@Override
public void removeFromFront() {
if (length < 1) {
System.out.println("The list is null and you can not delete any node!");
} else {
SingleListNode<E> tmp = head;
head.setNext(tmp.getNext().getNext());
length--;
}
}
@Override
public void remove(int index) {
if (length < 1 || index > length) {
System.out.println("index is out of bounds");
} else {
SingleListNode<E> tmp = head;
int i = 1;
//get the node before index
while (null != tmp.getNext() && i < index) {
tmp = tmp.getNext();
i++;
}
tmp.setNext(tmp.getNext().getNext());
length--;
}
}
@Override
public void removeFromLast() {
if (length < 1) { // if the list is null
System.out.println("The list is null and you can not delete");
} else {
SingleListNode<E> tmp1 = head;
SingleListNode<E> tmp2 = head.getNext(); //set tmp2 -tmp1 = 1
while (null != tmp2.getNext()) {
tmp2 = tmp2.getNext();
tmp1 = tmp1.getNext();
}
tmp1.setNext(null);
length--;
}
}
@Override
public int getLength() {
return length;
}
@Override
public boolean isEmpty() {
boolean isEmpty = false;
if (0 == length) {
isEmpty = true;
} else {
isEmpty = false;
}
return isEmpty;
}
@Override
public void display() {
if (length < 1) {
System.out.println("The list is null");
} else {
SingleListNode<E> tmp = head;
while (null != tmp.getNext()) {
tmp = tmp.getNext();
System.out.print(tmp.getData() + " ");
}
}
}
//test the list
public static void main(String[] args) {
LinkedList<Integer> l = new SingleLinkedListImpl<Integer>();
System.out.println(l.isEmpty());
l.insertAtLast(1);
l.insertAtLast(2);
l.insert(2, 5);
System.out.println("the list is : ");
l.display();
System.out.println();
System.out.println("the length is :" + l.getLength());
l.remove(2);
l.display();
}
}
跟我走啊~~