package Queue;
import java.util.Stack;
class Queuenode{
int data;
Queuenode next;
public Queuenode(int e){
this.data = e ;
}
public Queuenode(){
}
}
public class Lqueue implements QueueIntf{
Queuenode front ;
Queuenode rear ;
public void enQueue(Object x){
if(front == null){
rear = new Queuenode((Integer) x);
front = rear;
}
else{
Queuenode s = new Queuenode((Integer) x);
s.next = null;
rear.next = s;
rear = s;
}
}
public void delQueue(){
if(front==rear)
return ;
Queuenode p = front.next;
front = null;
front = p;
}
public int size(){
int count =1;
Queuenode p = front;
while(p!=rear){
p = p.next;
count++;
}
return count;
}
public void clear(){
rear = front;
front.data = 0;
}
public boolean isEmpty(){
return front==null;
}
public Object head(){
return front.data;
}
public void display(){
if(front == rear){
System.out.println("队列为空!");
return;
}
else {
Queuenode p = front;
while (p != rear) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.print(p.data + " ");
}
}
public static void main(String[] args) {
Lqueue lq = new Lqueue();
System.out.println("队列是否为空:"+lq.isEmpty());
System.out.println("进队。。。。");
lq.enQueue(6);
lq.enQueue(4);
lq.enQueue(2);
lq.enQueue(5);
lq.enQueue(1);
System.out.println("队列元素为:");
lq.display();
System.out.println(" ");
System.out.println("队列中有"+lq.size()+"个元素");
System.out.println("队列是否为空:"+lq.isEmpty());
System.out.println("队首元素为:"+lq.head());
System.out.println("出队。。。。");
lq.delQueue();
lq.delQueue();
System.out.println("队列元素为:");
lq.display();
System.out.println(" ");
System.out.println("清空队列。。。。");
lq.clear();
System.out.println("队列元素为:");
lq.display();
}
}
运行结果:
队列是否为空:true
进队。。。。
队列元素为:
6 4 2 5 1
队列中有5个元素
队列是否为空:false
队首元素为:6
出队。。。。
队列元素为:
2 5 1
清空队列。。。。
队列元素为:
队列为空!