Welcome to Dream.In.Code
Become a Java Expert!

Join 150,197 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,032 people online right now. Registration is fast and FREE... Join Now!




array stack problem

 
Reply to this topicStart new topic

array stack problem, need solution how to call stack in array

redzuan
14 Sep, 2008 - 08:41 AM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
have problem how to pass the array to stack...any idea
CODE

public class TestAStack {

    public static void main(String[] args) {
        AStack<Integer> arrayStk = new AStack<Integer>();
        
        Random rgen = new Random();
        int[] stk = new int[10];
        for(int i=0;i<stk.length;i++)
        {
            arrayStk.push(new Integer(stk[i] = (rgen.nextInt(10)+1)));
            //arrayStk.push(new Integer(rgen.nextInt(10)));//push 10 random number onto the stack;
            
        }
        System.out.println(arrayStk);
        
        
        /*while(arrayStk.size()<5)
        {
            arrayStk.pop();
        }
        System.out.println(arrayStk);
        
        
        /*while(alStk.size()>= 10)
        {
            
        }*/
        //System.out.println(arrayStk.size());
        
        
        
        
        
        
    }
}


CODE
public class AStack<T> {

    private T[] stk;
    private int length;//the number of element the stack can store
    private int count;//the number of element the stack is storing
    
    public AStack()
    {
        
    }
    public AStack(int len)//create an empty AStack that can store up to len element
    {
        length= len;
        stk =(T []) new Object[len];//array with empty element
    }
    
    
    public boolean isEmpty()
    {
        return count==0;
    }
    
    public boolean isFull()
    {
        return count == length;
    }
    
    public int size()
    {
        return length;
    }
     /*public Object top( ) {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayStack top" );
        return theArray[ topOfStack ];
    }*/
    
    
    public T peek()
    {
        if(isEmpty())
        {
            System.out.println("Peek:Stack is empty");
            return null;
        }
        else
            return stk[count--];
    }
     /*public void pop( ) {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayStack pop" );
        topOfStack--;
    }*/
    public T pop()
    {
        if(isEmpty())
        {
            System.out.println("Pop:Stack is empty");
            return null;
        }
        else
            return stk[count-1];
    }
    /*public void push( Object x ) {
        if( topOfStack + 1 == theArray.length )
            doubleArray( );
        theArray[ ++topOfStack ] = x;
    }*/
    public void push(T item)
    {
        for(count=0; count<size();count++)
        {
            stk[count]=item;
            System.out.print(stk[count]);
        }
        
         //stk[count++]=item;
    }
    @Override
    public String toString()//return the string element
    {
        String s = "[";
        int i;
        int count = size();
        
        for(i=0; i<count; i++)
        {
            
            s = s + stk[count];// [value1,value2,....]
            if(i<count-1)
            {
                s +=",";
            }
        }
        
        s +="]";
        return s;
    }
    
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Array Stack Problem
14 Sep, 2008 - 11:47 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Your constructor of AStack() withou parameter is useless... you cannot create an EmptyStack

Your Pop did not update count

java

import java.util.Random;

public class TestAStack {

public static void main(String[] args) {

// when constructing a stack it needs a length
int length = 10;
AStack<Integer> arrayStk = new AStack<Integer>(length);

Random rgen = new Random();
for(int i=0; i< length; i++)
{
arrayStk.push(new Integer(rgen.nextInt(10) + 1));
}
System.out.println(arrayStk);

while(!arrayStk.isEmpty())
System.out.println("Poping " + arrayStk.pop());

}
}


java

public class AStack<T> {

private T[] stk;
private int length;//the number of element the stack can store
private int count;//the number of element the stack is storing


public AStack(int len)//create an empty AStack that can store up to len element
{
length = len;
count = 0;
stk =(T []) new Object[len];//array with empty element
}


public boolean isEmpty()
{
return count==0;
}

public boolean isFull()
{
return count == length;
}

public int size()
{
return length;
}

public T peek()
{
if(isEmpty())
{
System.out.println("Peek:Stack is empty");
return null;
}
else
return stk[count--];
}

public T pop()
{
if(isEmpty())
{
System.out.println("Pop:Stack is empty");
return null;
}
else
return stk[--count];
}
public void push(T item)
{
if(count == length) {
System.out.println("Stack is full");
return;
}
System.out.println("Inserting " + item + " at [" + count + "]");
stk[count++] = item;

}
@Override
public String toString()//return the string element
{
String s = "[";
for(int i = 0; i <count; i++)
{
if(i != 0)
s += ", ";
s = s + stk[i];// [value1,value2,....]
}

s +="]";
return s;
}

}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:45AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month