forked from fnplus/interview-techdev-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class Queue { | ||
private static int front, rear, capacity; | ||
private static int queue[]; | ||
Queue(int c) | ||
{ | ||
front = rear = 0; | ||
capacity = c; | ||
queue = new int[capacity]; | ||
} | ||
static void queueEnqueue(int data) | ||
{ | ||
if (capacity == rear) { | ||
System.out.printf("\nQueue is full\n"); | ||
return; | ||
} | ||
else { | ||
queue[rear] = data; | ||
rear++; | ||
} | ||
return; | ||
} | ||
static void queueDequeue() | ||
{ | ||
if (front == rear) { | ||
System.out.printf("\nQueue is empty\n"); | ||
return; | ||
} | ||
else { | ||
for (int i = 0; i < rear - 1; i++) { | ||
queue[i] = queue[i + 1]; | ||
} | ||
if (rear < capacity) | ||
queue[rear] = 0; | ||
rear--; | ||
} | ||
return; | ||
} | ||
static void queueDisplay() | ||
{ | ||
int i; | ||
if (front == rear) { | ||
System.out.printf("\nQueue is Empty\n"); | ||
return; | ||
} | ||
for (i = front; i < rear; i++) { | ||
System.out.printf(" %d <-- ", queue[i]); | ||
} | ||
return; | ||
} | ||
static void queueFront() | ||
{ | ||
if (front == rear) { | ||
System.out.printf("\nQueue is Empty\n"); | ||
return; | ||
} | ||
System.out.printf("\nFront Element is: %d", queue[front]); | ||
return; | ||
} | ||
} | ||
public class StaticQueueinjava { | ||
public static void main(String[] args) | ||
{ | ||
Queue q = new Queue(4); | ||
q.queueDisplay(); | ||
q.queueEnqueue(20); | ||
q.queueEnqueue(30); | ||
q.queueEnqueue(40); | ||
q.queueEnqueue(50); | ||
q.queueDisplay(); | ||
q.queueEnqueue(60); | ||
q.queueDisplay(); | ||
q.queueDequeue(); | ||
q.queueDequeue(); | ||
System.out.printf("\n\nafter two node deletion\n\n"); | ||
q.queueDisplay(); | ||
q.queueFront(); | ||
} | ||
} |