Queue/Definition: Difference between revisions

m
→‎{{header|Java}}: Syntax highlighting, changed to queue method names
m (→‎{{header|Pascal}}: Free Pascal actually has a space in it ... Now the link works :-))
m (→‎{{header|Java}}: Syntax highlighting, changed to queue method names)
Line 432:
=={{header|Java}}==
{{works with|Java|1.5+}}
This task could be done using a LinkedList from java.util, but here is a user-defined version with generics:
<java>public class Queue<E>{
Node<E> nexthead, tail;
 
static class Node<E>{
This task could be done using a LinkedList from java.util, but here is a user-defined version:
E retVal= head.value;
public class Queue<E>{
Node<E> head, tailnext;
 
static class public Node<E>(){
E this(0, valuenull);
}
Node<E> next;
 
public Node(E value, Node<E> next){
this(0,.value= null)value;
this.next= next;
}
}
 
public Node(E value, Node<E> nextgetNext(){
this.value= value;
this.next=return next;
}
 
public void setNext(Node<E> getNext(next){
returnthis.next= next;
}
 
}
public void setNext(Node<E> next){
 
this.next= next;
public Queue(){
}
return head= =tail= null;
}
 
public void enqueue(E value){ //standard queue name for "push"
public Queue(){
head=Node<E> tailnewNode= new Node<E>(value, null);
if(empty()){
}
tail head= newNode;
}else{
public void push(E value){
tail.setNext(newNode);
Node<E> newNode= new Node<E>(value, null);
}
if(empty()){
headtail= newNode;
}
}else{
 
tail.setNext(newNode);
public E popdequeue() throws java.util.NoSuchElementException{//standard queue name for "pop"
}
if(empty()){
tail= newNode;
throw new java.util.NoSuchElementException("No more elements.");
}
}
E retVal= head.value;
public E pop() throws java.util.NoSuchElementException{
head= head.getNext();
if(empty()){
return retVal;
throw new java.util.NoSuchElementException("No more elements.");
}
}
 
E retVal= head.value;
public boolean empty(){
head= head.getNext();
return retValhead == null;
}
}</java>
public boolean empty(){
return head == null;
}
}
 
=={{header|Pascal}}==
Anonymous user