Created
June 24, 2013 02:23
-
-
Save pmiguel/5847375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package leakyqueue; | |
/** | |
* | |
* @author Pedro | |
*/ | |
public class LeakyQueue { | |
private Node head; | |
private Node tail; | |
private int maxSize; | |
private int nElem; | |
public LeakyQueue(int maxSize) { | |
this.maxSize = maxSize; | |
this.nElem = 0; | |
this.head = null; | |
this.tail = null; | |
} | |
public void in(double value) { | |
Node n = new Node(value); | |
if(head == null) { | |
head = n; | |
tail = n; | |
nElem++; | |
} else if(nElem < maxSize){ | |
tail.setNextNode(n); | |
tail = n; | |
nElem++; | |
} else { | |
out(); | |
tail.setNextNode(n); | |
tail = n; | |
nElem++; | |
} | |
} | |
public void out() { | |
head = head.getNextNode(); | |
nElem--; | |
} | |
public void print() { | |
Node tmp = head; | |
while(tmp != null) { | |
System.out.print(tmp.getValue() + "\t"); | |
tmp = tmp.getNextNode(); | |
} | |
} | |
private class Node { | |
private Node next; | |
private double value; | |
public Node(double value) { | |
this.value = value; | |
next = null; | |
} | |
public double getValue() { | |
return value; | |
} | |
public void setNextNode(Node n) { | |
next = n; | |
} | |
public Node getNextNode() { | |
return next; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment