算法第四版版参考答案

1.3.4
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Judge_Brackets_Whether_Match_Use_Stack {

	/**
	 * 判断多对括号是否匹配    利用stack实现
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Stack stack = new Stack();
		System.out.println("plase input brackets string...");
		Scanner input = new Scanner(System.in);
		String str = input.next();
		char[] a = str.toCharArray();

		int len = a.length;
		String[] s = new String[len];
		for(int i=0; i

1.3.14

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ResizingArrayQueueOfStrings implements Iterable{

	/**
	 * 使用定长数组实现队列
	 * 使用调整数组的方法突破大小的限制   
	 * 了解队列的性质以及如何实现数组的大小调整
	 */
	private int N=0;
	private int head=0;
	private int tail=0;
	private String[] str;
	
	public ResizingArrayQueueOfStrings(int cap){
		str = new String[cap];
	}
	public boolean isEmpty(){
		return N == 0;
	}
	public int size(){
		return N;
	}
	
	public void reverse(int max){
		String[] temp  = new String[max];
		for(int i=0; i iterator(){
		return new ReverseArrayIterator();
	}
	private class ReverseArrayIterator implements Iterator{
		private int i=0;
		public boolean hasNext(){
			return i < N;
		}
		public String next(){
			return str[i++];
		}
		public void remove(){}
	}
	public static void main(String[] args) {
		ResizingArrayQueueOfStrings queue = new ResizingArrayQueueOfStrings(10);
		for(int i=0; i<10; i++){
			queue.enqueue("hello"+i);
		}
		System.out.println(queue.size());
        Iterator itr = queue.iterator();
        while(itr.hasNext()){
        	String str = itr.next();
        	System.out.print(str+" ");
        }
        System.out.println();
        System.out.println(queue.dequeue());
	}
}


你可能感兴趣的:(algorithms)