Results 1 to 6 of 6
Thread: Help with this stack application
- 05-19-2009, 09:53 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Help with this stack application
Hi!
I have to modify the following code that constructs a stack so it can duplicate its length once it is full. I have also to modify the pop method so it can reduce the stack length by half if the quantity of elements is less than half of the length. The 'new' code has to keep the old elements. So far I changed the push method and it works (duplicates the stack length when is full) but I have problems with the pop. I would appreciate any help.
Thanks in advance
//Original code with the new push
public class SimpleStack{
private Object[] stack;
private int top;
public SimpleStack(int capacity){//método constructor
stack=new Object[capacity];
top=0;
}//end SimpleStack
public boolean isEmpty(){
return top==0;
}//end isEmpty
public boolean isFull(){
return top==stack.length;
}//end isFull
public void push(Object item){
if (isFull()){
System.out.println ("Stack overflow");
stack= new Object [2*stack.length];
}else{
stack[top++]=item;
}
}//termina método push
public Object pop(){
if(isEmpty()){
System.out.println("Stack underflow");
} else{
return stack[--top];
}//end pop
}//end class SimpleStack
// This is my pop method modification
public Object pop(){
if(isEmpty()){
System.out.println("Stack underflow");
}else{
if(top<(stack.length)/2){
stack=new Object[stack.length/2];
} else{
return stack[--top];
}}//end both else
}//end pop
}//end class SimpleStack
- 05-20-2009, 12:51 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
your push method seems remove all data stored before
your pop method also remove all data stored if top<(stack.length)/2
for "changing" length of array, you need to copy the data from the "old" array to "new" arrayLast edited by mtyoung; 05-20-2009 at 12:54 PM.
- 05-20-2009, 04:50 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
How can I store the data?
- 05-20-2009, 06:35 PM #4
Check out the arrayCopy method. It will allow you to easily copy the contents of your old array to a new array.
Also you do realize that when you do
Java Code:stack= new Object [2*stack.length];
This is one case you may want to copy the array.
The other case would be when you want to decrease the size of the array by half in the pop method.
Mr.Beans
- 05-20-2009, 07:52 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
I don't know if I can use the arrayCopy method. Is there another option to create the new array and put the data in the same order that was in the old one? Or, can be possible to double the length of the old array so it keeps the data?
Thanks
- 05-20-2009, 09:07 PM #6there another option to create the new array and put the data in the same order that was in the old one?
can be possible to double the length of the old array so it keeps the data?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Stack layout
By blue404 in forum SWT / JFaceReplies: 0Last Post: 03-22-2009, 02:15 PM -
Need help please – implementing a stack!
By sfe23 in forum New To JavaReplies: 0Last Post: 02-24-2009, 04:19 PM -
Stack not popping
By bugger in forum New To JavaReplies: 2Last Post: 01-28-2008, 05:59 PM -
Help with heap and stack
By coco in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 03:21 PM
Bookmarks