演算法(二)集合類資料型別實現
阿新 • • 發佈:2018-12-11
陣列實現棧
package com.lwl.algorithm.stack; import java.util.Iterator; import java.util.NoSuchElementException; /** *動態調整陣列大小的棧實現 */ public class ResizingArrayStack<Item> implements Iterable<Item> { private Item[] a; // array of items private int n; // number of elements on stack /** * Initializes an empty stack. */ public ResizingArrayStack() { a = (Item[]) new Object[2]; n = 0; } /** * Is this stack empty? * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return n == 0; } /** * Returns the number of items in the stack. * @return the number of items in the stack */ public int size() { return n; } // resize the underlying array holding the elements private void resize(int capacity) { assert capacity >= n; //如果為true,繼續執行,false丟擲 AssertionError // textbook implementation Item[] temp = (Item[]) new Object[capacity]; for (int i = 0; i < n; i++) { temp[i] = a[i]; } a = temp; // alternative implementation // a = java.util.Arrays.copyOf(a, capacity); } /** * Adds the item to this stack. * @param item the item to add */ public void push(Item item) { if (n == a.length) resize(2*a.length); // double size of array if necessary a[n++] = item; // add item } /** * Removes and returns the item most recently added to this stack. * @return the item most recently added * @throws java.util.NoSuchElementException if this stack is empty */ public Item pop() { if (isEmpty()) { throw new NoSuchElementException("Stack underflow"); } Item item = a[n-1]; a[n-1] = null; // to avoid loitering n--; // shrink size of array if necessary if (n > 0 && n == a.length/4) { resize(a.length / 2); } return item; } /** * 返回棧頂元素 * @return */ public Item peek() { if (isEmpty()) { throw new NoSuchElementException("Stack underflow"); } return a[n-1]; } /** * Returns an iterator to this stack that iterates through the items in LIFO order. * @return an iterator to this stack that iterates through the items in LIFO order. */ @Override public Iterator<Item> iterator() { return new ReverseArrayIterator(); } // an iterator, doesn't implement remove() since it's optional private class ReverseArrayIterator implements Iterator<Item> { private int i; public ReverseArrayIterator() { i = n-1; } @Override public boolean hasNext() { return i >= 0; } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public Item next() { if (!hasNext()) { throw new NoSuchElementException(); } return a[i--]; } } /** * Unit tests the {@code Stack} data type. * * @param args the command-line arguments */ public static void main(String[] args) { ResizingArrayStack<String> stack = new ResizingArrayStack<String>(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { stack.push(item); } else if (!stack.isEmpty()) { StdOut.print(stack.pop() + " "); } } StdOut.println("(" + stack.size() + " left on stack)"); } }
連結串列實現棧
package com.lwl.algorithm.stack; import java.util.Iterator; import java.util.NoSuchElementException; /** * 連結串列實現棧 * @param <Item> */ public class Stack<Item> implements Iterable<Item> { private Node<Item> first; // top of stack private int n; // size of the stack // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; } /** * Initializes an empty stack. */ public Stack() { first = null; n = 0; } /** * Returns true if this stack is empty. * * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return first == null; } /** * Returns the number of items in this stack. * * @return the number of items in this stack */ public int size() { return n; } /** * Adds the item to this stack. * * @param item the item to add */ public void push(Item item) { Node<Item> oldfirst = first; first = new Node<Item>(); first.item = item; first.next = oldfirst; n++; } public void push2(Item item) { Node<Item> oldfirst=first; first=new Node<Item>(); first.item=item; first.next=oldfirst; } /** * Removes and returns the item most recently added to this stack. * * @return the item most recently added * @throws NoSuchElementException if this stack is empty */ public Item pop() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); Item item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item } public Item pop2() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); Item item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item } /** * Returns (but does not remove) the item most recently added to this stack. * * @return the item most recently added to this stack * @throws NoSuchElementException if this stack is empty */ public Item peek() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); return first.item; } /** * Returns a string representation of this stack. * * @return the sequence of items in this stack in LIFO order, separated by spaces */ public String toString() { StringBuilder s = new StringBuilder(); for (Item item : this) { s.append(item); s.append(' '); } return s.toString(); } /** * Returns an iterator to this stack that iterates through the items in LIFO order. * * @return an iterator to this stack that iterates through the items in LIFO order */ public Iterator<Item> iterator() { return new ListIterator<Item>(first); } // an iterator, doesn't implement remove() since it's optional private class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; } } /** * Unit tests the {@code Stack} data type. * * @param args the command-line arguments */ public static void main(String[] args) { Stack<String> stack = new Stack<String>(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) stack.push(item); else if (!stack.isEmpty()) StdOut.print(stack.pop() + " "); } StdOut.println("(" + stack.size() + " left on stack)"); } }
連結串列實現
已實現
(一)表頭插入節點 (二)表頭刪除 (三)表尾插入
未實現(雙向連結串列實現)
(一)刪除指定節點 (二)在指定節點前插入一個新節點
佇列實現
package com.lwl.algorithm.linkedlist; import com.lwl.algorithm.stack.StdIn; import com.lwl.algorithm.stack.StdOut; import java.util.Iterator; import java.util.NoSuchElementException; /** * 連結串列實現佇列 * @param <Item> */ public class Queue<Item> implements Iterable<Item> { private Node<Item> first; // beginning of queue private Node<Item> last; // end of queue private int n; // number of elements on queue // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; } /** * Initializes an empty queue. */ public Queue() { first = null; last = null; n = 0; } /** * Returns true if this queue is empty. * * @return {@code true} if this queue is empty; {@code false} otherwise */ public boolean isEmpty() { return first == null; } /** * Returns the number of items in this queue. * * @return the number of items in this queue */ public int size() { return n; } /** * Returns the item least recently added to this queue.返回首節點 * * @return the item least recently added to this queue * @throws NoSuchElementException if this queue is empty */ public Item peek() { if (isEmpty()) { throw new NoSuchElementException("Queue underflow"); } return first.item; } /** * 向表尾新增元素 * @param item */ public void enqueue(Item item) { Node<Item> oldlast = last; last = new Node<Item>(); last.item = item; last.next = null; if (isEmpty()) { first = last; } else { oldlast.next = last; } n++; } /** * 自己實現 */ public void enqueue2(Item item){ Node<Item> oldlast=last; last = new Node<>(); last.item=item; last.next=null; if (isEmpty()){ first=last; }else { oldlast.next=last; } } /** * 從表頭刪除元素 * @return */ public Item dequeue() { if (isEmpty()) { throw new NoSuchElementException("Queue underflow"); } Item item = first.item; first = first.next; //不懂 n--; if (isEmpty()) { last = null; // to avoid loitering } return item; } /** * 自己實現(模糊) */ public Item dequeue2(){ if (isEmpty()){ throw new NoSuchElementException("Queue underflow"); } Item item = first.item; //新的first賦值 first=first.next; n--; if (isEmpty()){ last=null; } return item; } /** * Returns a string representation of this queue. * * @return the sequence of items in FIFO order, separated by spaces */ @Override public String toString() { StringBuilder s = new StringBuilder(); for (Item item : this) { s.append(item); s.append(' '); } return s.toString(); } /** * Returns an iterator that iterates over the items in this queue in FIFO order. * * @return an iterator that iterates over the items in this queue in FIFO order */ @Override public Iterator<Item> iterator() { return new ListIterator<Item>(first); } // an iterator, doesn't implement remove() since it's optional private class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } @Override public boolean hasNext() { return current != null; } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public Item next() { if (!hasNext()) { throw new NoSuchElementException(); } Item item = current.item; current = current.next; return item; } } /** * Unit tests the {@code Queue} data type. * * @param args the command-line arguments */ public static void main(String[] args) { Queue<String> queue = new Queue<String>(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { queue.enqueue(item); } else if (!queue.isEmpty()) { StdOut.print(queue.dequeue() + " "); } } StdOut.println("(" + queue.size() + " left on queue)"); } }
揹包實現
package com.lwl.algorithm.bag;
import com.lwl.algorithm.stack.StdIn;
import com.lwl.algorithm.stack.StdOut;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @author liuweilong
* @ClassName: bag
* @Description: 連結串列實現揹包
* @Version 1.0
**/
public class Bag<Item> implements Iterable<Item> {
private Node<Item> first; // beginning of bag
private int n; // number of elements in bag
// helper linked list class
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
/**
* Initializes an empty bag.
*/
public Bag() {
first = null;
n = 0;
}
/**
* Returns true if this bag is empty.
*
* @return {@code true} if this bag is empty;
* {@code false} otherwise
*/
public boolean isEmpty() {
return first == null;
}
/**
* Returns the number of items in this bag.
*
* @return the number of items in this bag
*/
public int size() {
return n;
}
/**
* Adds the item to this bag.
*
* @param item the item to add to this bag
*/
public void add(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}
/**
* Returns an iterator that iterates over the items in this bag in arbitrary order.
*
* @return an iterator that iterates over the items in this bag in arbitrary order
*/
public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;
public ListIterator(Node<Item> first) {
current = first;
}
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
/**
* Unit tests the {@code bag} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
Bag<String> bag = new Bag<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
bag.add(item);
}
StdOut.println("size of bag = " + bag.size());
for (String s : bag) {
StdOut.println(s);
}
}
}