Thursday, March 12, 2009

QUEUE IMPLEMENTATION

Concept: List of Barangay Councilor Officials

//declaration of a constructor
class Queue{
public int positionnum;
public String firstname;
public String lastname;
public char middlename;
public Queue next;

public Queue (int Pnum, String Fname String Lname char M, )
{

positionnum=Pnum;
firstname=Fname;
lastname=Lname;
middlename=M;
}


//displaying the elements on the queue
public void displayQueue()
{
System.out.print(positionnum +” “ + firstname +” “ +” “middlename+ “ “ +: + lastname)
}
}


/*a separate class which has the methods to be used by the queue implemented in a linked list*/
class QueueList
private Queue first;
private Queue last;
public QueueList()
{
first=null;
last=null;
}


//checking if the queue has elements
public Boolean isEmpty()
{
return (first==null);
}



//inserting an element on the queue
public void Enqueue(int Pnum, String Fname String Lname char M, )

{
Queue newQueue= new Queue (int Pnum, String Fname String Lname char M, )

if( isEmpty())
last = newQueue;
newQueue.next=first;
first=newQueue;
}


//deleting an element on the queue
public void Dequeue (int Pnum)
{
Queue newQueue=new Queue (int Pnum, String Fname String Lname char M, )

int temp=first.entrynum;
if (first.next==null)
last=null;
first=first.next;
return temp



}
}

public class MainClass {
public static void main(String[] args) {
LinkQueue theQueue = new LinkQueue();
theQueue.enqueue(1, “Marjorie”, “Egot”, ‘T’ )

theQueue.enqueue(2, “Chrisdyll”, “Pellejo”, ‘P’)
System.out.println(theQueue);

theQueue.enqueue(3, “Julie”, “Pabio”, ‘L’)
System.out.println(theQueue)



theQueue.dequeue(3);

System.out.println(theQueue);



System.out.println(theQueue);
}
}

No comments:

Post a Comment