public class Queue {
private int[] elements;
private int size;
private int first;
private int last;
public static final int DEFAULT_CAPACITY = 8;
public Queue(){
this (DEFAULT_CAPACITY);
}
public Queue (int capacity){
elements = new int[capacity];
first = 0;
last = 0;
size = 8;
}
public void Enqueue(int v){ //fills queue and lengthens if necessary
if (last>=size){
int[] temp = new int[elements.length*2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}
elements[last]=v;
last++;
}
public int Dequeue(){
int output = elements[first];
System.out.print(output + " ");
while(last != 0){
for(int i = 0; i<last;i++){
elements[i]= elements[i-1];
}
last--;
}
return output ;
}
public boolean empty(){ // tests for empty queue
return last==first;
}
public int getSize(){
size=last;
return size;
}
}
这是测试人员类。
public class QueueTester {
public static void main(String[] args){
Queue q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q.Enqueue(6);
q.Enqueue(7);
q.Enqueue(8);
q.Enqueue(9);
q.Enqueue(10);
q.Enqueue(11);
q.Enqueue(12);
q.Enqueue(13);
q.Enqueue(14);
q.Enqueue(15);
q.Enqueue(16);
q.Enqueue(17);
q.Enqueue(18);
q.Enqueue(19);
q.Enqueue(20);
while (q.empty()){
q.Dequeue();
答案 0 :(得分:1)
<style>
#ref2{
background-color: darkseagreen;
} #ref1{
background-color: pink;
}
</style>
<body>
<div id="ref1">
shortcut preview **
PHP -> isset ....add, delete, approve, search
then view the content (SELECT * FROM dbname)
see image ^
</div>
<div id="ref2">
<form action="tor.php" method="post">
<input type="text" name="search" placeholder="Search for members.." onkeydown="searchq();" required />
<input type="submit" value=">>">
<?php echo("$output"); ?>
</form>
</div>
<script langauge="javascript">
function updateContent() {
$.get("#", function(data)
{
$("#ref1").html( data );
});
}
setInterval(updateContent, 1000);
</script>
</body>
</html>
删除while循环。如果您正在尝试确保它没有使空队列出列,请检查if条件以确保其大小为&gt; 0
while(last != 0){
for(int i = 0; i<last;i++){
elements[i]= elements[i-1];
}
last--;
}
此外,您需要在测试人员类中打印输出,并且我假设您想在队列非空时出队:
public int Dequeue(){
if (getSize() == 0) {
// throw an error or something
}
int output = elements[first];
System.out.print(output + " ");
for(int i = 0; i<last;i++){
elements[i]= elements[i-1];
}
last--;
return output ;
}
答案 1 :(得分:0)
嗯,认为你使用的算法不正确尝试引用这个http://projectyogisha.com/implementing-queues/,它在C中。