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:

public static void main(String args[]){

//some random people in the room that all need my loving
String[] peopleThatNeedMyLove = {"john", "jesus", "buddha", "jenny from da block"};

//this queue i've made for them to stand in peacefully
Queue aQueueForMyLoving = new Queue();

//put these people in the queue (a forEach loop through the String array)
for(String desperatePerson: peopleThatNeedMyLove){
aQueueForMyLoving.enqueue(desperatePerson);
}

//then if the queue isn't empty start processing each person (remove from front)
while(!aQueueForMyLoving.isEmpty()){
aQueueForMyLoving.dequeue();
}


}

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.

// Need to make a person
public class Person(){
String name;
Person next;


public Person(String name, Person next){
this.name = name;
this.next = next;
}

}


//now the queue...
public class Queue(){

// variables that describe the queue
Person personAtFront;
Person personAtBack;


//if you put someone in the queue, the old person at the back is now THEIR next. 
//Then that person becomes person at the back
public void enqueue(Person newDude){

if(isEmpty()){
personAtFront = newDude;
personAtBack = newDude
} else {
newDude.next = personAtBack;
personatBack = newDude;
}


}

//you can check if the the front and back is empty. if it is then it's obviously empty.
public boolean isEmpty(){
return (personAtFront == null && personAtBack == null );
}

//remove from the front...
public void dequeue(){
Person temporaryPerson = personAtBack;

//find the front but getting next, next, next....
do{
temporaryPerson = temporaryPerson.next;
}while(temporaryPerson.next.next != null);

//no we've got the person behind the first person, KILL THEM!
temporary.next = null;


}

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!

Leave a Reply
You May Also Like

YouTube SEO: How To Make a YouTube Description Good for SEO (Free Template)

Your YouTube description is a hugely important part of determining how you…

5 Ways To Make Money without Leaving Your House

There’s a tonne of posts for teens and students out there showcasing…