isEmpty
int top = -1; //or 0
public boolean isEmpty () {
return (top == -1);
}isFull
public boolean isFull() {
return (top == size-1);
}
add to stack
public void add (int element) {
if (isFull() == true)
System.out.println("Stack full, cannot add element.");
else
stack[++top] = element;
}
delete from stack
public int delete () {
if (isEmpty() == true)
System.out.println("Stack empty, cannot delete element.");
else
return stack[top--];
}
print in order
public void printOrder() {
while(!isEmpty()) {
int value = remove();
System.out.print(value + " ");
}
}
find an element in stack
public boolean find(int element) {
System.out.print("Please enter element to find: ");
element = keyboard.nextInt();
for (int i = 0; i < size; i++) {
if (stack[i] == element)
return true;
}
return false;
}
TO NOTE
Although stack and queues have been implemented using an array - these ADTs are not accessed as an array. No actual code for a queue on this test; but you should be able to write an algorithm to demonstrate the concept of a queue.