data structure queue implementation in javaee code example
Example 1: java queue
import java.util.*;
Queue<Integer> queue = new LinkedList<Integer>();
// use non-primative types when constructing
// to add a value to the back of queue:
queue.add(7);
// to remove and return front value:
int next = queue.remove();
// to just return front value without removing:
int peek = queue.peek();
Example 2: queue with array python
class ArrayQueue:
def __init__(self):
self._data = []
self._size = 0
self._front = 0
def __len__(self):
return self._size()
def is_empty(self):
return self._size == 0
def enqueue(self, e):
self._data.append(e)
self._size = self._size + 1
def deque(self):
if self.is_empty():
raise Empty('queue is empty')
else:
value = self._data[self._front]
self._data[self._front] = None
self._front = self._front + 1
self._size = self._size - 1
return value
def first(self):
if self.is_empty():
raise Empty('queue is empty')
else:
return self._data[self._front]
class Empty(Exception):
pass