Self queues, output queues use Pascal's Triangle

Title: queue data structure based on the knowledge, the preparation of an algorithm using the output queue Pascal triangle
solving ideas:
a self queue, the queue Pascal's triangle data into the tail;
2 print, the print head of the queue removed, and from the queue remove;
code as follows:
queue Code


package queue;
    /**
    * 定义一个队列queue
   */
public class Queue<T> {
        private   Object[] data; //队列中存放的数据
        private int maxSize ; //队列的大小
        private int front ;//指向队列头部的指针
        private int rear ; //指向队列尾部的指针
        public Queue(int size){
            if (size < 0)
                throw new IllegalArgumentException("数据非法: "+
                        size);
            this.maxSize = size;
            this.data = new Object[maxSize];
            front = -1;
            rear = -1;
        }
        /**
         * 判断队列是否已满
         * @return
         */
        public boolean isFull(){
            return rear == maxSize -1 ;
        }

        /**
         * 判断队列是否为空
         * @return
         */
        public boolean isEmpty(){
            return rear == front;
        }

        /**
         * 添加数据到队列
         * @param n
         */
        public void add(T t){
            if(isFull()){
                System.out.println("队列已满,不能添加");
                return;
            }
            data[++rear] = t;
        }

        /**
         * 显示头部数据
         * @return
         */
        public Object head(){
            if(isEmpty()){
                throw new RuntimeException("队列为空");
            }
            Object obj=data[front+1];
            return obj;
            //System.out.println(data[front+1]);
        }

        /**
         * 取出头部数据
         * @return
         */
        public Object pop(){
            if(isEmpty()){
                throw new RuntimeException("队列为空");
            }
            Object obj = data[++front];
            data[front] = null;
            return obj;
        }

        /**
         * 打印全部数据
         */
        public void print(){
            if(isEmpty()){
                System.out.println("队列为空");
                return;
            }
            for(int i=0;i<data.length;i++){
                System.out.printf("array["+i+"]=%d\n",data[i]);
            }
        }
}

Print Pascal's Triangle Code :


import queue.Queue;
import java.util.Scanner;

public class YhTrianQueue {
    public static void main(String[] args) {
        System.out.println("How many lines do you want?:");
        Scanner input = new Scanner(System.in);
        //输入杨辉三角的行数
        int lines = input.nextInt();
        //创建二维数组,用于存放数据
        int[][] array=new int[lines][lines];
        //创建队列  队列的长度为杨辉三角中数据的总数(n*(n+1))/2
        Queue<Integer> queueI = new Queue((lines*(lines+1))/2);
        //循环在队列中加入数据
        for (int i=0;i<lines;i++){
            //打印数值
            for (int j=0;j<i+1;j++){
                if ((i >= 2) && (j >= 1)&&(j < i)){
                    array[i][j] = array[i-1][j-1] + array[i-1][j];
                    //在队列中添加数据
                    queueI.add(array[i][j]);
                }
                else {
                    array[i][j] = 1;
                    //在队列中添加数据
                    queueI.add(array[i][j]);
                };
            }
        }
        //打印杨辉三角
        for (int i=0;i<=lines-1;i++) {
            //打印空格
            for (int j=0;j<=lines-i-1;j++){
                System.out.print(" ");
            }
            //打印数值,每行打印的个数为行数的数量
            for (int m=0; m <= i; m++) {
                System.out.print(queueI.head() + " ");
                //队头出队列
                queueI.pop();
            }
            System.out.println();
        }



    }

}


Output
when the input is a row 9:
Here Insert Picture Description

Released six original articles · won praise 0 · Views 1067

Guess you like

Origin blog.csdn.net/helloworldchina/article/details/104082708