• 数据结构(一)之数组,栈,队列


    (一)数组对象

    数组对象即以对象的形式封装对数组的增,删,改,查的功能。具体见代码,首先封装一个数组对象。

    package cn.liusong.Array;
    
    public class MyArray {
        int[] arr;
    
        public MyArray() {
    
            arr = new int[0];
        }
    
        //数组大小
        public int size() {
            return arr.length;
        }
    
        //往數組中添加元素
        public void add(int element) {
            int[] newArr = new int[arr.length + 1];
            for (int i = 0; i < arr.length; i++) {
                newArr[i] = arr[i];
            }
            newArr[arr.length] = element;
            arr = newArr;
        }
    
        //刪除指定下标元素
        public void delete(int index) {
            //判断下标是否越界
            if (index < 0 || index > arr.length) {
                System.out.println("输入下标有误");
            }
            int newArr[] = new int[arr.length - 1];
            for (int i = 0; i < newArr.length; i++) {
                //想要删除的下标之前的元素
                if (i < index) {
                    newArr[i] = arr[i];
                } else {//想要删除的下标之后的元素
                    newArr[i] = arr[i + 1];
                }
            }
            //新数组替换旧数组
            arr = newArr;
        }
    
        //在任意位置插入元素
        public void insert(int index, int element) {
            if (index<0 || index > arr.length){
                System.out.println("输入下标越界");
            }
            int [] newArr = new int[arr.length+1] ;
            for (int i = 0 ; i<arr.length ; i++ ){
                if (i<index) {
                    newArr[i] = arr[i] ;
                }else {
                    newArr[i+1] = arr[i] ;
                }
            }
            newArr[index] = element ;
            arr = newArr ;
        }
    
        //替换指定下标的元素
        public void set(int index, int element) {
            if (index<0 || index > arr.length){
                System.out.println("输入下标越界");
            }
            arr[index] = element ;
        }
        public int get(int index){
            if (index<0 || index > arr.length){
                System.out.println("输入下标越界");
            }
            return arr[index] ;
        }
        //显示数组所有元素
        public void show() {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
            System.out.println("******************************");
        }
    }

    再建一个测试类的对象

    package cn.liusong.Array;
    
    public class TestArray {
        public static void main(String[] args) {
            MyArray ma = new MyArray();
            int size = ma.size();
            System.out.println(size);
            ma.add(99);
            ma.add(98);
            ma.show();
            ma.add(97);
            ma.add(96);
            ma.add(95);
            ma.show();
            ma.delete(3);
            ma.show();
            ma.insert(3,33);
            ma.show();
            ma.set(2,22);
            ma.show();
            System.out.println(ma.get(4));
    
        }
    }
    0
    99
    98
    ******************************
    99
    98
    97
    96
    95
    ******************************
    99
    98
    97
    95
    ******************************
    99
    98
    97
    33
    95
    ******************************
    99
    98
    22
    33
    95
    ******************************
    95
    
    Process finished with exit code 0

    上面是程序的运行结果。

    (二)栈

    栈的结构就是用上面数组的结构完成先进后出的功能

    package cn.liusong.Array;
    
    public class MyStack {
        int [] arr ;
        public  MyStack(){
            arr = new int[0] ;
        }
        //向栈中压入元素
        public void push(int element){
            int[] newArr = new int[arr.length+1] ;
            for (int i=0;i<arr.length;i++){
                newArr[i] = arr[i] ;
            }
            newArr[arr.length] = element ;
            arr = newArr ;
            for (int i=0;i<arr.length;i++){
                System.out.println(arr[i]);
            }
            System.out.println("*********************************");
        }
        //从栈中弹出元素
        public int pop() {
            int element;
            if (arr.length == 0) {
                System.out.println("Stack is empty!");
            }
            int[] newArr = new int[arr.length - 1];
            for (int i = 0; i < newArr.length; i++) {
                newArr[i] = arr[i];
            }
            element = arr[arr.length-1];
            arr = newArr ;
            return element ;
        }
    
        //查看栈顶元素
        public int peek(){
            int element = arr[arr.length-1] ;
            return element ;
        }
        //判断栈是否为空
        public boolean isEmpty(){
            return  arr.length == 0 ;
        }
    
    }

    再建立一个测试类

    package cn.liusong.Array;
    
    public class TestMyStack {
        public static void main(String[] args){
            MyStack ms = new MyStack();
            ms.push(9);
            ms.push(8);
            ms.push(7);
            ms.push(6);
            ms.push(5);
           // System.out.println( ms.pop());
            System.out.println( ms.peek());
        }
    }

    结果:

    (三)队列

    队列的结构与栈相似,只是它是先进先出的

    package cn.liusong.Array;
    
    public class Queue {
        int [] elements ;
        public Queue(){
            elements = new int[0] ;
        }
    
       public void push(int element){
            int newArr [] = new int [elements.length+1] ;
            for (int i = 0; i<elements.length;i++){
                newArr[i] = elements[i] ;
            }
           newArr[elements.length] = element ;
            elements = newArr ;
       }
    
       public int pop(){
            int element = elements[0] ;
            int[] newArr = new int[elements.length-1] ;
            for (int i=0 ; i < newArr.length ;i++ ){
                newArr[i] = elements[i+1] ;
            }
           elements = newArr ;
            return element ;
       }
    
       //判断当前队列是否为空值
        public boolean isEmpty(){
            return elements.length == 0;
        }
    
       //显示队列
        public  void show(){
            for (int i = 0; i<elements.length;i++){
             System.out.println(elements[i]);
            }
            System.out.println("****************");
    }
    }

    测试队列

    package cn.liusong.Array;
    
    public class TestQueue {
        public static void main(String[] args){
            Queue q = new Queue() ;
            q.push(5);
            q.push(4);
            q.push(3);
            q.show();
            q.pop();
            q.show();
        }
    }
    

     测试结果

  • 相关阅读:
    JavaScript
    css-装饰
    html 标签
    remote connect openshift mysql
    MySQL
    how to run a continuous background task on OpenShift
    openshifit 安装 redis
    Python之路,Day6
    选择排序
    C语言实现链表
  • 原文地址:https://www.cnblogs.com/Lovis/p/10685775.html
Copyright © 2020-2023  润新知