Sunday 17 June 2018

Queue and Vector program in java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprogram;

import java.util.Vector;
import java.io.*;
/**
 *
 * @author rajesh kumar shukla
 */

//Serialize an Object to a File
public class Queue extends Vector {
     /*
     ** FIFO, first in first out
     */
         Queue() {
         super();
     }
     
         void put(Object o) {
         addElement(o);
     }
    
         Object get() {
         if (isEmpty()) return null;
         Object o = firstElement();
         removeElement(o);
         return o;
     }
    
         Object peek() {
         if (isEmpty()) return null;
         return firstElement();
     }


//To serialize (save the Queue state to a file) :

     public static void main(String args[]) {
     Queue theQueue;
      
     theQueue = new Queue();
     theQueue.put("element 1");
     theQueue.put("element 2");
     theQueue.put("element 3");
     theQueue.put("element 4");
     System.out.println(theQueue.toString());
      
     // serialize the Queue
     System.out.println("serializing theQueue");
         try {
         FileOutputStream fout = new FileOutputStream("thequeue.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fout);
         oos.writeObject(theQueue);
         oos.close();
     }
     catch (Exception e) { e.printStackTrace(); }
}
}

0 comments:

Post a Comment