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 |