数组实现栈和队列

数组实现栈和队列

1.栈和队列

栈(stack)是一个后进先出(Last in first out,LIFO)的线性表,它要求只在表尾进行删除和插入操作。
a.栈的元素必须后进先出
b.栈的操作只能在这个线性表的表尾进行
c.对于栈来说,这个表尾成为栈的栈顶(top),相应的表头称为栈底(bottom)

1.1使用数组实现栈的流程

首先定义一个变量index和固定大小为size的数组arr,当有数据进入时就压入栈中,同时index++(index指示着进入的数据放入栈中的位置)如果index==size,栈已满;当需要弹出数据时,index–,同时要满足index>0.

public static class ArrayStack {
		private Integer[] arr;
		private Integer index;

		public ArrayStack(int initSize) {
			if (initSize < 0) {
				throw new IllegalArgumentException("The init size is less than 0");
			}
			arr = new Integer[initSize];
			index= 0;
		}

		public Integer peek() {
			if (index == 0) {
				return null;
			}
			return arr[size - 1];
		}

		public void push(int obj) {
			if (index== arr.length) {
				throw new ArrayIndexOutOfBoundsException("The queue is full");
			}
			arr[index++] = obj;
		}

		public Integer pop() {
			if (index== 0) {
				throw new ArrayIndexOutOfBoundsException("The queue is empty");
			}
			return arr[--index];
		}
	}

1.2使用数组实现队列的流程

首先定义两个指针start 和 end指针,初始化指向index=0的位置,如果有数据进入队列,把新来的数据放到end的位置上,之后end++,size++;start指向队列需要弹出的数据的位置上,如果size>0,总把start位置指向的数弹出。当start==end 且队列中有元素或 size==3 队列已满
在这里插入图片描述

在这里插入图片描述

public static class ArrayQueue {
		private Integer[] arr;
		private Integer size;
		private Integer first;
		private Integer last;

		public ArrayQueue(int initSize) {
			if (initSize < 0) {
				throw new IllegalArgumentException("The init size is less than 0");
			}
			arr = new Integer[initSize];
			size = 0;
			first = 0;
			last = 0;
		}

		public Integer peek() {
			if (size == 0) {
				return null;
			}
			return arr[first];
		}

		public void push(int obj) {
			if (size == arr.length) {
				throw new ArrayIndexOutOfBoundsException("The queue is full");
			}
			size++;
			arr[last] = obj;
			last = last == arr.length - 1 ? 0 : last + 1;
		}

		public Integer poll() {
			if (size == 0) {
				throw new ArrayIndexOutOfBoundsException("The queue is empty");
			}
			size--;
			int tmp = first;
			first = first == arr.length - 1 ? 0 : first + 1;
			return arr[tmp];
		}
	}

1.3经典题
题目描述:实现一个特殊栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
首先构建两个栈,Data栈与min栈。开始时,有数据要压入Data栈,min栈此时为空,也压入Data栈中的数据;接着当data栈再次压入数据时,将压入的数据与min栈的栈顶数据进行比较。如果压入的数据比min栈顶的元素大,则min栈重复压入min栈顶元素,否则压入Data栈顶的元素。图示如下:
在这里插入图片描述

public static class MyStack2 {
		private Stack<Integer> stackData;
		private Stack<Integer> stackMin;

		public MyStack2() {
			this.stackData = new Stack<Integer>();
			this.stackMin = new Stack<Integer>();
		}

		public void push(int newNum) {
			if (this.stackMin.isEmpty()) {
				this.stackMin.push(newNum);
			} else if (newNum < this.getmin()) {
				this.stackMin.push(newNum);
			} else {
				int newMin = this.stackMin.peek();
				this.stackMin.push(newMin);
			}
			this.stackData.push(newNum);
		}

		public int pop() {
			if (this.stackData.isEmpty()) {
				throw new RuntimeException("Your stack is empty.");
			}
			this.stackMin.pop();
			return this.stackData.pop();
		}

		public int getmin() {
			if (this.stackMin.isEmpty()) {
				throw new RuntimeException("Your stack is empty.");
			}
			return this.stackMin.peek();
		}
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值