Skip to content

Commit

Permalink
added Fixed array simple queue in java
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Jan 31, 2019
1 parent a59f446 commit 1e02652
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 2 deletions.
155 changes: 155 additions & 0 deletions Java/Data-Structures/QUEUES/SIMPLE-QUEUE/FixedArraySimpleQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
abstract class FixedArraySimpleQueue
{
private static class FixedArrayQueue
{
private int front;
private int rear;
private int[] que;

/**
* default constructor
*/
public FixedArrayQueue()
{
this(5);
}
/**
* default constructor
* @param size
*/
public FixedArrayQueue(int size)
{
que = new int[size];
front=-1;
rear=-1;
}

/**
* This method will add an element to the queue
* @param val for value to enqueue
* @throws Exception
* @return void
*/
public void enqueue(int val) throws Exception
{
if(front==-1 && rear==-1)
{
front++;
}
rear++;
if(rear==que.length)
{
throw new Exception("FullQueueException");
}
que[rear]=val;
}

/**
* This method will remove an element from the queue
* @return int dequeued value
* @throws Exception
*/
public int dequeue() throws Exception
{
if(front==-1 || rear==-1)
{
throw new Exception("EmptyQueueException");
}
int temp = que[front];

//shifting left
int i=0;
for(i=0;i<que.length-1;i++)
{
que[i]=que[i+1];
}
que[i]=0;
rear--;

return temp;
}

/**
* It will return the front value from the queue
* @return int front value
*/
public int front()
{
if(isEmpty())
{
return -1;
}
return que[front];
}

/**
* It will return the size of the Queue
* @return int size of the Queue
*/
public int size()
{
return rear-front+1;
}

/**
* Tells whether the Queue is empty or not
* @return boolean
*/
public boolean isEmpty()
{
if(front==-1 || rear==-1)
{
return true;
}
return false;
}

/**
* It will print the queue array
* @param void
* @return void
**/
public void printCheck()
{
for(int i=0;i<que.length;i++)
{
System.out.print(que[i]+" ");
}
System.out.println();
}
}

public static void main(String[] args)
{
try
{
FixedArrayQueue Q = new FixedArrayQueue();
System.out.println(Q.isEmpty());
Q.enqueue(1);
Q.enqueue(2);
Q.enqueue(3);
Q.enqueue(4);
Q.enqueue(5);
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.enqueue(1);
Q.dequeue();
Q.enqueue(2);
Q.enqueue(3);
Q.enqueue(4);
Q.enqueue(5);
Q.enqueue(6);
System.out.println(Q.isEmpty());
System.out.println(Q.front());
System.out.println(Q.size());
Q.printCheck();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
3 changes: 2 additions & 1 deletion Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#### QUEUES

* SIMPLE QUEUE
* FIXED ARRAY SIMPLE QUEUE
* [FIXED ARRAY SIMPLE QUEUE](Data-Structures/QUEUES/SIMPLE-QUEUE/FixedArraySimpleQueue.java)
* DYNAMIC ARRAY SIMPLE QUEUE
* LINKED SIMPLE QUEUE
* CIRCULAR QUEUE
Expand All @@ -75,6 +75,7 @@
* FIXED ARRAY PRIORITY QUEUE
* LINKED PRIORITY QUEUE
* HEAPED PRIORITY QUEUE
* MISC QUEUE

#### HASHTABLE

Expand Down
72 changes: 71 additions & 1 deletion datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Indexer for Data Structures Lover
* blog
* docs
* implementation
* [C](C/Data-Structures/ARRAYS/jaggedarray.c)
* [C](C/Data-Structures/ARRAYS/MISC/jaggedarray.c)
* complexity

## :octocat: STRING
Expand Down Expand Up @@ -263,6 +263,76 @@ Indexer for Data Structures Lover

## :octocat: QUEUES

### SIMPLE QUEUE

#### FIXED ARRAY SIMPLE QUEUE

* blog
* docs
* implementation
* [JAVA](Java/Data-Structures/QUEUES/SIMPLE-QUEUE/FixedArraySimpleQueue.java)
* complexity

#### DYNAMIC ARRAY SIMPLE QUEUE

* blog
* docs
* implementation
* complexity

#### LINKED SIMPLE QUEUE

* blog
* docs
* implementation
* complexity

### CIRCULAR QUEUE

#### FIXED ARRAY CIRCULAR QUEUE

* blog
* docs
* implementation
* complexity

#### DYNAMIC ARRAY CIRCULAR QUEUE

* blog
* docs
* implementation
* complexity

#### LINKED CIRCULAR QUEUE

* blog
* docs
* implementation
* complexity

### PRIORITY QUEUE

#### FIXED ARRAY PRIORITY QUEUE

* blog
* docs
* implementation
* complexity

#### LINKED PRIORITY QUEUE

* blog
* docs
* implementation
* complexity

#### HEAPED PRIORITY QUEUE

* blog
* docs
* implementation
* complexity

## :octocat: TREES

### BINARY TREES
Expand Down

0 comments on commit 1e02652

Please sign in to comment.