算法(第四版)1.3背包、队列和栈

下压(LIFO)栈(能够动态调整数据大小的实现)

import java.util.Iterator;
// 使用了泛型的概念,使用了Iterable接口
public class ResizingArrayStack<Item> implements Iterable<Item>{
	// 新建一个长度为1的数组a,用N表示栈的大小
	private Item[] a = (Item[]) new Object[1];
	private int N = 0;
	// 判断栈是否为空
	public boolean isEmpty() {  return N == 0;  }
	// 获取栈的大小
	public int size() {  return N;  }
	/* 将栈移动到一个大小为max的新数组
	 * 动态调整数组大小
	 * 使数组大小:栈大小<4
	 */
	private void resize(int max) {
		Item[] temp = (Item[]) new Object[max];
		for (int i = 0; i < N; i++)  temp[i] = a[i];
		a = temp;
	}
	/*
	 * 入栈:栈大小加一,
	 * 如果数组空间不足,数组空间调整为2倍
	 */
	public void push(Item item) {
		if(N == a.length) resize(2 * a.length);
		a[N++] = item;
	}
	/*
	 * 出栈:栈大小减一,
	 * 若数组空间为栈4倍,数组大小缩小两倍
	 * 并且返回出栈顶的值
	 */
	public Item pop() {
		Item item = a[--N];
		a[N] = null;
		if(N > 0 && N == a.length/4)  resize(a.length/2);
		return item;
	}
	/*
	 * 
	 */
	public Iterator<Item> iterator(){  return new ReverseArrayIterator();  }
	private class ReverseArrayIterator implements Iterator<Item>{
		private int i = N;
		public boolean hasNext() {  return i > 0;  }
		public Item next() {  return a[--i];  }
		public void remove() {  } 
	}
	
}

下压堆栈(链表实现)

package wang.ex_1_3;
import java.util.Iterator;

public class Stack<Item>  implements Iterable<Item>{
	private Node first;
	private int N;
	private class Node{
		Item item;
		Node next;
	}
	public boolean isEmpty() { return first == null; }
	public int size() { return N; }
	public void push(Item item) {
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		N++;
	}
	public Item pop() {
		Item item = first.item;
		first = first.next;
		N--;
		return item; 
	}
	public Item getTop() {
		return first.item;
	}
	/*
	public void print() {
		for(Node x = first; x != null; x = x.next) {
			System.out.println(x.item);
		}
	}
	*/
	public Iterator<Item> iterator(){ 
		return new ListIterator();
	}
	private class ListIterator implements Iterator<Item>{
		private Node current = first;
		public boolean hasNext() { return current != null; }
		public Item next() {
			Item item = current.item;
			current = current.next;
			return item;
		}
		public void remove() {  }
	}
}

先进先出队列

import java.util.Iterator;

public class Queue<Item> implements Iterable<Item>{
	private Node first;
	private Node last;
	int N = 0;
	private class Node {
		Item item;
		Node next;
	}
	public boolean isEmpty() { return first == null; }
	public int size() { return N; }
	public void enqueue(Item item) {
		Node oldlast = last;
		last = new Node();
		last.item = item;
		last.next = null;
		if(isEmpty()) last = first;
		else oldlast.next = last;
		N++;
	}
	public Item dequeue() {
		Item item = first.item;
		first = first.next;
		if(isEmpty()) last = null;
		N--;
		return item;
	}
	
	public Iterator<Item> iterator(){ 
		return new ListIterator();
	}
	private class ListIterator implements Iterator<Item>{
		private Node current = first;
		public boolean hasNext() { return current != null; }
		public Item next() {
			Item item = current.item;
			current = current.next;
			return item;
		}
		public void remove() {  }
	}
}

背包

import java.util.Iterator;

public class Bag<Item> implements Iterable<Item>{
	private Node first;
	private int N;
	private class Node{
		Item item;
		Node next;
	}
	public void add(Item item) {
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		N++;
	}
	
	public Iterator<Item> iterator(){ 
		return new ListIterator();
	}
	private class ListIterator implements Iterator<Item>{
		private Node current = first;
		public boolean hasNext() { return current != null; }
		public Item next() {
			Item item = current.item;
			current = current.next;
			return item;
		}
		public void remove() {  }
	}
}

发布了5 篇原创文章 · 获赞 0 · 访问量 75

猜你喜欢

转载自blog.csdn.net/qq_41984731/article/details/104129350