通常称,栈和队列是限定插入和删除只能在表的“端点”进行的线性表。
栈stack是后进先出LIFO, insert操作通常称为push,而无参数的删除操作称为pop。最先插入的元素成为“栈底元素”,最后插入的称为“栈顶元素”。栈空条件? top==0
栈满条件? top==stackElem.length
栈的长度? top
栈顶元素? stackElem[top-1]
队列queue是先进先出FIFO,像栈操作POP一样,DEQUEUE也不需要取队列的元素作为参数。队列Q具有属性head指向队列的头元素,tail指向新元素即将被插入的地方。在最后一个位置要进行“卷绕”,即队列中的n个元素排成环形,位置1接在位置n之后。
//定义栈基本操作的接口
- package operation;
- public interface IStack {
- public void clear();
- public boolean isEmpty();
- public boolean push(Object x);//入栈操作
- public int length();
- public Object peek();//取栈顶元素
- public Object pop() throws Exception; //出战操作
- public Object min(); //取栈中的最小值,招聘中常考
- public void reverseStack(Stack s)throws Exception; //将栈中元素颠倒,招聘中常考
- }
//栈基本操作的实现
- package operation;
- /**
- * 本类针对顺序栈
- * */
- public class Stack implements IStack {//实现接口
- private int[] mIndex;
- private Object[] stackElem;
- public Object[] getStackElem() {
- return stackElem;
- }
- public int[] getmIndex() {
- return mIndex;
- }
- private int top;
- public Stack(int maxSize) {
- stackElem = new Object[maxSize];
- mIndex = new int[maxSize];
- top = 0;
- }
- public void clear() {
- // TODO Auto-generated method stub
- top = 0;
- }
- public boolean isEmpty() {
- // TODO Auto-generated method stub
- return top == 0;//当top为0,栈为空;
- }
- public int length() {
- // TODO Auto-generated method stub
- return top;
- }
- // 取栈顶元素的函数
- public Object peek() {
- // TODO Auto-generated method stub
- if (!isEmpty())
- top = top - 1;
- return stackElem[top+1];//top指向最新插入的元素,即栈顶。
- else
- return null;
- }
- /**
- * 进栈操作 要求:时间复杂度是O(1)
- * */
- public boolean push(Object x) {
- // TODO Auto-generated method stub
- if (top == stackElem.length) {
- System.out.print("栈已满");
- return false;
- }
- if (top == 0) {
- mIndex[top] = 0;
- } else if ((Integer)x <(Integer)peek()&& peek() != null) {
- mIndex[top] = top;
- } else if (peek() != null) {
- mIndex[top] = mIndex[top - 1];
- }
- stackElem[top++] = x;
- return true;
- }
- /**
- * 顺序栈的出栈操作 时间复杂度为:O(1)
- * */
- public Object pop() throws Exception {
- // TODO Auto-generated method stub
- if (!isEmpty()) {// 若栈不空,则移去栈顶元素并返回其值
- mIndex[top - 1] = -1;
- return stackElem[--top];
- } else
- return null;// 若栈空,则返回空值
- }
- /**
- * 返回栈中最小的元素,需要一个辅助栈 要求:时间复杂度是O(1)
- * 解决这道题的思路在于:用空间换时间!很关键,很直白,但并不是很个人都能很灵活的运用!比如我就不能!
- * 代码实现也很简单,定义一个结构体,结构体中两个等大的数组stackElem,mIndex;一个存储数据,另一个存储最小值的下标,
- * mIndex[i]表示[0,i]区间中最小值的下标。
- * */
- public Object min() {
- if (top == 0)
- return null;
- else
- return stackElem[mIndex[top - 1]];
- }
- @Override
- /**
- * 要求用递归的方法,在不浪费空间的情况下,颠倒栈。
- * */
- public void reverseStack(Stack s) throws Exception {
- if (!s.isEmpty()) {
- Object t = s.pop();
- reverseStack(s);
- pushStackButtom(s, t);
- }
- }
- public void pushStackButtom(Stack s, Object t) throws Exception {
- if (s.isEmpty()) {
- s.push(t);
- } else {
- Object top = s.pop();
- pushStackButtom(s, t);
- s.push(top);
- }
- }
- }
--------------------------------------------------------------------------------------------------------------
队列//队列基本操作的接口
- package operation;
- public interface IQueue {
- public void clear();
- public boolean isEmpty();
- public int length();
- public Object peek(); //取栈顶元素
- public void enQueue(Object x)throws Exception;//入队操作
- public Object deQueue();//出队操作
- }
//第一种:顺序队列的基本操作的实现
- package operation;
- /**
- * 本类针对顺序队列
- * */
- public class Queue implements IQueue {
- private Object[] q;
- private int head, tail;
- public Queue(int maxSize) {
- q = new Object[maxSize];
- head =0;
- tail = 0;
- }
- public Object[] getQ() {
- return q;
- }
- @Override
- public void clear() {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean isEmpty() {
- return false;
- }
- @Override
- public int length() {
- return tail;
- }
- @Override
- public Object peek() {
- if (!isEmpty())
- return q[tail - 1];
- return null;
- }
- @Override
- public void enQueue(Object x) throws Exception {
- if (tail == q.length)
- throw new Exception("队列已满");
- q[tail++] = x;
- }
- @Override
- public Object deQueue() {
- if (!isEmpty())
- return q[head++];
- else
- return null;
- }
- }
//第二种:循环队列(即环状的)的基本操作的实现
- package operation;
- public class CircleQueue implements IQueue{
- private int flag;//用来判断队列是否满
- private int head,tail;
- private Object[] q;
- public Object[] getQ() {
- return q;
- }
- public CircleQueue(int maxSize){
- q=new Object[maxSize];
- head=tail=0;
- flag=0;
- }
- @Override
- public void clear() {
- // TODO Auto-generated method stub
- head=tail= 0;
- }
- @Override
- public boolean isEmpty() {
- // TODO Auto-generated method stub
- if(head==tail&&flag==0)
- return true;
- else
- return false;
- }
- @Override
- public int length() {
- // TODO Auto-generated method stub
- return
- tail-head;
- }
- @Override
- public Object peek() {
- // TODO Auto-generated method stub
- if(!isEmpty())
- return q[tail];
- return null;
- }
- @Override
- public void enQueue(Object x) throws Exception {
- // TODO Auto-generated method stub
- if(head==tail&&flag==1)//判断栈是否满了
- throw new Exception("栈已满");
- else
- q[tail++]=x;
- }
- @Override
- public Object deQueue() {
- // TODO Auto-generated method stub
- if(!isEmpty())
- return q[--tail];
- return null;
- }
- }