Java Data Structures - Contents
A D V E R T I S E M E N T
We have already written quite a lot of useful stuff,
and there might come a time, when you're just too lazy to write something new, and would
like to reuse the old source. This section will show you how you can convert some data
structures previously devised, to implement a stack and a queue, with almost no creativity
(by simply reusing the old source).
Before we start, lets review the function of a
stack. It has to be able to push and pop items of from one end. What structure do we know
that can do something similar? A list! Lets write a list implementation of a stack.
import pLinkedList;
public class pEasyStack{
protected pLinkedList l;
public pEasyStack(){
l = new pLinkedList();
}
public boolean isEmpty(){
return l.isEmpty();
}
public void push(Object o){
l.insert(o);
}
public Object pop(){
return l.remove();
}
}
See how easily the above code simulates a stack by
using a list? It may not be the best implementation, and it's certainly not the fastest,
but when you need to get the project compiled and tested, you don't want to spend several
unnecessary minutes writing a full blown optimized stack. Test for the stack follows:
import java.io.*;
import pEasyStack;
class pEasyStackTest{
public static void main(String[] args){
pEasyStack s = new pEasyStack();
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<10;i++){
j = new Integer((int)(Math.random() * 100));
s.push(j);
System.out.println("push: " + j);
}
while(!s.isEmpty()){
System.out.println("pop: " + ((Integer)s.pop()));
}
System.out.println("Done ;-)");
}
}
The stack test program is exactly the same as for
the previous stack version (doesn't really need explanation). For the completion, I'll
also include the output.
starting...
push: 23
push: 99
push: 40
push: 78
push: 54
push: 27
push: 52
push: 34
push: 98
push: 89
pop: 89
pop: 98
pop: 34
pop: 52
pop: 27
pop: 54
pop: 78
pop: 40
pop: 99
pop: 23
Done ;-)
You've seen how easily we can make a stack. What
about other data structures? Well, we can just as easily implement a queue. One thing
though, now instead of just inserting and removing, we'll be removing from the end (the
other from the one we're inserting).
import pLinkedList;
public class pEasyQueue{
protected pLinkedList l;
public pEasyQueue(){
l = new pLinkedList();
}
public boolean isEmpty(){
return l.isEmpty();
}
public void insert(Object o){
l.insert(o);
}
public Object remove(){
return l.removeEnd();
}
}
Pretty easy huh? Well, the test driver is also easy.
import java.io.*;
import pEasyQueue;
class pEasyQueueTest{
public static void main(String[] args){
pEasyQueue s = new pEasyQueue();
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<10;i++){
j = new Integer((int)(Math.random() * 100));
s.insert(j);
System.out.println("insert: " + j);
}
while(!s.isEmpty()){
System.out.println("remove: " + ((Integer)s.remove()));
}
System.out.println("Done ;-)");
}
}
I guess you get the picture, reusing code may not
always be the best choice, but it sure is the easiest! Definitely, if you have time,
always write a better implementation; these approaches are only good for the deadline,
just to compile and test the code before the actual hard work of optimizing it. For the
sake of completeness, the output of the above program follows:
starting...
insert: 77
insert: 79
insert: 63
insert: 59
insert: 22
insert: 62
insert: 54
insert: 58
insert: 79
insert: 25
remove: 77
remove: 79
remove: 63
remove: 59
remove: 22
remove: 62
remove: 54
remove: 58
remove: 79
remove: 25
Done ;-)
I guess that covers code reuse! And remember, you
can also reuse the code in the Java API. The reuse can be of many forms. You can reuse it
the way I've shown in this section, or you can extend that particular class to provide the
necessary functions. (Extending a java.util.Vector class is very useful ;-)
Back to Table of Contents
A D V E R T I S E M E N T
|
Subscribe to SourceCodesWorld - Techies Talk |
|