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:
- /** * 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