Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

17.8 CODE LISTINGS

---------------------------------Version 1----------------------------------------
/*
  Class PQNode for Priority Queue
  Introducing Data Structures with Java
   David Cousins 2010
 */
public class PQNode {
  int priority;
  PQNode next;
  PQNode back;
  public PQNode(int value) {
  priority = value;
  next = null;
  back = null;
  }
}

----------------------------------------------------------------------------------

/* Amended priority queue class
used in Aircraft Boarding simulation version 1
Introducing Data Structures with Java
David Cousins 2010
 */
import java.io.*;
public class PQueue2 {
  static String passType[] = {"Standard passenger","Passenger with
children","Priority passenger","Passenger needing assistance"};
  public static PQNode head, tail;
  public PQueue2() {
    head = new PQNode(-1);
     tail = head;
  }
  public static boolean isEmpty() {
    if (head.priority == −1)
       return true;
    else
       return false;
  }
  public static void enQueue(int newvalue) {
    PQNode current, previous, temp;

					  

   temp = new PQNode(newvalue);
    if (isEmpty()) {
       temp.back = tail;
       tail.next = temp;
       head = temp;
    } else {
       current = tail;
       previous = tail;
       while (temp.priority > current.priority && current.next != null) {
         previous = current;
         current = current.next;
       }
       if (temp.priority <= current.priority) {
          previous.next = temp;
          temp.back = previous;
          temp.next = current;
          current.back = temp;
       } else {
          current.next = temp;
          temp.back = current;
          head = temp;
       }
    }
  }
  public static void deQueue() {
    int passenger = head.priority;
    if (isEmpty())
       System.out.println("Priority Queue is empty");
    else {
           System.out.println(passType[passenger]);
       }
       head = head.back;
    }
  }
----------------------------------------------------------------------------------
/*
  Simulation program version 1
  Aircraft Boarding order
  Introducing Data Structures with Java
  David Cousins 2010
 */
import java.io.*;
public class Queuesimulation {
  static PQueue2 myPQ;
  public static int readValue( )throws IOException {
   BufferedReader  in =  new BufferedReader(new InputStreamReader(System.
in));
   String input;
   int value =0;

					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint