HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

My implementation of Stack and Queue with Array without help of library functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withoutfunctionsstackwitharrayhelplibraryandimplementationqueue

Problem

I am new to Java and am trying to learn coding. Here is my implementation of a stack and a queue without the help of library functions. Please provide your suggestions on design, coding style and algorithm.

Stack.java

```
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;

public class Stack
{
private int size;
private int topPointer=-1;
private T[] stackArray;

public Stack()
{
this.size=10;
stackArray=(T[]) new Object[size];
}

public Stack(int size)
{
this.size=size;
stackArray=(T[]) new Object[size];
}

public void push(T element)
{
if(isFull())
{
throw new UnsupportedOperationException("Stack is full");
}
else
{
topPointer++;
stackArray[topPointer]=element;
System.out.println(element+" pushed to stack");
}
}

public T pop()
{
if(isEmpty())
{
throw new UnsupportedOperationException("Stack is empty");
}
else
{
T element=stackArray[topPointer];
topPointer--;
return element;
}
}

public boolean isFull()
{
if(topPointer==size-1)
{
return true;
}
else
{
return false;
}
}

public boolean isEmpty()
{
if(topPointer<0)
{
return true;
}
else
{
return false;
}
}

public T peek()
{
if(isEmpty())
{
throw new UnsupportedOperationException("Stack is empty");
}
else
{
return stackArray[topPointer];
}
}

public int search(T element)
{
if(!isEmpty())
{
for(int i=0;i<size;i++)
{
if(stackArray[i]!=null && stackArray[i].equals(element))
{

Solution

Stack

Boolean logic:

An expression "a == b" or "topPointer == size - 1" is already a boolean. Thus your isFull and isEmpty methods essentially read:

if(true) {
    return true;
}
else {
    return false;
}


replace this with the direct expression:

public boolean isEmpty() {
    return topPointer < 0;
}

// same for isFull


Search method:

Search needs to loop to topPointer, not to size. As topPointer is already smaller than a starting i = 0 for an empty stack, you can leave out the precheck for emptyness:

for(int i = 0; i <= topPointer; i++) {
    if(stackArray[i] != null && stackArray[i].equals(element)) {
         return topPointer - i; // provided that you mean to say: this many elements away from the top.
    }
}
throw new NoSuchElementException(...);


Style:

Spaces around operators and a different brace style would be java standard as used by most of the rest of the world. See the code-snippets I posted.

For your Queue implementation the feedback is exactly the same.

Code Snippets

if(true) {
    return true;
}
else {
    return false;
}
public boolean isEmpty() {
    return topPointer < 0;
}

// same for isFull
for(int i = 0; i <= topPointer; i++) {
    if(stackArray[i] != null && stackArray[i].equals(element)) {
         return topPointer - i; // provided that you mean to say: this many elements away from the top.
    }
}
throw new NoSuchElementException(...);

Context

StackExchange Code Review Q#153638, answer score: 6

Revisions (0)

No revisions yet.