用数组结构实现大小固定的队列和栈

1、用数组结构实现大小固定的栈

/*
用数组结构实现大小固定的栈
 */
public class exercise1{
    //用数组实现大小固定的栈
    public  static class ArrayStack
    {
        private  Integer[] mystack;
        //index始终指向栈顶的元素
        private  Integer   index=-1;
        public ArrayStack(int initsize) {
            if (initsize<0){
                throw new IllegalArgumentException("栈的长度必须大于0!");
            }
            mystack=new Integer[initsize];
        }
        //返回栈顶元素
        public Integer peak(){
            if (index==-1){
                System.out.println("栈为空,无法读取栈顶元素!!!");
                return null;
            }
            else return mystack[index];
        }
        //栈中加元素
        public void push(Integer obj){
            if(index==mystack.length-1) {
    throw new ArrayIndexOutOfBoundsException("数组已经满了,添加失败!");
            }
            mystack[++index]=obj;
        }
        //栈中弹出元素
        public Integer pop(){
            if (index==-1){
            throw new ArrayIndexOutOfBoundsException("数组为空,读取失败!");
            }
            else return mystack[index--];
        }

        public static void main(String[] args) {
            ArrayStack a=new ArrayStack(5);
            a.push(1);
            a.push(3);
            a.push(9);
            a.push(91);
            System.out.println("压栈操作");
            System.out.println(Arrays.toString(a.mystack));
           System.out.println("peak操作");
           System.out.println(a.peak());
          System.out.println("出栈操作");
            System.out.println(a.pop());
            System.out.println(a.pop());
            System.out.println(a.pop());
        }
    }

2、用数组实现大小固定的队列

//---------------------------------------------------------------
    /*
    固定数组实现队列
    start和end最初都指向myQueue[0]
    每加上一个数end++;
    每减一个数start++;
    size来判断数组是队列是否犯了
    当start和end走到数组的结尾是,在重新指向myQueue[0]
     */
        //用数组实现大小固定的栈=队列
    public static class Queue{
        private Integer[]   myQueue;
        private Integer     size;//size判断是否为空
        private Integer     start;
        private Integer     end;
        public Queue(int initialize){
            if (initialize<0){
                throw new IllegalArgumentException("队列大小必须大于0!");
            }
                myQueue=new Integer[initialize];
                size=0;//
                start=0;
                end  =0;

        }
        //返回栈顶元素
        public Integer peak(){
            if (size==0){
                throw new ArrayIndexOutOfBoundsException("队列为空!");
            }
            return myQueue[start];
        }
        public void push(Integer i){
            if (size==myQueue.length) {
                throw new ArrayIndexOutOfBoundsException("队列满了!!!");
            }
            size++;
            myQueue[end]=i;
            end=end==myQueue.length-1?0:end+1;


        }
        public Integer pop(){
            if (size==0){
                throw new ArrayIndexOutOfBoundsException("队列空了!!!");
            }
            size--;
            int tmp=myQueue[start];
            start=start==myQueue.length-1?0:start+1;
            return tmp;
        }

        public void printQueue(){

            int n=start;
            int count=0;
            System.out.printf("[ ");
            while (count<size){
                System.out.print(" ");
                System.out.print(myQueue[n]);
                n=n==myQueue.length-1?0:n+1;
                count++;
            }
            System.out.printf(" ]");
            System.out.println();
        }

        public static void main(String[] args) {
            Queue q=new Queue(4);
            q.push(3);
            q.push(4);
            q.push(5);
            q.push(6);

            q.printQueue();
            q.pop();
            q.pop();
            q.push(1);
            q.push(2);
            q.printQueue();
        }
    }


3、构建一个人特殊的栈在实现栈的基础功能上,再返回一个栈中最小元素的操作。

要求:①push()、pop()、getMin()操作的时间复杂度都为O(1)。
   ②设计的栈类型,可以使用现成的栈结构。
思路:在构建的栈内部放两个栈,data栈和min栈;
  data栈内正常压入数,min栈则会比较当前数和自己栈顶的数字的大小,将较小的数下入栈顶。当要取得栈内数字最小值是只需要将栈顶的元素弹出来即可。

public class e2_stackMin {
    private Stack<Integer> data;
    private Stack<Integer> min;
    public e2_stackMin(){
        this.data=new Stack<Integer>();
        this.min =new Stack<Integer>();
    }
    public void push(Integer newnum){
        //min栈的更新
        if (this.min.isEmpty()||newnum<this.min.peek()){
            this.min.push(newnum);
        } else {
            int tmp=min.peek();
            this.min.push(tmp);
        }
        //data栈的更新
        this.data.push(newnum);
    }
    public  Integer pop(){
        if (this.data.isEmpty()){
      throw new ArrayIndexOutOfBoundsException("栈内元素为0,无法出栈!");
        }
        this.min.pop();
        return this.data.pop();
    }
    public Integer getMin() {
        if (min.isEmpty()){
     throw new ArrayIndexOutOfBoundsException("栈为空,无法找最小元素!");
        }
        return  min.peek();
    }
    public static void main(String[] args) {
        e2_stackMin s=new e2_stackMin();
                    s.push(10);
                    s.push(3);
                    s.push(9);
                    s.push(100);
                    s.push(16);
                    s.push(1);
                    s.push(16);
        System.out.println(s.data);
        System.out.println(s.min);
        System.out.println(s.min.peek());
                    s.pop();
                    s.pop();
        System.out.println(s.data);
        System.out.println(s.min);
        System.out.println(s.min.peek());
        }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值