Queue supports the following methods:
insert(obj): Adds object obj at the rear of a queue.
Input: Object; Output: None.
obj remove(): Deletes an item from the front of a queue and returns object obj; an error occurs if the queue is empty.
Input: None; Output: Object.
obj peek(): Returns the object obj at the front of a queue , without removing it; an error occurs if the queue is empty.
Input: None; Output: Object.
boolean isEmpty(): Returns a boolean indicating if the queue is empty.
Input: None; Output: boolean (true or false).
int size(): Returns the number of items in the queue.
Input: None; Output: integer.
Type Object may be any type that can be stored in the queue. The actual type of the object will be provided by the user.
Queue Implementation Using Array:
Queue Implementation Using LinkedList:
Please comment if you find any mistake.
insert(obj): Adds object obj at the rear of a queue.
Input: Object; Output: None.
obj remove(): Deletes an item from the front of a queue and returns object obj; an error occurs if the queue is empty.
Input: None; Output: Object.
obj peek(): Returns the object obj at the front of a queue , without removing it; an error occurs if the queue is empty.
Input: None; Output: Object.
boolean isEmpty(): Returns a boolean indicating if the queue is empty.
Input: None; Output: boolean (true or false).
int size(): Returns the number of items in the queue.
Input: None; Output: integer.
Type Object may be any type that can be stored in the queue. The actual type of the object will be provided by the user.
Queue Implementation Using Array:
interface Queue { public void insert(Object ob); public Object remove(); public Object peek(); public boolean isEmpty(); public int size(); } class ArrayQueue implements Queue { private int maxSize; // maximum queue size private Object[] que; // que is an array private int front; private int rear; private int count; // count of items in queue (queue size) public ArrayQueue(int s) // constructor { maxSize = s; que = new Object[maxSize]; front = rear = -1; count = 0; } public void insert(Object item) // add item at rear of queue { if( count == maxSize ) { System.out.println("Queue is Full"); return; } if(rear == maxSize-1 || rear == -1) { que[0] = item; rear = 0; if( front == -1) front = 0; } else que[++rear] = item; count++; // update queue size } public Object remove() // delete item from front of queue { if( isEmpty() ) {System.out.println("Queue is Empty"); return 0; } Object tmp = que[front]; // save item to be deleted que[front] = null; // make deleted item’s cell empty if( front == rear ) rear = front = -1; else if( front == maxSize-1 ) front = 0; else front++; count--; // less one item from the queue size return tmp; } public Object peek() // peek at front of the queue { return que[front]; } public boolean isEmpty() // true if the queue is empty { return (count == 0); } public int size() // current number of items in the queue { return count; } public void displayAll() { System.out.print("Queue: "); for( int i = 0; i < maxSize; i++ ) System.out.print( que[i] + " "); System.out.println(); } } public class Main { public static void main(String[] args) { /* queue holds a max of 5 items */ ArrayQueue q = new ArrayQueue(5); Object item; q.insert('A'); q.insert('B'); q.insert('C'); q.displayAll(); item = q.remove(); // delete item System.out.println(item + " is deleted"); item = q.remove(); System.out.println(item + " is deleted"); q.displayAll(); q.insert('D'); // insert 3 more items q.insert('E'); q.insert('F'); q.displayAll(); item = q.remove(); System.out.println(item + " is deleted"); q.displayAll(); System.out.println("peek(): " + q.peek()); q.insert('G'); q.displayAll(); System.out.println("Queue size: " + q.size()); } } |
Queue Implementation Using LinkedList:
interface Queue { public void insert(int d); public int remove(); public int peek(); public boolean isEmpty(); public int size(); } //to create the node of a class class Node { int data ; Node next; Node(int d) { data =d; next=null; } } class QueueLinkedList implements Queue { Node root=null; Node newnode; Node temp; /* function to add the element at the rear */ public void insert(int d) { newnode = new Node(d); if(root==null) { root=newnode; } else { temp=root; while(temp.next!=null) { temp=temp.next; } temp.next=newnode; } } //function to retrieve the element from the front public int remove() { if(root==null) { System.out.println("Queue is empty "); return -1; } int num=root.data; Node temp =root.next; root=temp; return num; } //function to check wether queue is empty public boolean isEmpty() { if(root==null) return true; else return false; } //function to return the element at the front without deleting it public int peek() { if(root==null) { System.out.println("Queue is empty "); return -1; } int num=root.data; return num; } // function to return the size(number of elements) of Queue public int size() { Node temp=root; int count=0; while(temp!=null) { count++; temp=temp.next; } return count; } } public class Main { public static void main (String[] args) { QueueLinkedList Q=new QueueLinkedList(); Q.insert(9); Q.insert(1); Q.insert(8); Q.insert(2); System.out.println(Q.peek()); System.out.println(Q.remove()); System.out.println(Q.remove()); System.out.println(Q.size()); } } |
Please comment if you find any mistake.
No comments:
Post a Comment