Java Reference
In-Depth Information
A byte-code file named StackArray.class encapsulating the object character-
istics and its methods is produced into the current directory. We can now use
this bytecode and object interface into a demonstration program stored into
another text file, say DemoStack.java :
Program 8.16 Stack in action: A simple demonstration program
class DemoStack {
public static void main( String [ ] args )
{ StackArray myStack= new StackArray (10) ;
int i;
for (i=0;i < 10; i++)
myStack.Push(i);
for (i=0;i < 15; i++)
System . out . println (myStack . Pull () ) ;
}
Compiling and running this demonstration program, we get the following
console output:
Succesfully created a stack array object...
9
8
7
6
5
4
3
2
1
0
-1
-1
-1
-1
-1
Let us now see how to implement the two methods of the stack interface using
another kind of data-structure for its backbone: linked lists.
8.6.2 Implementing generic stacks with linked lists
Instead of using an array, let us now use a linked list for storing incoming
elements. We first recall the list declaration and its basic primitives:
 
Search WWH ::




Custom Search