基础数据结构之栈和队列

前言

我们生活中会遇见很多的看起来很棘手的问题,比如一堆尚未处理的邮件,正要去电影院排队购买一张电影票(两张也可以,你值得拥有...),这些我们用自己熟悉的方式来处理他们,而我们自己熟悉的这些方式,很可能就和栈和队列有关.今天,我们就来谈谈栈和队列.

栈的定义

栈是限定仅在表尾进行插入和删除操作的线性表,也称为LIFO(后入先出)结构.

我们把允许插入和删除的地方称之为栈顶,另一端称之为栈底,,不包含任何元素称之为空栈

栈可以用顺序结构和链式结构表示,本例为了方便描述问题,用顺序结构.

栈的插入操作我们称之为入栈(push),栈的删除操作我们称之为出栈(pop)

顺序栈的Java代码实现

5.public class Statck<E extends Object> {  
6.    private List<E> pool = new ArrayList<E>();  
7.  
8.    public Statck() {  
9.    }  
10.  
11.    public void clear() {  
12.        pool.clear();  
13.    }  
14.  
15.    public boolean isEmpty() {  
16.        return pool.isEmpty();  
17.    }  
18.  
19.    /** 
20.     * 获取栈顶元素 
21.     * */  
22.    public E getTopObjcet() {  
23.        if (isEmpty()) {return null;}  
24.        return pool.get(0);  
25.    }  
26.  
27.    /** 
28.     * 弹出栈操作 
29.     * */  
30.    public E pop() {  
31.        if (isEmpty()) {throw new EmptyStackException();}  
32.        return pool.remove(pool.size() - 1);  
33.    }  
34.  
35.    /** 
36.     * 压入栈 
37.     * */  
38.    public void push(E e) {  
39.        if (isEmpty()) {throw new EmptyStackException();}  
40.        pool.add(e);  
41.    }  
42.  
43.    /** 
44.     * 获取当前栈大小 
45.     * */  
46.    public int getStatckSize() {  
47.        if (isEmpty()) {throw new EmptyStackException();}  
48.        return pool.size();  
49.    }  
50.  
51.}  

队列的定义

 队列是只允许在一端插入,在另一端删除的线性表

允许插入的那一端称之为队尾,允许删除的那一端称之为队头,是FIFO(先入先出)结构

队列有顺序结构和链式结构,一般来说,顺序结构删除操作是o(N)的性能,而链式结构只有o(1);

顺序队列的Java代码实现

 

class Queue   //队列类
    {
    private int maxSize; //队列长度,由构造函数初始化
    private long[] queArray; // 队列
    private int front; //队头
    private int rear; //队尾
    private int nItems;  //元素的个数

    public Queue(int s)           // 构造函数
       {
       maxSize = s;
       queArray = new long[maxSize];
       front = 0;
       rear = -1;
       nItems = 0;
       }

    public void insert(long j)    // 进队列
       {
       if(rear == maxSize-1)          // 处理循环
          rear = -1;
       queArray[++rear] = j;          // 队尾指针加1,把值j加入队尾
       nItems++;                    
       }

    public long remove()          // 取得队列的队头元素。
       {
       long temp = queArray[front++]; // 取值和修改队头指针
       if(front == maxSize)            // 处理循环
          front = 0;
       nItems--;                      
       return temp;
       }

    public long peekFront()       // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。
       {
       return queArray[front];
       }

    public boolean isEmpty()     // 判队列是否为空。若为空返回一个真值,否则返回一个假值。
       {
       return (nItems==0);
       }

    public boolean isFull()      // 判队列是否已满。若已满返回一个真值,否则返回一个假值。
       {
       return (nItems==maxSize);
       }
    public int size()            // 返回队列的长度
       {
       return nItems;
       }

    }  


public class IntegerQueue
{
public static void main(String[] args)
   {
 Queue theQueue = new Queue(5);   // 队列有5个元素

   theQueue.insert(10);             // 添加4个元素
   theQueue.insert(20);
   theQueue.insert(30);
  

   theQueue.remove();               // 移除3个元素

   theQueue.insert(60);           
   theQueue.insert(70);
   theQueue.insert(80);
   

   while( !theQueue.isEmpty() )     // 遍历队列并移除所有元素
      {                            
      long n = theQueue.remove();   // (40, 50, 60, 70, 80)
      System.out.print(n);
      System.out.print(" ");
      }
   System.out.println("");
   }  
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值