Let us create the \(\displaystyle \text{Stack}\) class and put inside it our \(\displaystyle \text{push}\) method.
Our \(\displaystyle \text{Stack}\) class is:
class \(\displaystyle \text{Stack}\){
}
We just need to put the \(\displaystyle \text{push}\) method inside the \(\displaystyle \text{Stack}\) class.
class \(\displaystyle \text{Stack}\){
Node top = null;
public void push(int data){
Node newNode = new Node(data);
if (top == null){
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
}
Why do we need to put the \(\displaystyle \text{push}\) method inside a class? Because it gives us the ability to create many objects of that class, each one of them can use the \(\displaystyle \text{push}\) method with a different value anywhere in our code. This helps us fill our box with different numbers each time we call the \(\displaystyle \text{push}\) method.
Our code is almost ready. All remains is to figure out a way to display on the screen what we have inside our box. That will be the job of creating a new method and place it inside the \(\displaystyle \text{Stack}\) class.
Let us call that new method \(\displaystyle \text{display}\).
Coding the \(\displaystyle \text{display}\) method is the easiest part of our program because we are just displaying information on the screen. Then, let us code it.
public void display(){
Node topNode = top;
System.out.println("Put "+topNode.data+" inside the box.");
System.out.print("Our box contains: ");
while (topNode != null){
System.out.print (topNode.data+" ");
topNode = topNode.next;
}
System.out.println("\n");
}
What does this code exactly do? It simply references the top \(\displaystyle \text{Node}\) to topNode. We know that the top \(\displaystyle \text{Node}\) has two important pieces of information, the top
number and a
reference to the previous \(\displaystyle \text{Node}\) which also contains a number and another reference.
Let us assume that our box has these numbers:
\(\displaystyle 19 \ 7 \ 3\)
which can be written with visualization as:
\(\displaystyle 19 \rightarrow 7 \rightarrow 3 \rightarrow \text{null}\)
This means that the top \(\displaystyle \text{Node}\) contains the number \(\displaystyle \text{data} = 19\) and a reference \(\displaystyle \text{next} = 7 \rightarrow 3 \rightarrow \text{null}\).
We use topNode.data to display \(\displaystyle 19\) on the screen.
Then, we enter a loop, the loop says:
If \(\displaystyle 19 \neq \text{null}\) which is true, then print \(\displaystyle 19\) on the screen and set the previous \(\displaystyle \text{Node}\) as the top \(\displaystyle \text{Node}\).
This will make the top \(\displaystyle \text{Node}\) now contains \(\displaystyle \text{data} = 7\) with a reference \(\displaystyle \text{next} = 3 \rightarrow \text{null}\).
The process will repeat until all numbers are printed on the screen like this:
\(\displaystyle 19 \ 7 \ 3\)
And the condition will become \(\displaystyle \text{null} = \text{null}\) which will terminate the loop which in turn will terminate the program successfully.
Our \(\displaystyle \text{Stack}\) class now becomes:
class \(\displaystyle \text{Stack}\){
Node top = null;
public void push(int data){
Node newNode = new Node(data);
if (top == null){
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
public void display(){
Node topNode = top;
System.out.println("Put "+topNode.data+" inside the box.");
System.out.print("Our box contains: ");
while (topNode != null){
System.out.print (topNode.data+" ");
topNode = topNode.next;
}
System.out.println("\n");
}
}
In the next post, we will put our code in the test and we will display a screenshot to see the result.