数据结构和算法(二)

package com.example.demo;

/**
 * 
 * <P>Description: 栈功能模拟--数组</P>
 * @ClassName: StackX
 * 
 * 栈的后进先出特性
 * @author 冯浩  2018年4月23日 上午11:29:50
 * @see TODO
 */
public class StackX {
    
    private int maxSize;
    private char[] stackArray;
    private int top;
    
    public StackX(int max) {
        this.maxSize=max;
        stackArray=new char[maxSize];
        top=-1;
    }
    
    public void push(char l) throws Exception {
        if(!isFull()) {
            stackArray[++top]=l;
        }else {
            throw new Exception();
        }
        
    }
    
    public char pop() throws Exception {
        if(!isEmpty()) {
            return stackArray[top--];
        }else {
            throw new  Exception();
        }
    }
    
    public char peek() {
        return stackArray[top];
    }
    
    public boolean isEmpty() {
        return (top==-1);
    }
    
    public boolean isFull() {
        return (top==maxSize-1);
    }
    
    
    public static void main(String[] args) throws Exception {
        String aaa="fenghao";
        StackX stack=new StackX(aaa.length());
        for (int i = 0; i < aaa.length(); i++) {
            char charAt = aaa.charAt(i);
            stack.push(charAt);
        }
        for (int i = 0; i < aaa.length(); i++) {
            System.out.println(stack.pop());
        }
    }
 
}
package com.example.demo;


/**
 * 
 * <P>Description: 队列编写</P>
 * @ClassName: Queue
 * @author 冯浩  2018年4月23日 下午3:46:14
 * @see TODO
 */
public class Queues {
    
    private int maxSize;
    private Long[] queArray;
    private int front;
    private int rear;
    private int nItems;
    
    public Queues(int s) {
        this.maxSize=s;
        queArray = new Long[maxSize];
        this.front=0;
        this.rear=-1;
        this.nItems=0;
    }
    
    public void insert(Long s) {
    
        if(rear==maxSize-1) {
            rear=-1;
        }
        queArray[++rear]=s; 
        nItems++;
    }
    
    public Long remove() {
        
        Long x=queArray[front++];
        queArray[front++]=null;
        if(front==maxSize) {
            front=0;
        }
        nItems--;
        return x;
    }
    
    public static void main(String[] args) {
        /*BigDecimal  a =  new BigDecimal("10");
        BigDecimal  b =  new BigDecimal("3");
        System.out.println(a.divide(b,2,2).toString());*/
    }

}

约瑟夫问题代码

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <P>Description: 约瑟夫问题</P>
 * @ClassName: YueSefu
 * @author 冯浩  2018年4月24日 上午10:38:49
 * @see TODO
 */
public class YueSefu {
    
       public static void yuesefu(int totalNum, int countNum) {  
                  // 初始化人数  
                  List<Integer> start = new ArrayList<Integer>();  
                  for (int i = 1; i <= totalNum; i++) {  
                      start.add(i);  
                  }  
                  //从第K个开始计数  
                  int k = 0;  
                  while (start.size() >0) {  
                      k = k + countNum;  
                      //第m人的索引位置  
                      k = k % (start.size()) - 1;  
                      System.out.println(String.format("k %s size %s", k,start.size()));
                     // 判断是否到队尾  
                      if (k < 0) {  
                          System.out.println(start.get(start.size()-1));  
                          start.remove(start.size() - 1);  
                          k = 0;  
                      } else {  
                          System.out.println(start.get(k));  
                          start.remove(k);  
                      }  
                  }  
              }  

    public static void main(String[] args) {
        YueSefu.yuesefu(41, 3);
//        int a=3%1;
//        System.out.println(a);
    }

}

网上找的代码

猜你喜欢

转载自www.cnblogs.com/nihaofenghao/p/8929813.html