栈与队列

本文介绍了基于数组实现的栈和队列数据结构。栈遵循后进先出原则,而队列则遵循先进先出原则。文章提供了详细的Java代码示例,包括栈的基本操作如压栈、弹栈及队列的入队、出队等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

Java代码   收藏代码
  1.   

       今天我要来说说常用的两种数据结构:栈与队列。何为栈?简而言之,它具有“先进后出、后进先出”的特征。何为队列?就像我们日常生活中的排队,具有“先到先服务、先进先出”的特征。

一、基于数组的栈。

Java代码   收藏代码
  1. public class ArrayStack {  
  2.     private Object[] theArray;  
  3.     private int topOfStack;  
  4.     private static final int DEFAULT_CAPACITY=10;  
  5.     //初始化实例变量  
  6.     public ArrayStack(){  
  7.         theArray=new Object[DEFAULT_CAPACITY];  
  8.         topOfStack=-1;  
  9.     }  
  10.     //判断是否为空栈  
  11.     public boolean isEmpty(){  
  12.         return topOfStack==-1;  
  13.     }  
  14.     //入栈  
  15.     public void push(Object x){  
  16.         //如果栈的容量不够,则扩展栈的容量  
  17.         if (topOfStack+1==theArray.length) {  
  18.             doubleArray();  
  19.         }  
  20.         theArray[++topOfStack]=x;  
  21.     }  
  22.     //出栈  
  23.     public Object pop() throws Exception{  
  24.         //如果栈为空,则抛出未找到异常  
  25.         if (isEmpty()) {  
  26.             throw new Exception("UnderFlowExcption");   
  27.         }  
  28.         return theArray[topOfStack--];  
  29.     }  
  30.     //查看栈顶元素  
  31.     public Object top() throws Exception{  
  32.         if (isEmpty()) {  
  33.             throw new Exception("UnderFlowExcption");   
  34.         }  
  35.         return theArray[topOfStack];  
  36.     }  
  37.     //容量扩展的实现方式  
  38.     private void doubleArray() {  
  39.         Object[] newArray=new Object[theArray.length*2+1];  
  40.         for (int i = 0; i < theArray.length; i++) {  
  41.             newArray[i]=theArray[i];  
  42.         }  
  43.         theArray=newArray;  
  44.     }  
  45. }  

 二、基于数组的队列。


影音先锋电影 http://www.iskdy.com/

Java代码   收藏代码
  1. public class ArrayQueue {  
  2.     private Object[] theArray;  
  3.     //用来记录取值的位置  
  4.     private int front;  
  5.     //用来记录添加值的位置  
  6.     private int back;  
  7.     //存放当前数组已有多少个对象  
  8.     private int currentSize;  
  9.       
  10.     private static final int DEFAULT_CAPACITY=10;  
  11.     //初始化实例变量  
  12.     public ArrayQueue() {  
  13.         theArray=new Object[DEFAULT_CAPACITY];  
  14.         front=0;  
  15.         back=-1;  
  16.         currentSize=0;  
  17.     }  
  18.     //判断队列是否为空  
  19.     public boolean isEmpty(){  
  20.         return currentSize==0;  
  21.     }  
  22.     //入队  
  23.     public void enqueue(Object x){  
  24.         //如果当前已有对象的记录数等于数组长度,则扩容  
  25.         if (currentSize==theArray.length) {  
  26.             doubleArray();  
  27.         }  
  28.         //增加对象之后一定要记得改变实例变量的值  
  29.         back=increment(back);  
  30.         theArray[back]=x;  
  31.         currentSize++;  
  32.     }  
  33.     //删除队列中的对象,即出队  
  34.     public Object delQueue(){  
  35.         //判断队列是否空之后,再操作  
  36.         if (isEmpty()) {  
  37.             try {  
  38.                 throw new Exception("underFlowException");  
  39.             } catch (Exception e) {  
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.         currentSize--;  
  44.         Object returnValue=theArray[front];  
  45.         front=increment(front);  
  46.         return returnValue;  
  47.     }  
  48.     //获取队列中的第一个对象  
  49.     public Object getFront(){  
  50.         if (isEmpty()) {  
  51.             try {  
  52.                 throw new Exception("underFlowException");  
  53.             } catch (Exception e) {  
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.         return theArray[front];  
  58.     }  
  59.     //返回的值为参数+1或者为0  
  60.     private int increment(int x) {  
  61.         if (++x==theArray.length) {  
  62.             x=0;  
  63.         }  
  64.         return x;  
  65.     }  
  66.     //扩容的方法  
  67.     private void doubleArray() {  
  68.         Object[] newArray=new Object[theArray.length*2+1];  
  69.         for (int i = 0; i < theArray.length; i++,front=increment(front)) {  
  70.             newArray[i]=theArray[front];  
  71.         }  
  72.         front=0;  
  73.         theArray=newArray;  
  74.         back=currentSize-1;  
  75.     }  
  76. }  

    

伦理片 http://www.dotdy.com/

   我也是个菜鸟,说得不明白的地方,还请谅解与补充!今天就暂时到这里,明天再来说说,用链表实现栈与队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值