public interface StackADT<T> {//public 保留字 泛型參數 /** * 彈出棧頂 * @return */ public T pop(); public void push(T element); /** * 獲得棧頂,但不彈出 * @return */ public T peek(); public boolean isEmpty(); /** * 獲得棧的大小 * @return */ public int Size(); /** * 打印出棧 * @return */ @Override public String toString(); }
public class Stack<T> implements StackADT<T>{ /** * 1. 數組的大小 * 2. 定義一個數組 * 3. 肯定棧頂位置 */ private int size=100; private T[] stack; private int top; public Stack(){//構造函數 top = 0 ; stack = (T[]) (new Object[size]); } public Stack(int a){//方法重載 top = 0 ; stack = (T[]) (new Object[a]); } }
@Override public void push(T element) { if (Size() == stack.length){ expandCapacity(); } stack[top] = element; top++; }
@Override public T pop() throws EmptyCollectionException{ if (isEmpty()) { throw new EmptyCollectionException("Stack"); } top--; T result = stack[top]; stack[top]=null; return result; }
@Override public T peek() throws EmptyCollectionException{ if (isEmpty()){ throw new EmptyCollectionException("Stack"); } return stack[top-1]; }
@Override public String toString(){ String result = ""; for(int i =0;i<Size();i++) { result += " " + stack[i]; } return result; }
public class EmptyCollectionException extends RuntimeException { public EmptyCollectionException(String collection){ super("The " + collection + " is empty. "); } }
public class Person { private String name; private String address; private Person next; //到另外一個Person的連接 //其餘內容 }
Person current = first; for ( int i = 0; i<3; i++){ current = current.next; }//訪問到第四個元素 String s = current.name//獲得屬性name
public static void InsertNode(Student Head, Student node){//當.next爲null時,插入node Student node1 = Head; if (node1==null){ Head=node;} else { while(node1.next!= null){ node1=node1.next; } node1.next = node; } } public static void InsertNode(Student Head,Student node1,Student node2,Student node3){//將node2插入在node1與node3之間(方法重載) Student node0 =Head; while(node0.number != node1.number && node0!= null){ node0 = node0.next; } if (node0.number == node1.number){ node0.next = node2; node2.next = node3;} else{ System.out.println("沒有找到:"+node1.number+"!"); } }
public static void DeleteNode(Student Head, int number){//刪除學號爲number 的人 Student node = Head; if (node!=null&&node.number!=number){ while(node.next.number!=number){ node = node.next; } node.next =node.next.next; } else { if (node ==null){ System.out.println("鏈表中沒有學號爲:"+number+"的這我的。"); } else { node.next = node.next.next; } } }
public class LindedStack<T> implements StackADT<T> { private int count;//用於計算棧中元素的個數 private LinearNode<T> top;//指向頂棧的指針 public LindedStack(){ count = 0 ; top = null; } @Override public T pop() { if (isEmpty()) throw new EmptyCollectionException("Stack"); T result = top.getElement(); top = top.getNext(); count--; return result; } @Override public void push(T element) { LinearNode<T> temp = new LinearNode<T>(element); temp.setNext(top); top = temp; count++; } @Override public T peek() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("Stack");} T result = top.getElement(); return result; } @Override public boolean isEmpty() { if (count == 0) return true; else return false; } @Override public int Size() { return count; } public String toString(){ String result=""; for (int i =0;i<count;i++){ System.out.println(top.getElement()); top = top.getNext(); } return result; } }
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 075/200 | 1/1 | 05/20 | |
第二週 | 560/500 | 1/2 | 13/38 |