Problem Statement:
Implement Queue
Solution:
Given below is the kind of scenario in case of Queue Data structure ,
where items are pushed(en-queue-d) from rear and removed (de-queue-d) from front.
Operations supported by Queue data structure are:
1. En-queue:
It means insertion of elements from rear pointer
2. De-queue:
It means deletion of items from front pointer.
A Queue supports FIFO operations i.e. First In First Out (FIFO)
Implementation:
References:
http://basicstodatastructures.blogspot.in/2013/10/queue.html from a good friend of mine.
Please post your comments and suggestions
Happy Coding!! :)
Implement Queue
Solution:
Given below is the kind of scenario in case of Queue Data structure ,
where items are pushed(en-queue-d) from rear and removed (de-queue-d) from front.
![]() |
Fig: Example of Queue |
1. En-queue:
It means insertion of elements from rear pointer
2. De-queue:
It means deletion of items from front pointer.
A Queue supports FIFO operations i.e. First In First Out (FIFO)
Implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | - /** * Queue Implementation * @author Prateek */ public class Queue { private static int DEFAULT_CAPACITY = 5 ; int front; int rear; int arr[]; public Queue() { arr = new int [DEFAULT_CAPACITY]; Arrays.fill(arr, - 1 ); front = 0 ; rear = 0 ; } public Queue( int size) { DEFAULT_CAPACITY=size; arr = new int [DEFAULT_CAPACITY]; Arrays.fill(arr, - 1 ); front = 0 ; rear = 0 ; } public void push( int data) { rear = (rear) % (DEFAULT_CAPACITY); if (arr[rear] == - 1 ) { arr[++rear] = data; System.out.println( "Pushed: " + arr[rear - 1 ]); } else System.out.println( "Queue is Full" ); } public void pop() { int val = arr[front]; arr[front] = - 1 ; front++; System.err.println( "Poped: " + val); } public void display() { System.out.print( "Queue is : \t" ); int i = front; while (i <= DEFAULT_CAPACITY) { System.out.print(arr[i]+ "\t" ); i = (i + 1 ) % DEFAULT_CAPACITY; if (i == rear) break ; } } } - |
References:
http://basicstodatastructures.blogspot.in/2013/10/queue.html from a good friend of mine.
Please post your comments and suggestions
Happy Coding!! :)
No comments:
Post a Comment