zoukankan      html  css  js  c++  java
  • 栈,队列,优先级队列

    一.栈

    1.栈:先进后出,后进先出,每次访问一个数据项,即最后一个添加的数据项(每次添加的数据放到最后)。数据入栈和出栈的时间复杂度O(1),栈不需要移动和比较数据。

    2.代码

     1 public class Stack {
     2     private int maxsize;
     3     private int[] arr;
     4     private int top;
     5     
     6     public Stack(int s){
     7         maxsize = s;
     8         arr = new int[maxsize];
     9         top = -1;
    10     }
    11     
    12     public void push(int s){
    13         arr[++top] = s;
    14     }
    15     
    16     public int delete(){
    17         return arr[top--];
    18     }
    19     
    20     public boolean isEmpty(){
    21         return top == -1;
    22     }
    23     
    24     public boolean isFull(){
    25         return top == maxsize-1;
    26     }
    27     
    28     public static void main(String[] args) {
    29         Stack s = new Stack(10);
    30         s.push(5);
    31         s.push(10);
    32         s.push(2);
    33         s.push(4);
    34         while(!s.isEmpty()){
    35             int a = s.delete();
    36             System.out.println(a);
    37         }
    38     }
    39 
    40 }

    二.队列

    1.队列:先进先出,每次操作一个数据项,先进去的。数据放到最后,从最前面开始拿,所以需要两个标记,队头和队尾。

        为了解决队列不满,又不能添加数据的问题,采用环绕式处理。

    2.代码:

     1 public class Stack1 {
     2     private int maxsize;
     3     private int[] arr;
     4     private int first;
     5     private int last;
     6     private int num;
     7 
     8     public Stack1(int s) {
     9         maxsize = s;
    10         arr = new int[maxsize];
    11         first = -1;
    12         last = 0;
    13         num = 0;
    14     }
    15 
    16     public void insert(int a) {
    17         if (last == maxsize - 1 && num != maxsize) {
    18             last = 0;
    19         }
    20         arr[last++] = a;
    21         num++;
    22     }
    23 
    24     public int delete() {
    25         num--;
    26         return arr[++first];
    27 
    28     }
    29 
    30     public boolean isEmpty() {
    31         return num == 0;
    32     }
    33 
    34     public boolean isFull() {
    35         return maxsize == num;
    36     }
    37 
    38     public static void main(String[] args) {
    39         Stack1 s = new Stack1(5);
    40         s.insert(10);
    41         s.insert(5);
    42         s.insert(9);
    43         s.insert(80);
    44         while (!s.isEmpty()) {
    45             int b = s.delete();
    46             System.out.println(b);
    47         }
    48 
    49     }

    三.优先级队列

    1.优先级队列:跟普通队列一样,优先级队列也有队头和队尾,但是,优先级队列中,按关键字的值进行排序,数据项插入的时候也按照顺序插入确保队列的顺序。

    2.代码

     1 public class Stack2 {
     2     private int maxsize;
     3     private int arr[];
     4     private int nItems;
     5 
     6     public Stack2(int a) {
     7         maxsize = a;
     8         arr = new int[maxsize];
     9         nItems = 0;
    10     }
    11 
    12     public void insert(int b) {
    13         int j;
    14         if (nItems == 0)
    15             arr[nItems] = b;
    16         else {
    17             for (j = nItems - 1; j >= 0; j--) {
    18                 if (arr[j] > b)
    19                     arr[j + 1] = arr[j];
    20                 else
    21                     break;
    22             }
    23             arr[j+1] = b;            
    24         }
    25         nItems++;
    26     }
    27     
    28     public int remove(){
    29         return arr[--nItems];
    30     }
    31     
    32     public boolean isEmpty(){
    33         return nItems==0;
    34     }
    35     
    36     public boolean isFull(){
    37         return maxsize == nItems;
    38     }
    39 
    40     public static void main(String[] args) {
    41         Stack2 s = new Stack2(5);
    42         s.insert(10);
    43         s.insert(5);
    44         s.insert(80);
    45         s.insert(2);
    46         while(!s.isEmpty()){
    47             int a = s.remove();
    48             System.out.println(a);
    49         }
    50     }
    51 }
  • 相关阅读:
    怎样解决:未找到路径“……”的控制器或该控制器未实现 IController?
    错误:org.springframework.jdbc.support.SQLErrorCodesFactory
    springbean的生命周期
    注解到处excel
    nio读取文件,输出文件
    AtomicReference
    唯一id
    hashmap1.7的死锁模拟
    数组模拟stack
    环形队列
  • 原文地址:https://www.cnblogs.com/xwzp/p/7514555.html
Copyright © 2011-2022 走看看