算法第四版习题1.3.14和1.3.19(Java实现)

近来在啃数据结构与算法,欢迎交流。

1.3.14:用可变长度的数组实现队列(FIFO)

  • 数据结构ResizingArrayQueueOfStrings 的实现:
public class ResizingArrayQueueOfStrings {
	String[] arr;
	int N; 

	public ResizingArrayQueueOfStrings(int cap) {
		arr = new String[cap];
	}

	public void queue(String str) {
		if (N + 1 == arr.length) 
			resize(arr.length * 2);
		for (int i = N; i >= 0; i--) {
			arr[i + 1] = arr[i];
		}
		arr[0] = str;
		N++;
	}

	public String dequeue() {
		String s = arr[--N];
		if (N > 0 && N == arr.length / 4)
			resize(arr.length / 2);
		return s;
	}

	private void resize(int max) {
		String[] arrnew = new String[max];
		for (int i = 0; i <= N; i++) {
			arrnew[i] = arr[i];
		}
		arr = arrnew;
	
	}
	
	public boolean isEmpty(){
		return N == 0;
	}
	public int size(){
		return N;
	}
}
  • client code(书上似乎称作:用例代码,翻译有点拗口哟)
public class Ex1_3_14_Client {

	public static void main(String[] args) {
		ResizingArrayQueueOfStrings qos = new ResizingArrayQueueOfStrings(5);
		qos.queue("Baba ");
		qos.queue("black ");
		System.out.println("size1:"+qos.size());
		qos.queue("sheep ");
		qos.queue("have ");
		System.out.println("size2:"+qos.size());
		qos.queue("you ");
		qos.queue("any ");
		qos.queue("wood ");
		qos.queue("?");
		System.out.println("size3:"+qos.size());
		
		String s = "";
		while (!qos.isEmpty()) {
			s += qos.dequeue();			
		}
		System.out.println(s);		
	}
}
  • 测试结果:
size1:2
size2:4
size3:8
Baba black sheep have you any wood ?

Baba black sheep have you any wood ?不知道有多少人知道这首朗朗上口的英文儿歌呢~

1.3.19:在以链表为基础的栈中添加一个方法,使之可以进行头尾“压栈”、头尾“弹栈”并返回弹出的元素:

  • 数据结构MyLinkedlist实现:
public class MyLinkedlist<Item> {
	private int N;
	private Node first;
	private Node last;
	
	
	private class Node{
		Node next;
		Item item;
	}
	
	public void push(Item item){
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		if (N == 0){
			first.next = null;
			last = first;
		}
		else
			first.next = oldfirst;
		N++;		
	}
		
	public void pushBottom(Item item) {
		Node oldlast = last;
		last = new Node();
		last.item = item;
		last.next = null;
		if (N == 0)
			first = last;
		else 
			oldlast.next = last;
		N++;
	}
	
	public Item pop(){
		Item item = first.item;
		first = first.next;
		N--;
		return item;
	}
	
	public Item popBottom(){
		Item item  = last.item;
		Node temp = first;
		for (int i = 1; i < N-1; i++) {
			temp = temp.next;       			// 第n-1个结点 
		}
		last = temp;
		last.next = null;
		N--;
		return item;
	}
	
	public int size(){
		return N;
	}
}
  • client code:
public class Ex1_3_19 {

	public static void main(String[] args) {
		MyLinkedlist<String> mll = new MyLinkedlist<>();
		mll.push("wood ");
		mll.push("any ");
		mll.push("you ");
		mll.push("have ");
		mll.pushBottom("? ");
		
		String str = "";
		int len = mll.size();
		for (int i = 0; i < len; i++) {
			str += mll.popBottom();
//              str += mll.pop();
		}
		System.out.println(str);
	}
}
  • 测试结果:
popBottom:
? wood any you have 

pop:
have you any wood ? 

猜你喜欢

转载自blog.csdn.net/weixin_41712499/article/details/82786034