队列的顺序实现(循环数组)与链式实现

队列(queue)同栈一样,也是表。 是插入在一端(队尾)进行而删除在另一端(队头)进行的表。通过enqueue向队列中输入,通过dequeue从队列中输出。

顺序实现如下:

public class ArrayQueue<AnyType> {

	private AnyType theArray[];
	private int front,back;      //定义队头队尾
	private int currentSize;     //队列中存放元素个数
	private int maxSize=5;       //队列中能存放元素的个数
   
	public int size(){
		return currentSize;
	}
	public ArrayQueue(){
		theArray=(AnyType []) new Object[maxSize];
		front=back=0;
		currentSize=0;
	}
	
	public boolean isEmpty(){
		return front==back&¤tSize==0;
	}
	
	public void enQueue(AnyType x){           //入队
		if(currentSize==maxSize){             //溢出
			System.out.println("队列已满");
		}
		theArray[back]=x;
		back++;
		if(back==maxSize)                      //构成循环队列
			back=0;
		 currentSize++;
		
	}
	
	public AnyType deQueue(){         //出队
		if(isEmpty())
			return null;
		AnyType a=theArray[front];
		front++;
		if(front==maxSize)
			front=0;
		currentSize--;
		return a;	
	}
	
	public AnyType getFront(){
		if(isEmpty())
			return null;
		return theArray[front];
	}
	public static void main(String[] args) {
		//验证部分
		ArrayQueue<Integer> aq=new ArrayQueue<Integer>();
		aq.enQueue(0);aq.enQueue(1);aq.enQueue(2);aq.enQueue(3);aq.enQueue(4);
		aq.deQueue();aq.deQueue();	
		aq.enQueue(0);
        int m=aq.size();
		for(int i=0;i<m;i++){
			System.out.println(aq.deQueue());
		}
	}

}


链式实现如下:

public class SingleLinkedQueue<AnyType> {
      private Node<AnyType> front,back;           //定义front,back结点
      private int currenSize;                    //链表中数据个数
	
      private static class Node<AnyType>{                 //定义结点类
    		public AnyType data;
    		public Node<AnyType> next;
    		
    		public Node(AnyType data,Node<AnyType> next){
    			this.data=data;
    			this.next=next;
    		}
    		public Node(AnyType data){
    			this.data=data;
    			this.next=null;
    		}
    		public Node(){
    			this.data=null;
    			this.next=null;
    		}
    	}
      
      public SingleLinkedQueue(){
    	  front=back=null;
    	  currenSize=0;
      }
      
      public boolean isEmpty(){
    	  return currenSize==0;
      }
      
      public int size(){
    	  return currenSize;
      }
	 
      public void enQueue(AnyType x){                    //入队
    	  Node<AnyType> p=new Node<AnyType>(x); 
    	  if(front==null){                             //
    		  front=back=p;
    	  }
    	  back.next=p;
    	  back=p;
    	  currenSize++;
      }
      
     
      public AnyType deQueue(){                    //出队
    	  if(front==null){
    		  return null;
    		  //若为void方法,System.out.println("此队列已无数据");
    	  }
    	  AnyType old=front.data;                       
    	  front=front.next;                      //令front1后移
    	  currenSize--;
    	  return old;
      }
       
      
	public static void main(String[] args) {
		//验证部分
		SingleLinkedQueue<String> slq=new SingleLinkedQueue<String>();
		slq.enQueue("aaa");slq.enQueue("bbb");slq.enQueue("ccc");slq.enQueue("ddd");slq.enQueue("eee");
		int m=slq.size();
		for(int i=0;i<m;i++){
			System.out.println("第"+(i+1)+"次输出的数据"+slq.deQueue());
		}

	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光光-Leo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值