单向链表
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;
列表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向NULL的指针。
单向链表的结构
图片(注:百度搜索而来的图片)链接: link.
java代码实现
public class LinkedList<E> {
private Node head;
private int size;
public LinkedList() {
this.head = null;
this.size = 0;
}
public static void main(String[] args) {
int[] arr = new int[]{49, 45, 32, 32, 32, 6, 32, 36, 45, 48, 18, 13, 50, 46, 8 };
LinkedList<Integer> list = new LinkedList<>();
System.out.println(list);
// for (int i = 0; i < arr.length; i++) {
// System.out.println("add e: "+ arr[i]);
// list.addFirst(arr[i]);
// System.out.println("list's size is "+list.getSize());
// System.out.println(list);
// System.out.println("---------------------------------------");
// }
for (int i = 0; i < arr.length; i++) {
System.out.println("add e: " + arr[i]);
list.addLast(arr[i]);
System.out.println("list's size is " + list.getSize());
System.out.println(list);
System.out.println("---------------------------------------");
}
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++ ");
System.out.println("add (index 5, e 24)");
list.add(5, 24);
System.out.println("list's size is " + list.getSize());
System.out.println(list);
System.out.println("---------------------------------------");
System.out.println("list contail 3 :" + list.contail(3));
System.out.println("get first: " + list.getFirst());
System.out.println("get last: " + list.getLast());
System.out.println("get 5: " + list.get(5));
list.set(5, 100);
System.out.println("set (5,100): " + list);
list.remove(5);
System.out.println("remove (5): " + list);
}
/**
* 链表的长度
*
* @return
*/
public int getSize() {
return this.size;
}
/**
* 链表是否为空
*
* @return
*/
public boolean isEmpty() {
return this.size == 0;
}
/**
* 在链表头部加入元素 e
*
* @param e
*/
public void addFirst(E e) {
if (this.head == null) {
this.head = new Node(e);
this.size++;
return;
}
Node h_node = this.head;
Node new_head = new Node(e);
new_head.next = h_node;
this.head = new_head;
this.size++;
}
/**
* 在链表尾部添加元素 e
*
* @param e
*/
public void addLast(E e) {
if (this.head == null) {
this.head = new Node(e);
this.size++;
return;
}
Node tail = this.head;
while (tail.next != null) {
tail = tail.next;
}
tail.next = new Node(e);
this.size++;
}
/**
* 在 [0,this.size]范围内添加元素e
*
* @param index
* @param e
*/
public void add(int index, E e) {
if (this.size < 0 || index > this.size) {
throw new IllegalArgumentException("Add failed. illegal index.");
}
if (index == 0) {
addFirst(e);
}
Node pre_node = this.head;
for (int i = 0; i < index - 1; i++) {
pre_node = pre_node.next;
}
Node nextNode = pre_node.next;
pre_node.next = new Node(e);
pre_node.next.next = nextNode;
this.size++;
}
@Override
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("head : ");
Node curNode = this.head;
while (curNode != null) {
stringBuffer.append(curNode.e + " => ");
curNode = curNode.next;
}
stringBuffer.append(" tail.");
return stringBuffer.toString();
}
/**
* 获取第一个元素
*
* @return
*/
public E getFirst() {
return get(0);
}
/**
* 获取索引为index的元素
*
* @param index
* @return
*/
public E get(int index) {
if (index < 0 || index >= this.size) {
throw new IllegalArgumentException("Get failed. illegal index");
}
Node curNode = this.head;
for (int i = 0; i < index; i++) {
curNode = curNode.next;
}
return curNode.e;
}
/**
* 获取最后一位的元素
*
* @return
*/
public E getLast() {
return get(this.size - 1);
}
/**
* 修改索引为index的元素为e
*
* @param index
* @param e
*/
public void set(int index, E e) {
if (index < 0 || index >= this.size) {
throw new IllegalArgumentException("Set failed. illegal index");
}
Node curNode = this.head;
for (int i = 0; i < index; i++) {
curNode = curNode.next;
}
curNode.e = e;
}
/**
* 链表中是否包含元素e
*
* @param e
* @return
*/
public boolean contail(E e) {
Node curNode = this.head;
while (curNode != null) {
if (curNode.e.equals(e)) {
return true;
}
curNode = curNode.next;
}
return false;
}
/**
* 删除索引为index的元素
*
* @param index
* @return
*/
public E remove(int index) {
if (index < 0 || index >= this.size) {
throw new IllegalArgumentException("Remove fail,illegal index.");
}
Node curNode = this.head;
for (int i = 0; i < index - 1; i++) {
curNode = curNode.next;
}
Node nextNode = curNode.next;
curNode.next = nextNode.next;
nextNode.next = null;
this.size--;
return nextNode.e;
}
/**
* 删除链表头部的元素
*
* @return
*/
public E removeFirst() {
return remove(0);
}
/**
* 删除链表尾部的元素
*
* @return
*/
public E removeLast() {
return remove(this.size - 1);
}
/**
* 节点
*/
private class Node {
private E e;
private Node next;
public Node(E e) {
this.e = e;
this.next = null;
}
@Override
public String toString() {
return this.e.toString();
}
}
}
head : tail.
add e: 49
list's size is 1
head : 49 => tail.
---------------------------------------
add e: 45
list's size is 2
head : 49 => 45 => tail.
---------------------------------------
add e: 32
list's size is 3
head : 49 => 45 => 32 => tail.
---------------------------------------
add e: 32
list's size is 4
head : 49 => 45 => 32 => 32 => tail.
---------------------------------------
add e: 32
list's size is 5
head : 49 => 45 => 32 => 32 => 32 => tail.
---------------------------------------
add e: 6
list's size is 6
head : 49 => 45 => 32 => 32 => 32 => 6 => tail.
---------------------------------------
add e: 32
list's size is 7
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => tail.
---------------------------------------
add e: 36
list's size is 8
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => tail.
---------------------------------------
add e: 45
list's size is 9
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => tail.
---------------------------------------
add e: 48
list's size is 10
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => tail.
---------------------------------------
add e: 18
list's size is 11
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => tail.
---------------------------------------
add e: 13
list's size is 12
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => tail.
---------------------------------------
add e: 50
list's size is 13
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => tail.
---------------------------------------
add e: 46
list's size is 14
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => tail.
---------------------------------------
add e: 8
list's size is 15
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 => tail.
---------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++
add (index 5, e 24)
list's size is 16
head : 49 => 45 => 32 => 32 => 32 => 24 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 => tail.
---------------------------------------
list contail 3 :false
get first: 49
get last: 8
get 5: 24
set (5,100): head : 49 => 45 => 32 => 32 => 32 => 100 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 => tail.
remove (5): head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 => tail.