Skip to main content
Tweeted twitter.com/StackCodeReview/status/1001141746242289664
deleted 18 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Looking for advice and suggestions on Array based Stack-based stack

Description: An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to whatever the program gives you. The key reason was not to use any outside help like Arrays.copyOf(array, size) to increase size etc. (In future, LinkedStack will solve size problem). MAX_SIZE is made public to let a user know about MAX_SIZE and size() returns current size of the Stack. Return value int is used to make sure all push/insertions are successful on Stack.

An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to whatever the program gives you. The key reason was not to use any outside help like Arrays.copyOf(array, size) to increase size etc. (In future, LinkedStack will solve size problem). MAX_SIZE is made public to let a user know about MAX_SIZE and size() returns current size of the Stack. Return value int is used to make sure all push/insertions are successful on Stack.

So rightRight now I'm just looking for suggestions to improve my code before I push it to my githubGitHub account. All suggestions are appreciated. Thanks.

Looking for advice and suggestions on Array based Stack

Description: An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to whatever the program gives you. The key reason was not to use any outside help like Arrays.copyOf(array, size) to increase size etc. (In future, LinkedStack will solve size problem). MAX_SIZE is made public to let a user know about MAX_SIZE and size() returns current size of the Stack. Return value int is used to make sure all push/insertions are successful on Stack.

So right now just looking for suggestions to improve my code before I push it to my github account. All suggestions are appreciated. Thanks.

Array-based stack

Description:

An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to whatever the program gives you. The key reason was not to use any outside help like Arrays.copyOf(array, size) to increase size etc. (In future, LinkedStack will solve size problem). MAX_SIZE is made public to let a user know about MAX_SIZE and size() returns current size of the Stack. Return value int is used to make sure all push/insertions are successful on Stack.

Right now I'm just looking for suggestions to improve my code before I push it to my GitHub account.

Source Link

Looking for advice and suggestions on Array based Stack

Description: An array-based Stack class. Since this class is designed using an array the size of the Stack is limited to whatever the program gives you. The key reason was not to use any outside help like Arrays.copyOf(array, size) to increase size etc. (In future, LinkedStack will solve size problem). MAX_SIZE is made public to let a user know about MAX_SIZE and size() returns current size of the Stack. Return value int is used to make sure all push/insertions are successful on Stack.

So right now just looking for suggestions to improve my code before I push it to my github account. All suggestions are appreciated. Thanks.

package Stack;

public class StackArray
{
   public static final int MAX_SIZE = 1040;   // MAX size of a Stack
   private int topLook;                       // current element on Stack
   private int stackElements[];               // array to store Stack elements

public StackArray()                        // default constructor creates a Stack with capacity of 50
{
    topLook = -1;                          // initially no elements on the Stack
    stackElements = new int[MAX_SIZE];    // an array of MAX_SIZE gets created
}

public int size()                          // returns current size of the Stack
{
    return (topLook + 1);
}

public boolean emptyCheck()                // returns true if this Stack has no elements in it, false otherwise
{
    return (topLook == -1);
}

public boolean fullCheck()                 // returns true if this Stack is full, false otherwise
{
    return (topLook == MAX_SIZE - 1);
}

public int top()                           // returns top element of the Stack, -1 otherwise
{
    if (!emptyCheck())
    {
        return stackElements[topLook];
    }
    return -1;
}

public int push(int value)                 // push passed argument on the Stack. 1 indicates insertion was successful, -1 otherwise
{
    if (!fullCheck())
    {
        stackElements[++topLook] = value;
        return 1;
    }
    return -1;
}

public int pop()                           // returns top element of the Stack and remove it, -1 otherwise
{
    if (!emptyCheck())
    {
        return stackElements[topLook--];
    }
    return -1;
}
}