(第4讲)栈和队列的数组实现

来源:互联网 发布:linux查看内存cpu命令 编辑:程序博客网 时间:2024/05/08 15:32

 栈队列优先队列插入O(1)O(1)O(N)移除O(1)O(1)O(1)描述数组。链表数组。链表数组、堆特点后进先出先进先出

先进先出


1、栈(数组实现的顺序栈)

栈是一种特殊的线性表,与线性表的差别在于:线性表的插入和删除操作可以再表中的任意位置上进行;而栈只能在栈顶进行插入和删除操作(即在表的尾部进行)。

还有一个问题要注意:top的定义的问题(栈顶的初始值的问题):一般有两种定义方式:

1、一种是将其设置为指向栈顶元素存储位置的下一个存储单元的位置,则

空栈的时候:   top=0;              满栈的时候:    top=array.length,

进栈的时候: array[top++];   出栈的时候:  array[ - - top];

2、另一种是将其设置为指向栈顶元素的存储单元的位置,则

空栈的时候:   top= - 1;              满栈的时候:    top=array.length -1,

进栈的时候: array[++top];   出栈的时候:  array[ top- -];


package com.four;

public class myStack {

    public static void main(String[] args) {
        int max = 5;
        stack1 stack = new stack1(max);
        stack.push(4);
        stack.push(45);
        stack.push(34);
        stack.push(14);
        stack.push(23);
        stack.push(89);
        stack.pop();
        System.out.println(stack.peck());
        stackObject s = new stackObject(max);
        s.push('a');s.push('b');s.push('c');s.push('d');s.push('e');s.push('f');
        s.display();
        System.out.println("栈的长度时"+s.length());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        s.display();
        System.out.println("栈的长度时"+s.length());
        s.push('a');s.push('b'); s.push('f');
        s.display();
        System.out.println("栈的长度时"+s.length());
        s.clear();
        System.out.println("栈的长度时"+s.length());

    }

}
//任意类型的栈
class stackObject{
    private Object[] array;
    private int length;
    private int top;//栈顶
    public stackObject(int max){
        length = max;
        array = new Object[length];
        top=-1;//采用栈顶指向栈顶元素的存储位置
    }
    //栈是否为空
    public boolean isEmpty(){
        return (top==-1);
    }
    //栈是否满
    public boolean isFull(){
        return (top==length-1);
    }
    //进栈
    public void push(Object value){
        if(!isFull()){
            array[++top] = value;
        }else{
            System.out.println("栈满了");
        }
    }
    //出栈
    public Object pop(){
        if(!isEmpty()){
            return array[top--];
        }else{
            return null;
        }
    }
    //看栈顶元素
    public Object peck(){
        return array[top];
    }
    //将栈置空
    public void clear(){
        top = -1;
    }
    //看栈的长度
    public int length(){
        return top+1;
    }
    //查看栈中所有元素
    public void display(){
        //for(int i=top;i>=0;i--)//(从栈顶元素到栈尾元素)
        for(int i=0;i<=top;i++)//(从栈尾元素到栈顶元素:即正常输入的顺序)
        {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}
class stack1{
    private static int[] a;
    private static int size;
    private static int top;
    public stack1(int max){
        size = max;
        a = new int[size];
        top=-1;
    }
    //进栈
    public void push(int value){
         //如果栈没满
        if(!stack1.isFull()){
        a[++top] = value;
        System.out.println(value+"进栈成功");
        } else{    
            System.out.println("栈满了");
        }
    }
    //出栈
    public void pop(){
        if(!stack1.isEmpty()){
            System.out.println(a[top--]);System.out.println( "出栈成功");
        } else{
            System.out.println("栈里没有数据");
        }
        //如果栈不是空的,弹出
        /*if(!stack1.isEmpty()){
            return a[top--];
        } else{
            return 0;
        }*///如果用if else 就必须都写return才不会报错,但是这样就不对了
        
    }
    //查看栈顶
    public int peck(){
        return a[top];
    }
    //栈是否为空
    public static boolean isEmpty(){
        return (top==-1);
    }
    //栈是否满
    public static boolean isFull(){
        return (top==size-1);
    }
}

结果是:4进栈成功
45进栈成功
34进栈成功
14进栈成功
23进栈成功
栈满了
23
出栈成功
14
栈满了
a b c d e
栈的长度时5
e
d
c
b
a
null
null

栈的长度时0
a b f
栈的长度时3
栈的长度时0


2、循环队列(数组实现的顺序循环队列)

队列是一种特殊的线性表,与线性表的差别在于:线性表的插入和删除操作可以再表中的任意位置上进行;而队列在对头进行删除操作,在队尾进行插入操作。

package com.four;

public class myQueue3 {

    public static void main(String[] args) {
        String s ="123456789";
        //int len = s.length();
        QueueObject q = new QueueObject(9);
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            q.insert(c);
        }
        q.display();  
        q.remove() ;        
        q.display();    q.remove() ;        
        q.display();    q.remove() ;        
        q.display();
        q.insert("a");
        q.display();
        q.insert("b");q.display();
    }

}
class QueueObject{
    private Object[] o;
    private int head;
    private int rear;
    public QueueObject(int max){
        o = new Object[max];
        head =0;
        rear =0;
    }
    public boolean isEmpty(){
        return (head==rear);
    }
    public boolean isFull(){
        return ((rear+1)%o.length==head);
    }
    public void insert(Object value){
        if(!isFull()){
            o[rear]=value;
            rear = (rear+1)%o.length;
        }else{
            System.out.println("队列满了");
        }
    }
     public void remove(){
         if(!isEmpty()){
             System.out.println(o[head]);
             head = (head+1)%o.length;
         }else{
             System.out.println("队列是空的");
         }
     }
     public void display(){
         for(int i =head;i!=rear;i=(i+1)%o.length){
                System.out.print(o[i]+" ");
            }
            System.out.println();
     }
}

结果是:

队列满了
1 2 3 4 5 6 7 8
1
2 3 4 5 6 7 8
2
3 4 5 6 7 8
3
4 5 6 7 8
4 5 6 7 8 a
4 5 6 7 8 a b


3、优先队列(数组实现的优先对列)

优先队列是指数据项按照关键字的值进行排序,这样关键字最小的数据项总在对头(或是关键字最大的数据项总在对头,本文默认全部是关键字最小的在对头);并且

优先队列中没有使用指针回绕,即没有处理假溢出的问题。

另外优先队列中不必非要声明对头和队尾字段,因为这两个字段都是固定的。即head= elem-1    rear = 0

/**
 * 优先队列:不必必须有head和rear,因为这是固定的,head总是等于elem-1;rear总是等于0
 * 优先队列中没有使用指针回绕
 */
package com.four;

public class PriorityQueue {

