伊人如斯君如昔吧 关注:2贴子:113
  • 6回复贴,共1

JAVA 先马了再说

只看楼主收藏回复



来自Android客户端1楼2015-07-22 23:44回复
    栈和队列:
    一般是作为程序员的工具,用于辅助构思算法,生命周期较短,运行时才被创建
    访问受限,在特定时刻,只有一个数据可被读取或删除
    是一种抽象的结构,内部的实现机制,对用户不可见,比如用数组、链表来实现栈
    栈:
    同时,只允许一个数据被访问,后进先出
    对于入栈和出栈的时间复杂度都为O(1),即不依赖栈内数据项的个数,操作比较快
    例,使用数组作为栈的存储结构
    [java] view plaincopyprint?
    public class StackS<T> {
    private int max;
    private T[] ary;
    private int top; //指针,指向栈顶元素的下标
    public StackS(int size) {
    this.max = size;
    ary = (T[]) new Object[max];
    top = -1;
    }
    // 入栈
    public void push(T data) {
    if (!isFull())
    ary[++top] = data;
    }
    // 出栈
    public T pop() {
    if (isEmpty()) {
    return null;
    }
    return ary[top--];
    }
    // 查看栈顶
    public T peek() {
    return ary[top];
    }
    //栈是否为空
    public boolean isEmpty() {
    return top == -1;
    }
    //栈是否满
    public boolean isFull() {
    return top == max - 1;
    }


    来自Android客户端2楼2015-07-22 23:44
    回复
      //size
      public int size() {
      return top + 1;
      }
      public static void main(String[] args) {
      StackS<Integer> stack = new StackS<Integer>(3);
      for (int i = 0; i < 5; i++) {
      stack.push(i);
      System.out.println("size:" + stack.size());
      }
      for (int i = 0; i < 5; i++) {
      Integer peek = stack.peek();
      System.out.println("peek:" + peek);
      System.out.println("size:" + stack.size());
      }
      for (int i = 0; i < 5; i++) {
      Integer pop = stack.pop();
      System.out.println("pop:" + pop);
      System.out.println("size:" + stack.size());
      }
      System.out.println("----");
      for (int i = 5; i > 0; i--) {
      stack.push(i);
      System.out.println("size:" + stack.size());
      }
      for (int i = 5; i > 0; i--) {
      Integer peek = stack.peek()


      来自Android客户端3楼2015-07-22 23:46
      回复
        for (int i = 5; i > 0; i--) {
        stack.push(i);
        System.out.println("size:" + stack.size());
        }
        for (int i = 5; i > 0; i--) {
        Integer peek = stack.peek();
        System.out.println("peek:" + peek);
        System.out.println("size:" + stack.size());
        }
        for (int i = 5; i > 0; i--) {
        Integer pop = stack.pop();
        System.out.println("pop:" + pop);
        System.out.println("size:" + stack.size());
        }
        }
        }


        来自Android客户端4楼2015-07-22 23:46
        回复
          上面的例子,有一个maxSize的规定,因为数组是要规定大小的,若想无限制,可以使用其他结构来做存储,当然也可以new一个新的长度的数组。
          例,使用LinkedList存储来实现栈
          [java] view plaincopyprint?
          /**
          * 使用LinkedList存储来实现栈
          * @author stone
          *
          * @param <T>
          */
          public class StackSS<T> {
          private LinkedList<T> datas;
          public StackSS() {
          datas = new LinkedList<T>();
          }
          // 入栈
          public void push(T data) {
          datas.addLast(data);
          }
          // 出栈
          public T pop() {
          return datas.removeLast();
          }


          来自Android客户端5楼2015-07-22 23:47
          回复
            // 查看栈顶
            public T peek() {
            return datas.getLast();
            }
            //栈是否为空
            public boolean isEmpty() {
            return datas.isEmpty();
            }
            //size
            public int size() {
            return datas.size();
            }
            public static void main(String[] args) {
            StackS<Integer> stack = new StackS<Integer>(3);
            for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println("size:" + stack.size());
            }
            for (int i = 0; i < 5; i++) {
            Integer peek = stack.peek();
            System.out.println("peek:" + peek);
            System.out.println("size:" + stack.size());
            }
            for (int i = 0; i < 5; i++) {
            Integer pop = stack.pop();
            System.out.println("pop:" + pop);
            System.out.println("size:" + stack.size());
            }


            来自Android客户端6楼2015-07-22 23:47
            回复
              System.out.println("----");
              for (int i = 5; i > 0; i--) {
              stack.push(i);
              System.out.println("size:" + stack.size());
              }
              for (int i = 5; i > 0; i--) {
              Integer peek = stack.peek();
              System.out.println("peek:" + peek);
              System.out.println("size:" + stack.size());
              }
              for (int i = 5; i > 0; i--) {
              Integer pop = stack.pop();
              System.out.println("pop:" + pop);
              System.out.println("size:" + stack.size());
              }
              }
              }


              来自Android客户端7楼2015-07-22 23:47
              回复