Cracking coding interview(3.1)用一个数组实现3个堆栈

3.1 Describe how you could use a single array to implement three stacks.

Queue类型在Stack2中将会用到,用于保存非used指向的空间

class Stack1{
	final int STACK_SIZE = 100;
	int[] data = new int[3 * STACK_SIZE];
	int[] stack_top = {-1, -1, -1};
	public void push(int stack_num, int element){
		//if stack num is not filled
		if(stack_top[stack_num] < STACK_SIZE - 1){		
//			System.out.println("element="+element);//
			int index = stack_num * STACK_SIZE + stack_top[stack_num] + 1;
//			System.out.println("index="+index);//
			data[index] = element;
			stack_top[stack_num]++;
		}
	}
	public int pop(int stack_num){
		//if stack is empty
		if(stack_top[stack_num] == -1)
			return -1;
		int index = stack_num * STACK_SIZE + stack_top[stack_num];
		stack_top[stack_num]--;
		return data[index];	
	}
	public boolean empty(int stack_num){
		if(stack_top[stack_num] == -1)
			return true;
		else
			return false;
	}
}
class Stack2{
	private final int STACK_SIZE = 300;
	private Node[] data = new Node[STACK_SIZE * 3];
	private Queue queue = new Queue();//record deleted node
	private int[] stack_top = {-1, -1, -1};
	private int used = -1;
	//-1 stand for relevant stack is empty
	class Node{
		int val;
		int previous;
		public Node(int val, int previous){
			this.val = val;
			this.previous = previous;
		}
	}
	public void push(int stack_num, int val){
		if(used < STACK_SIZE * 3 || !queue.empty()){
			int index = 0;
			if(!queue.empty()){
				index = queue.remove().val;
			}else{
				index = ++used;	
			}
			data[index]	 = new Node(val, stack_top[stack_num]);
			stack_top[stack_num] = index;
		}else{
			System.out.println("stack is filled !");
		}
	}
	public int pop(int stack_num){
		if(stack_top[stack_num] != -1){
			int index = stack_top[stack_num];
			System.out.print("["+index+"]");//
			if(index != used)
				queue.add(index);
			else
				used--;
			Node n = data[index];				
			stack_top[stack_num] = n.previous;

			return n.val;
		}else
		return -1;
	}
	public boolean empty(int stack_num){
		if(stack_top[stack_num] == -1)
			return true;
		else
			return false;
	}
}

public class Solution{
	public static void main(String[] args){
	//	test for Stack1
	/*	
		Stack1 stack1 = new Stack1();
		for(int i=1;i < Integer.MAX_VALUE - 17;i += 17)
			stack1.push(i % 3, i);
		
		for(int i=0;i < 3;i++){
			System.out.println("stack1" + i + ":");
			for(;!stack1.empty(i);){
				System.out.print(stack1.pop(i) + " ");
			}
			System.out.println();
		}
	*/
		//test for Queue
	/*
		Queue queue = new Queue();
		for(int i=0;i < 20;i += 2)
			queue.add(i);
		while(!queue.empty()){
			System.out.print(queue.remove().val+" ");
		}
		System.out.println();
	*/
		//test for Stack2
	/**/
		Stack2 stack2 = new Stack2();
		int[] A = {
			1, 3, 4, 4, 5, 6, 7, 11, 11, 11, 23,
			22, 34, 34, 35, 45, 46, 47, 56, 58,
			78, 79, 80, 81, 89, 90,100
		};
		for(int i=0;i < A.length;i++)
			stack2.push(A[i] % 3, A[i]);
		for(int i=0;i < 3;i++){
			System.out.println("stack2" + i + ":");
			for(;!stack2.empty(i);){
				System.out.print(stack2.pop(i) + " ");
			}
			System.out.println();
		}			
	/**/
	}
}
class Queue {
	private Node first = null, last = null;
	class Node{
		int val;
		Node next;
		public Node(int val){
			this.val = val;
			this.next = null;
		}
	}
	public boolean empty(){
		if(first == null)
			return true;
		else
			return false;
	}
	public void add(int val){
		Node n = new Node(val);
		if(last == null){
			first = last = n;
		}else{
			last.next = n;
			last = n;
		}
	}
	public Node remove(){
		if(first != null){
			Node n = first;
			//remove queue with only one node
			if(first == last)
				first = last = null;
			else
				first = first.next;	
			return n;
		}else
			return null;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值