Java Data Structure queue

Source: Internet
Author: User

Queuing is similar to queuing in real life. Queue is the principle of first-in-first-out. Only elements can be removed from the queue header, and elements can be added to the end of the queue.

The following are queues implemented by using arrays and linked lists as storage structures respectively.

public interface Queue
 
   {int size();T remove();void add(T element);boolean isEmpty();void clear();boolean hasElement();}
 

Public class ArrayQueue
 
  
Implements Queue
  
   
{// Default size of the array: private static final int DEFAULT_SIZE = 10; // The default size of the array to store private Object [] values = new Object [DEFAULT_SIZE]; private int arrayLength = DEFAULT_SIZE; // private int top =-1; private int bottom =-1; @ Overridepublic int size () {return (top-bottom) + 1 ;} // Delete the element @ SuppressWarnings ("unchecked") @ Overridepublic T remove () {if (isEmpty () {throw new NullPointerException ();} T value = (T) values [++ top]; return value;} // add the element @ Overridepublic void add (T element) {if (bottom> = arrayLength-1) at the bottom of the column) {resize () ;}values [++ bottom] = element ;}@ Overridepublic boolean isEmpty () {return top> bottom ;}@ Overridepublic void clear () {top = bottom =-1 ;}@ Overridepublic boolean hasElement () {return top <bottom;} public void resize () {arrayLength = arrayLength + DEFAULT_SIZE; object [] temp = new Object [arrayLength]; for (int I = 0; I
   
    
ArrayQueue = new ArrayQueue
    
     
(); ArrayQueue. add (1); arrayQueue. add (2); arrayQueue. add (3); arrayQueue. add (4); arrayQueue. add (5); arrayQueue. add (6); arrayQueue. add (7); arrayQueue. add (8); arrayQueue. add (9); arrayQueue. add (10); arrayQueue. add (11); while (arrayQueue. hasElement () {System. out. println (arrayQueue. remove ());}}}
    
   
  
 

public class LinkedList
 
   implements Queue
  
    {private int size = 0;private Item top ;private Item bottom;private class Item{private T data;private Item next;Item(T data,Item next){this.data = data;this.next = next;}}@Overridepublic int size() {return size;}@Overridepublic T remove() {if(--size < 0){throw new NullPointerException();}T value = top.data;top = top.next;return value;}@Overridepublic void add(T element) {if(top == null){top = new Item(element,null);}else if(bottom == null){bottom = new Item(element,null);top.next = bottom;}else{Item item = new Item(element,null);bottom.next = item;bottom = item;}size ++;}@Overridepublic boolean isEmpty() {return size == 0;}@Overridepublic void clear() {top = bottom = null;}@Overridepublic boolean hasElement() {if(top == null){return false;}return top.data != null;}public static void main(String args[]){LinkedList
   
     linkedList = new LinkedList
    
     ();linkedList.add(1);linkedList.add(2);linkedList.add(3);linkedList.add(4);linkedList.add(5);linkedList.add(6);linkedList.add(7);linkedList.add(8);linkedList.add(9);linkedList.add(10);linkedList.add(11);while(linkedList.hasElement()){System.out.println(linkedList.remove());}}}
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.