PASCAL語言之父、圖靈獎得主尼古拉斯·沃斯(Niklaus Wirth)曾有一句在計算機領域幾乎人盡皆知的名言:「算法+數據結構=程序」(Algorithm+Data Structures=Programs)。java
數據結構指的是是計算機存儲、組織數據的方式。其中數據的邏輯結構分爲線性結構和非線性結構。node
線性結構是一個有序數據元素的集合,其數據元素之間的關係是一對一的關係,即除了第一個和最後一個數據元素以外,其它數據元素都是首尾相接的。算法
最基礎的兩種表示元素集合的方式就是數組和鏈表。數組
數組指的是有序的元素序列。其中一維數組是線性的數據結構。數組的優勢是經過索引能夠訪問任意元素,查找快。數組的缺點是在初始化數組的時候,就要知道數組的元素數量,並且插入和刪除元素很是複雜。數據結構
鏈表就很好地解決了數組的缺點,其使用的空間大小和元素數量成正比,即不須要提供初始化的大小,並且插入和刪除元素比較簡單,不會「牽一髮而動全身」。鏈表的缺點是須要經過引用訪問任意元素,查找很複雜。ide
鏈表是一種遞歸的數據結構。通常鏈表分爲單向鏈表和雙向鏈表兩種實現。測試
單向鏈表(單鏈表、線性鏈表)是鏈表的一種,其特色是鏈表的連接方向是單向的,對鏈表的訪問要經過從頭部開始,依序往下讀取。而雙向鏈表(雙鏈表)的每一個數據結點中都有兩個指針,分別指向直接後繼和直接前驅。因此,從雙向鏈表中的任意一個結點開始,均可以很方便地訪問它的前驅結點和後繼結點。spa
下面經過Java語言實現單向鏈表,來展現鏈表的相關操做。.net
單向鏈表的每一個節點只須要定義兩個屬性:當前節點的元素,下一個節點。指針
class Node{
Item item;
Node node;
}
複製代碼
棧是一種先進後出(FILO)的數據結構,若是經過數組實現棧,則要考慮數組擴容的問題,能夠經過鏈表來解決這個問題。
下面經過鏈表實現棧,並藉此演示了在鏈表的頭部增長節點
以及除鏈表頭部節點
。
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack;
public interface Stack<Item> {
/** * add an item * * @param item */
void push(Item item);
/** * remove the most recently added item * * @return */
Item pop();
/** * is the stack empty? * * @return */
boolean isEmpty();
/** * number of items in the stac */
int size();
}
複製代碼
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.impl;
import net.ijiangtao.tech.algorithms.algorithmall.datastructure.stack.Stack;
import java.util.Iterator;
/** * 鏈表實現可迭代的棧 * @param <Item> * @author ijiangtao.net */
public class IterableLinkedListStack<Item> implements Stack<Item>,Iterable<Item> {
/** 棧頂元素 */
private Node first;
/** 棧元素數量 */
private int N;
/** 經過內部類定義鏈表節點 */
private class Node{
Item item;
Node next;
}
/** * 向棧頂增長元素,即在鏈表的頭插入節點 * @param item */
@Override
public void push(Item item) {
Node oldFirst=first;
first=new Node();
first.item=item;
first.next=oldFirst;
N++;
}
/** * 從棧頂移除元素,即移除鏈表的頭部節點 * @return */
@Override
public Item pop() {
Item item=first.item;
first=first.next;
N--;
return item;
}
@Override
public boolean isEmpty() {
return N==0;
}
@Override
public int size() {
return N;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
/** * 支持迭代方法,實如今內部類裏 */
private class ListIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
//或者 N!=0
return current !=null;
}
@Override
public Item next() {
Item item=current.item;
current=current.next;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public static void main(String[] args) {
//測試
Stack<String> stack=new IterableLinkedListStack<>();
stack.push("aa");
stack.push("bbb");
stack.push("abc");
for (String s:(IterableLinkedListStack<String>)stack) {
System.out.println(s);
}
System.out.println("stack.size="+stack.size());
System.out.println("stack.pop="+stack.pop());
System.out.println("stack.size="+stack.size());
System.out.println("stack.isEmpty="+stack.isEmpty());
System.out.println("stack.pop="+stack.pop());
System.out.println("stack.pop="+stack.pop());
System.out.println("stack.size="+stack.size());
System.out.println("stack.isEmpty="+stack.isEmpty());
//測試輸出
/** * abc * bbb * aa * stack.size=3 * stack.pop=abc * stack.size=2 * stack.isEmpty=false * stack.pop=bbb * stack.pop=aa * stack.size=0 * stack.isEmpty=true */
}
}
複製代碼
隊列是一種基於先進先出(FIFO)策略的集合模型。
下面基於鏈表實現隊列,並演示使用。
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.queue;
/** * 隊列 * @author ijiangtao.net * @param <Item> */
public interface Queue<Item> {
/** * 隊列是否爲空 * @return */
boolean isEmpty();
/** * 隊列大小 * @return */
int size();
/** * 入隊 * @param item */
public void enqueue(Item item);
/** * 出隊 * @return */
public Item dequeue();
}
複製代碼
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.queue.impl;
import net.ijiangtao.tech.algorithms.algorithmall.datastructure.queue.Queue;
import java.util.Iterator;
/** * 隊列 * @author ijiangtao.net * @param <Item> */
public class IterableLinkedListQueue<Item> implements Queue<Item>,Iterable<Item>{
/** 隊頭,最初入隊的節點 */
private Node first;
/**隊尾,最晚入隊的節點*/
private Node last;
/** 隊列中元素的數量 */
private int N;
private class Node{
Item item;
Node next;
}
@Override
public boolean isEmpty() {
//或N==0
return first==null;
}
@Override
public int size() {
return N;
}
/** * 入隊,即向隊尾增長一個元素 * @param item */
@Override
public void enqueue(Item item) {
Node oldLast=last;
last=new Node();
last.item=item;
last.next=null;
if (isEmpty()){
first=last;
}else {
oldLast.next=last;
}
N++;
}
/** * 出隊,即移除隊頭元素 * @return */
@Override
public Item dequeue() {
Item item=first.item;
first=first.next;
if (isEmpty()){
last=null;
}
N--;
return item;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
/** * 支持迭代方法,實如今內部類裏 */
private class ListIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
//或者 N!=0
return current !=null;
}
@Override
public Item next() {
Item item=current.item;
current=current.next;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public static void main(String[] args) {
//測試
Queue<String> queue=new IterableLinkedListQueue<>();
queue.enqueue("aa");
queue.enqueue("bbb");
queue.enqueue("abc");
for (String s:(IterableLinkedListQueue<String>)queue) {
System.out.println(s);
}
System.out.println("queue.size="+queue.size());
System.out.println("queue.dequeue="+queue.dequeue());
System.out.println("queue.size="+queue.size());
System.out.println("queue.isEmpty="+queue.isEmpty());
System.out.println("queue.dequeue="+queue.dequeue());
System.out.println("queue.dequeue="+queue.dequeue());
System.out.println("queue.size="+queue.size());
System.out.println("queue.isEmpty="+queue.isEmpty());
//測試輸出
/** * aa * bbb * abc * queue.size=3 * queue.dequeue=aa * queue.size=2 * queue.isEmpty=false * queue.dequeue=bbb * queue.dequeue=abc * queue.size=0 * queue.isEmpty=true */
}
}
複製代碼
揹包是一種只支持添加不支持刪除的容器,它的做用是收集元素,並遍歷所收集到的元素。
包(Bag)與棧同樣,遵循先進後出的策略(FILO)。
下面提供基於鏈表的揹包實現。
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.bag;
/** * 揹包 * @author ijiangtao.net * @param <Item> */
public interface Bag<Item> extends Iterable<Item>{
/** * 向背包內加入元素 * @param item */
void add(Item item);
}
複製代碼
package net.ijiangtao.tech.algorithms.algorithmall.datastructure.bag.impl;
import net.ijiangtao.tech.algorithms.algorithmall.datastructure.bag.Bag;
import java.util.Iterator;
public class IterableLinkedListBag<Item> implements Bag<Item> {
private Node first;
private class Node{
Item item;
Node next;
}
@Override
public void add(Item item) {
Node oldFirst=first;
first=new Node();
first.item=item;
first.next=oldFirst;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
/** * 支持迭代方法,實如今內部類裏 */
private class ListIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
//或者 N!=0
return current !=null;
}
@Override
public Item next() {
Item item=current.item;
current=current.next;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public static void main(String[] args) {
//測試
Bag<String> bag=new IterableLinkedListBag<>();
bag.add("aa");
bag.add("bbb");
bag.add("abc");
for (String s:bag) {
System.out.println(s);
}
//測試輸出
/** * abc * bbb * aa */
}
}
複製代碼