    public static void main(String[] args) {
         int a[] = {1,324,6,2,-9,0,45};
         priorityQ q = new priorityQ(a.length);
         for(int i = 0;i<a.length;i++){
             q.insert(a[i]);
         }
         q.display();
        System.out.println(q.remove());
         q.display();
    }

}
class priorityQ{
    private int[] array;
    private int elem;
    private int size;
    public priorityQ(int max){
        size = max;
        array = new int[size];
        elem = 0;
    }
    //判断是否为空
    public boolean isEmpty(){
        return (elem==0);
    }
    //判断是否为满
    public boolean isFull(){
        return (elem==size);
    }
    //新增
    public void insert(int value){
        int j=0;
        if(!isFull()){
            //如果队列中没有数据项,就插入到下标为0的单元里
            if(elem==0){
                array[elem++] = value;
            }else{ //否则就从数组顶部开始上移存在的数据项,直到找到新数据项应当插入的位置,然后加入,并把elem加1
                for( j= elem-1;j>=0;j--){
                    if(value > array[j]){
                        //后移
                        array[j+1] = array[j];
                    }else{
                        break;
                    }
                }
                array[j+1]= value;
                elem++;
            }
        }else{
            System.out.println("优先队列满了");
        }
        
    }
    //删除
    public int remove(){
        return array[--elem];
    }
    //遍历
    public void display(){
        for(int i = 0;i<elem;i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}

//用object写的话,不能用< > 比较
/*class priorityQ{
    private Object[] array;
    private int elem;
    private int size;
    public priorityQ(int max){
        size = max;
        array = new Object[size];
        elem = 0;
    }
    //判断是否为空
    public boolean isEmpty(){
        return (elem==0);
    }
    //判断是否为满
    public boolean isFull(){
        return (elem==size);
    }
    //新增
    public void insert(Object value){
        if(!isFull()){
            //如果队列中没有数据项,就插入到下标为0的单元里
            if(elem==0){
                array[elem++] = value;
            }else{ //否则就从数组顶部开始上移存在的数据项,直到找到新数据项应当插入的位置,然后加入,并把elem加1
                for(int j = elem-1;j>=0;j--){
                    if(value > array[j]){
                        
                    }
                }
            }
        }else{
            System.out.println("优先队列满了");
        }
        
    }
    //删除
    public Object remove(){
        return array[--elem];
    }
    //遍历
    public void display(){
        for(int i = 0;i<elem;i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}*/

结果是:

324 45 6 2 1 0 -9
-9
324 45 6 2 1 0


0 0
原创粉丝点击