Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    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" array
    Last edited by mtyoung; 05-20-2009 at 12:54 PM.

  3. #3
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    How can I store the data?

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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];
    You are making stack reference to a new array of Objects (which is empty).
    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

  5. #5
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    there another option to create the new array and put the data in the same order that was in the old one?
    Yeap... use a "for" loop... along with: newArray[i] = oldArray[i]
    can be possible to double the length of the old array so it keeps the data?
    If it's the Array class, no. If you use ArrayList class, then you shouldn't have any problems

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Stack layout
    By blue404 in forum SWT / JFace
    Replies: 0
    Last Post: 03-22-2009, 02:15 PM
  2. Need help please – implementing a stack!
    By sfe23 in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 04:19 PM
  3. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 05:59 PM
  4. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •