SHARE
virus
Advertisement

Java comes with some different datatypes you can use from the start. To list a few (Source: Java for Dummies Quick Reference):

  • Ints
  • Strings
  • Longs
  • Byte

Each acts as a “barrier” between the low-level programming required to make each thing and the people/programmers using them. Datatypes make things easier for everyone because they’re premade and are easily understood.

But, let’s  say you want to make a new data-type. You want to make a queue or a stack. Java and other programs may not have an obvious implementation of these things. Due to this, you need to make an Abstract Datatype.

Firstly you need to understand the datatype you’re going to make. So, if you think about a Queue, you know that new people join the end and the people at the front get served first. So that’s our starting point.

Obviously, you need to make a new queue too. An empty queue if needs be, but you need to make the structure first. You also need to be able to enqueue items and dequeue items in the queue. So you need to implement those things too.

Before we get onto the code just let me list out the properties of a queue:

  • Create a queue
  • Enqueue an object (add to back)
  • Dequeue an object (remove from front)
  • Check if the queue is empty
  • Take a look at who’s at the front of the queue

We want to implement queues (this ADT) easily by using code like this:

So now we know what we need. We need to make the queue ADT. And that’s pretty simple. We just need a Queue class where we describe the queue and the operations we want to carry out on it.

Now you can add, dequeue, check if the queue is empty .etc. Note, the code may be incorrect is it’s essentially Java-esque pseudocode. But you should get the point!

Advertisement