Data Structures and Algorithms (2)

package com.example.demo;

/**
 *
 * <P>Description: stack function simulation--array</P>
 * @ClassName: StackX
 *
 * The last in first out feature of the stack
 * @author Feng Hao on April 23, 2018 at 11:29:50 AM
 * @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: Queue writing</P>
 * @ClassName: Queue
 * @author Feng Hao on April 23, 2018 at 3:46:14 PM
 * @see ALL
  */ 
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());*/
    }

}

joseph problem code

package com.example.demo;

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

/**
 *
 * <P>Description: Joseph Problem</P>
 * @ClassName: YueSefu
 * @author Feng Hao on April 24, 2018 at 10:38:49 AM
 * @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);  
                  }  
                  // count from the Kth   
                  int k = 0 ;  
                   while (start.size() >0 ) {  
                      k = k + countNum;  
                       // The index position of the mth person   
                      k = k % (start.size()) - 1 ;  
                      System.out.println(String.format( "k %s size %s" , k,start.size()));
                      // Determine whether the end of the queue is   reached 
                      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);
    }

}

code found online

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819007&siteId=291194637
Recommended