
466
19 COMPUTATIONAL THINKING AND PROBLEM SOLVING
19
static void push(int item)
{
if (topPointer < stackFull - 1)
{
topPointer = topPointer + 1;
stack[topPointer] = item;
}
else
System.out.println("Stack is full, cannot push");
}
Java
ACTIVITY 19E
In your chosen programming language, write a program using subroutines
to implement a stack with 10 elements. Test your program by pushing two
integers 7 and 32 onto the stack, popping these integers off the stack, then
trying to remove a third integer, and by pushing the integers 1, 2, 3, 4, 5, 6, 7,
8, 9 and 10 onto the stack, then trying to push 11 on to the stack.
Queues
In Chapter 10, we looked at the data and the operations for a circular queue
using pseudocode. You will need to be able to write a program to implement a
queue. The data structures and operations required to implement a similar queue
using a fixed length integer array and separate sub routines for the enqueue and
dequeue operations are set out below in each of the three programing languages.
If you are unsure how the operations work, look back at Chapter 10.
Queue data structure Language
queue = [None for index in range(0,10)]
frontPointer = 0
rearPointer = -1
queueFull = 10
queueLength = 0
Python
empty queue with
no items
Public Dim queue() As Integer = {Nothing, Nothing, Nothing, Nothing,
Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}
Public Dim frontPointer As Integer = 0
Public Dim rearPointer As Integer = -1
Public Const queueFull As Integer = 10
Public Dim queueLength As Integer = 0
Public Dim item As Integer
VB
empty queue
with no items
and variables,
set to public for
subroutine access
public static int queue[] = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public static int frontPointer = 0;
public static int rearPointer = -1;
public static final int queueFull = 10;
public static int queueLength = 0;
public static int item;
Java
empty queue
with no elements
and variables,
set to public for
subroutine access
V
Table 19.10
V Table 19.11