临近找工作高峰,最近也在不断的练习数据结构与算法。今天看到一道题,不难但是的的却却吸引住了我,我想了几个办法,思想和相应代码如下,如果大家有更好的建议或者有发现错误,请吐槽指出。
思路1:
一个定长数组,三个堆栈。首先想到的是,每个堆栈长度是固定的,如若数组长度为n,那么堆栈a的空间从0到n-1,堆栈b的空间从n到2n-1,堆栈C的空间从2n到3n-1. 给定三个引用,分别指向三个堆栈的top,相应操作就操作top即可。
(1)优劣分析
好处:操作方便。
劣势:空间利用局限,比如堆栈A使用空间远超其他两个堆栈,那么就造成堆栈A的使用效率急剧降低,而其他两个堆栈空间浪费。
(2)代码
代码如下:(此代码只经过简单测试,如有bug,望不吝赐教)
public class StackNQueueTest{
int[] array;
int topA,topB,topC;
public StackNQueueTest(int n){
array = new int[n];
topA = -1;
topB = n/3-1;
topC = 2*n/3-1;
}
public boolean push(int type, int value){
boolean isOk= false;
if(type == 1){
if(topA == topB)
System.out.println("Stack A is full.");
else{
array[++topA] = value;
isOk= true;
}
}
else if(type == 2){
if(topB == topC)
System.out.println("Stack B is full.");
else{
array[++topB] = value;
isOk = true;
}
}
else{
if(topC == array.length)
System.out.println("Stack C is full.");
else{
array[++topC] = value;
isOk = true;
}
}
return isOk;
}
public int pop(int type){
int value =-1;
if(type == 1){
if(topA<0)
System.out.println("Stack A is empty.");
else
value = array[topA--];
}
else if(type ==2){
if(topB<(array.length/3))
System.out.println("Stack B is empty.");
else
value = array[topB--];
}
else{
if(topC< array.length*2/3)
System.out.println("Stack B is empty.");
else
value = array[topC--];
}
return value;
}
public static void main(String[] args){
StackNQueueTest s = new StackNQueueTest(6);
s.push(1, 2);
s.push(1, 3);
s.push(1,4);
System.out.println(s.pop(1));
System.out.println(s.pop(2));
System.out.println(s.pop(1));
System.out.println(s.pop(1));
}
}
思路2:
主要思路是,堆栈A在数组低端自底而上生长,堆栈B在数组顶端自上而下生长,而堆栈C在数组中间,生长顺序按照mid+1,mid-1,mid+2,mid-2.....的顺序增长,注意这里的mid是动态的,即每一次移动堆栈A和堆栈B的栈顶指针,mid就要从新计算一次。
(1)优劣分析:
优势:这种思路,突破了思路1里使用空间限制问题,可以使得堆栈A和堆栈B充分增长至没有空闲空间为止。
劣势:但是,这样造成了堆栈C的操作比较麻烦。同时C的第一次入栈操作就决定了,C的操作空间。因为一旦入栈,那么C的元素就要开始记录,一旦改变了mid,那么就意味着原始入栈记录要同时保持移动,否则会造成内存泄漏。所以看似此种思路有突破,但是代价是比较大的。这种代价主要来自于两方面:1.堆栈C的数据移动 2堆栈C空间受到压缩。
所以这种思路使用于优先级的情况下,如果堆栈C的优先级明显低于其他堆栈,并且操作并不频繁,那么此种思路较为符合。
(2)代码:此处代码暂略,有兴趣可以把你的代码贴出来,大家一起研究下。
思路3:设定数据结构,即每个数组元素可以设定成如下格式:
class Node{
int value;
Node previous;
}
即每个元素都有指向上一元素的引用。每次入栈出栈只需要记录上一元素位置即可。
(1)优劣分析:
好处:充分使用空间,同时免去移动数据之苦,操作简捷明了。
坏处:但是入栈操作的时候,我们怎么能知道哪些空间可以使用呢?这又是一个问题。在下一个思路里,我会提出两种方法进行改进。
(2)代码:
思路未完整,代码放在中间
思路4:
对于思路3的整理,我提出两种方法改进。
(1)改进方法一:
每次入栈出栈操作保留三个栈顶指针的最大索引,入栈出栈的时候按照最大索引操作。比如 topA =1,topB=2,topC =3 那如果对栈A操作,我会选择在index 4上面入栈。出栈同理。
优劣分析:
优势:在一定程序上弥补思路3,比如我们知道哪些空间可以使用。
劣势:但是这种方式会造成一定空间浪费。 比如下图:
A A B A C B_ _ _ _ C
(字母代表栈存储数据,_代码空闲空间)。
在上面的情况中,如果C最后一个元素没有出栈,那么栈A和栈B的入栈操作只能在最后一个C元素之后进行,这样就造成了空间浪费。
(2)最终改进版本:
如何有效改进呢?运用链表(双向循环链表最好,单链表也可以达到目的)的思路,把空闲空间链接起来,同时这也是操作系统里对内存使用的方法。
一旦我们有空闲空间,我们有了记录,同时每个结点都可以指向上一个结点的地址,这样的话,操作也简单不少,空间利用达到最大。
(3)代码:代码匆匆完成,没有经过太多测试,如果大家发现bug,请及时通知我修正。同时此代码比较sloppy,如果大家发现我编程中有哪些不好的习惯或者有更好的实现方式,麻烦你能及时指正。非常感谢。
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class StackNQueueTest{
ArrayList<Element> array;
Queue<Integer> freeList;
int topA;
int topB;
int topC;
public class Element{
int value;
int previous;
public Element(int value){
this.value = value;
this.previous = -1;
}
public Element(int value, int pre){
this.value = value;
this.previous = pre;
}
}
public StackNQueueTest(int num){
array = new ArrayList<Element>(num);
freeList = new LinkedList<Integer>();
for(int i =0;i<num;i++)
freeList.add(i);
topA = -1;
topC = -1;
topB = -1;
}
public boolean push(int type,int value){
boolean isOK = true;
int temp =0;
if(freeList.isEmpty()){ System.out.println("Stack "+ (char)(type+64) +" is full: "+value+" can't be inserted."); return false;}
//The top pointer always points at the toppest one of stacks
temp = freeList.poll();
if(type ==1){
//add new element to appointed position: temp
array.add(temp,(new Element(value,topA)));
//keep top pointer stores the last update position of relevent stack
topA = temp;
}else if(type == 2){
array.add(temp,(new Element(value,topB)));
topB = temp;
}else{
array.add(temp,(new Element(value,topC)));
topC = temp;
}
return isOK;
}
public int pop(int type){
int value;
if(type ==1){
if(topA == -1) { System.out.println("Empty StackA");return -1;}
else{
//take the toppest element
Element temp = array.get(topA);
value = temp.value;
//update freelist
freeList.add(topA);
//update topA
topA = temp.previous;
//free Element
temp = null;
}
}else if(type == 2){
if(topB == -1) { System.out.println("Empty StackB");return -1;}
else{
//take the toppest element
Element temp = array.get(topB);
value = temp.value;
//update freelist
freeList.add(topB);
//update topA
topB = temp.previous;
//free Element
temp = null;
}
}else{
if(topC == -1) { System.out.println("Empty StackC");return -1;}
else{
//take the toppest element
Element temp = array.get(topC);
value = temp.value;
//update freelist
freeList.add(topC);
//update topA
topC = temp.previous;
//free Element
temp = null;
}
}
return value;
}
public static void main(String[] args){
StackNQueueTest s = new StackNQueueTest(6);
s.push(1, 1);
s.push(2, 2);
s.push(3, 3);
s.push(3, 4);
s.push(3, 5);
s.push(3, 6);
s.push(3, 7);
s.push(3, 8);
s.push(3, 9);
s.push(3, 10);
s.push(3, 11);
s.push(3, 3);
s.push(3, 3);
s.push(3, 3);
System.out.println(s.pop(1));
System.out.println(s.pop(2));
System.out.println(s.pop(3));
}
}