这周课上讲了基础数据结构:
背包(Bags),栈(Stacks)。首先对这两种数据结构有一个基础的认识:
背包:是一种不支持删除元素的集合数据类型,它的目的就是帮助用例收集元素并迭代遍历所有收集到的元素。就像一个袋子里
装弹珠,不过是一次一个。从背包口添加元素,也是从背包口出来,是后进先出。
下压栈(简称栈):是一种基于后进先出策略的集合类型。就像试管,只能从管口倒水,从管口出水。这说明添加元素是从管口,删除元素也是从管口。
这里面教授给出了这三种数据结构基本的接口设计(是我的教授自己设计的。不多做评价。)
背包(Bags):
public interface Bag<Item> extends Iterable <Item> { /** * Update this it bag by adding item; * No guarantee is made regarding the order of items in the iterator; * @param Item the item to add; */ void add(Item item); /** * @return true if this bag is empty; */ boolean isEmpty(); /** * @return the number of elements in this bag (not the capacity which is an implementation-dependent feature) */ int size(); }
堆栈(Stacks):
public interface Stack<Item> { /** * Update this stack by adding item on the top; * @param Item the item to add; */ void push(Item Item); /** * Update this stack by taking the top item of this stack; * @return this item; * @throws Exception */ Item pop() throws Exception; /** * take the peek at the item on the top of this stack; * @return */ Item peek(); /** * @return true if this stack is empty */ boolean isEmpty(); }
队列(Queue):
public interface Queue<Item> { /** * Update this Queue by adding an item on the newest end; * @param item the item to add; */ void enqueue(Item item); /** * Update this Queue by taking the oldest item off the queue * @return the item or null if there is no such item; */ Item dequeue(); /** * @return true if the queue is empty; */ boolean isEmpty(); }
设计API的时候有一些微妙的地方,很多人有不同的设计规则和观念。我“可爱”的教授告诉我他的规则是:
1、不要添加任何不是必要的标志或者说明;
2、将可变和不可变方法放在不同的接口内;(这里比较不能理解,希望有好心人能够给出解释:split mutating and non-mutating methods into separate interfaces; )
下面对这三个接口进行实现:
1)bags:
import java.util.Arrays; import java.util.Iterator; import java.util.Objects; public class Bag_Array<Item> implements Bag<Item> { private Item[] items=null; private int count=0; public Bag_Array(){ grow((Item[])new Object[0],32); } public void add(Item item){ if(full()) grow(items,2*capacity()); items[count++]=item; } public boolean isEmpty(){ return count==0; } public int size(){ return count; } public Iterator<Item> iterator(){ return Arrays.asList(Arrays.copyOf(items,count)).iterator(); } private void grow(Item[] source,int size){ items=growFrom(source,size); } private int capacity(){ return items.length; } private boolean full(){ return size()==capacity(); } private static <T> T[] growFrom(T[] from,int size){ T[] result=(T[])new Objects[size]; System.arraycopy(from, 0, result, 0, from.length); return result; } }
这个bags的实现是教授给我的例子,但是对背包里面又很多调用的方法没有细说,我会在之后的文章里面对上述一些Array处理函数的功能和使用方法进行一定的解释。
2)stack
public class SeqStack<Item> implements Stack<Item> { private int capacity=10; private int top=-1; private int size=0; private Item[] item=null; public SeqStack(){ item=(Item[])new Object[capacity]; } public boolean isEmpty(){ return getsize()==0; } public int getsize(){ return size; } public boolean isFull(){ return getsize()==capacity; } public void push(Item item){ if(isFull()){ System.out.println("the stack is full"); return ; } this.item[++top]=item; size++; } public Item pop() throws Exception{ if(isEmpty()){ throw new Exception("the stack is empty"); } size--; return item[top--]; } public Item peek() { if(isEmpty()){ } return item[top]; } }
这是我自己设计的堆栈接口的实现,因为堆栈可以实现删除的方法,可能会对空栈进行删除操作,所以在pop()方法内抛出了一个异常。
注意:不要去catch这个异常!!!!
教授语录:不要去轻易的抓捕异常,除非你有一个完美的系统去解决它。将它抛给使用者去解决。(Do not catch exceptions unless u have some perfect solutions.)