Important alert: (current site time 7/15/2013 2:48:53 PM EDT)
 

VB icon

a simple queue

Email
Submitted on: 4/23/2009 8:54:03 AM
By: felix knorr 
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 5211
 
     it's a simple queue written by me with generics it's for beginner to understand how a queue works for an integer QUEUE: ( Queue<Integer> myQueue = new Queue<Integer>() )
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: a simple queue
// Description:it's a simple queue written by me with generics 
it's for beginner to understand how a queue works
for an integer QUEUE:
( Queue<Integer> myQueue = new Queue<Integer>() )
// By: felix knorr
//
// Inputs:/
//
// Returns:/
//
// Side Effects:/
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6350&lngWId=2//for details.//**************************************

public class Queue<T> {
	private Object[] arr;
	private int 	 getIndex;
	private int putIndex;
	private int size;
	private int usedSize;
	private boolean isSetSize;
	
	public Queue(){
		this(10);
		isSetSize = true;
	}
	
	public Queue(int size){
		arr= new Object[size];
		getIndex = 0;
		putIndex = 0;
		this.size = size;
		usedSize = 0;
	}
	
	public boolean isEmpty(){
		return usedSize == 0;
	}
	
	@SuppressWarnings("unchecked")
	public T get() throws QueueUnderflowExeption{
		usedSize--;
		
		if(usedSize == 0)
			throw new QueueUnderflowExeption("Die Queue ist leer");
		
		T element = (T)arr[getIndex];
		getIndex = (getIndex+1)%size;
		return element;
	}
	public void put(T element) throws QueueOverflowExeption{
		usedSize++;
		
		if((usedSize >= size) && (isSetSize))
			extend();
		else if(usedSize >= size)
			throw new QueueOverflowExeption("Die Queue ist voll");
			
		arr[putIndex] = element;
		putIndex = (putIndex+1)%size;
	}
	
	public int getSize() {
		return size;
	}
	public int getUsedSize() {
		return usedSize;
	}
	public boolean isSetSize() {
		return isSetSize;
	}
	public String toString(){
		StringBuilder sb = new StringBuilder();
		
		for(int i=usedSize;i>0;i--){
			T element;
			try {
				element = this.get();
				sb.append(element.toString());
				sb.append("\n");
				this.put(element);
			} catch (QueueUnderflowExeption e) {
				System.out.println(e.getMessage());
			} catch (QueueOverflowExeption e) {
				System.out.println(e.getMessage());
			}
			
		}
		return sb.toString();
	}
	
	private void extend() {
		Object[] tempArr = new Object[size*2];
		int count = 0;
		
		while(putIndex != getIndex){
			try {
				tempArr[count] = get();
				count++;
			} catch (QueueUnderflowExeption e) {
				e.printStackTrace();
			}
		}
		usedSize = count+1;
		getIndex = 0;
		putIndex = count;
		size *= 2;
		arr = tempArr;
	}
	public static void main(String[] args){
		Queue<String> myQueue = new Queue<String>();
		
		for(int i=0;i<20; i++){
			try {
				myQueue.put(i+"");
			} catch (QueueOverflowExeption e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(myQueue.toString());
		try {
			myQueue.get();
			myQueue.get();
			myQueue.get();
		} catch (QueueUnderflowExeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(myQueue.toString());
	}
}
//
//_ _ _ _ _ _ _ _ _ _
//	|_|_|_|_|_|_|_|_|_|_|
// ^ ^
///_\/_\
//	| |
// putIndex getIndex
//
//
//____________________
//	|Queue| 
// |--------------------|
// |-arr:Object[]|
// |-putIndex:int|
// |-getIndex:int|
// |-size:int|
// |-usedSize:int|
// |-isSetSize:boolean |
// |--------------------|
// |+Queue()|
// |+Queue(int) |
// |+isEmpty():boolean |
// |+get():<T> |
// |+put(<T>)|
// |+getSize():int |
// |+getUsedSize:int|
// |+isSetSize():boolean|
// |+toString():String |
// |-extend()|
// |____________________|
public class QueueOverflowExeption extends Exception{
	private static final long serialVersionUID = 1L;
	public QueueOverflowExeption() { }			
	 
	public QueueOverflowExeption(String s){
		super(s);	
	}
}
public class QueueUnderflowExeption extends Exception{
	private static final long serialVersionUID = 1L;
	public QueueUnderflowExeption() { }			
	 
	 public QueueUnderflowExeption(String s){
	super(s);	
	 }
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.