In: Computer Science
Define empty methods in Queue class using LinkedList class in Java
-------------------------------------------------------------------------------
//Queue class
public class Queue{
public Queue(){
// use the linked list
}
public void enqueue(int item){
// add item to end of queue
}
public int dequeue(){
// remove & return item from the front of the queue
}
public int peek(){
// return item from front of queue without removing it
}
public boolean isEmpty(){
// return true if the Queue is empty, otherwise false
}
public int getElementCount(){
// return current total number of elements in Queue
}
public static void main(String[] args){
Queue s = new Queue();
int[] inputArr = {1, 2, 3, 4, 5, 6};
System.out.println("\nTrying to dequeue and peek on empty Queue:");
int test = s.dequeue();
test = s.peek();
System.out.println("\nPopulating the Queue:");
for (int v : inputArr){
System.out.println("Enqueuing: " + v);
s.enqueue(v);
}
System.out.println("\nRemoving from the Queue:");
while (!s.isEmpty()){
System.out.println("Dequeuing: " + s.dequeue());
}
System.out.println("\nTrying to dequeue and peek on empty Queue:");
test = s.dequeue();
test = s.peek();
}
}
------------------------------------
//Node class
public class Node{
private int value; // Stores the actual value
private Node nextNode; // Stores the link to the next Node object
public Node(int value){
setValue(value);
setNextNode(null);
}
public Node(int value, Node nextNode){
setValue(value);
setNextNode(nextNode);
}
// getters
public int getValue(){
return value;
}
public Node getNextNode(){
return nextNode;
}
// setters
public void setValue(int value){
this.value = value;
}
public void setNextNode(Node nextNode){
this.nextNode = nextNode;
}
@Override
public String toString(){
return String.valueOf(value);
}
}
-------------------------------------------------------------------------------
//LinkedList class
public class LinkedList implements LinkedListBehavior{
private int length;
private Node head;
private Node tail;
public LinkedList(){
length = 0;
head = null;
tail = null;
}
public boolean isEmpty(){
return head == null;
}
public int getLength(){
return length;
}
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public void append(Node newNode){
Node oldTail = getTail();
tail = newNode;
if (isEmpty()){
head = newNode;
}
else{
oldTail.setNextNode(tail);
}
length++; // update the current number of Node's in the LinkedList
}
public Node removeHead(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove head Node.");
return null;
}
Node oldHead = getHead();
head = oldHead.getNextNode();
oldHead.setNextNode(null);
if (isEmpty()){
tail = null;
}
length--;
return oldHead;
}
public Node removeTail(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove tail Node.");
return null;
}
Node oldTail = tail;
if (length == 1){
head = null;
tail = null;
length = 0;
return oldTail;
}
Node curNode = head;
while (curNode.getNextNode() != oldTail){
curNode = curNode.getNextNode();
}
curNode.setNextNode(null);
tail = curNode;
length--;
return oldTail;
}
@Override
public String toString(){
String output = "";
Node curNode = head;
while (curNode != null){
output += curNode + " ---> ";
curNode = curNode.getNextNode();
}
return output;
}
}
-------------------------------------------------------------------------------
public interface LinkedListBehavior{
public void append(Node newNode);
public Node getHead();
public Node getTail();
public Node removeHead();
public Node removeTail();
public boolean isEmpty();
public int getLength();
}
------------------------------------
//Node class
public class Node{
private int value; // Stores the actual value
private Node nextNode; // Stores the link to the next Node object
public Node(int value){
setValue(value);
setNextNode(null);
}
public Node(int value, Node nextNode){
setValue(value);
setNextNode(nextNode);
}
// getters
public int getValue(){
return value;
}
public Node getNextNode(){
return nextNode;
}
// setters
public void setValue(int value){
this.value = value;
}
public void setNextNode(Node nextNode){
this.nextNode = nextNode;
}
@Override
public String toString(){
return String.valueOf(value);
}
}
-------------------------------------------------------------------------------
//LinkedList class
public class LinkedList implements LinkedListBehavior{
private int length;
private Node head;
private Node tail;
public LinkedList(){
length = 0;
head = null;
tail = null;
}
public boolean isEmpty(){
return head == null;
}
public int getLength(){
return length;
}
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public void append(Node newNode){
Node oldTail = getTail();
tail = newNode;
if (isEmpty()){
head = newNode;
}
else{
oldTail.setNextNode(tail);
}
length++; // update the current number of Node's in the LinkedList
}
public Node removeHead(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove head Node.");
return null;
}
Node oldHead = getHead();
head = oldHead.getNextNode();
oldHead.setNextNode(null);
if (isEmpty()){
tail = null;
}
length--;
return oldHead;
}
public Node removeTail(){
if (isEmpty()){
System.out.println("\nLinkedList is empty. Can not remove tail Node.");
return null;
}
Node oldTail = tail;
if (length == 1){
head = null;
tail = null;
length = 0;
return oldTail;
}
Node curNode = head;
while (curNode.getNextNode() != oldTail){
curNode = curNode.getNextNode();
}
curNode.setNextNode(null);
tail = curNode;
length--;
return oldTail;
}
@Override
public String toString(){
String output = "";
Node curNode = head;
while (curNode != null){
output += curNode + " ---> ";
curNode = curNode.getNextNode();
}
return output;
}
}
-------------------------------------------------------------------------------
public interface LinkedListBehavior{
public void append(Node newNode);
public Node getHead();
public Node getTail();
public Node removeHead();
public Node removeTail();
public boolean isEmpty();
public int getLength();
}
-------------------------------------------------------------------
//Queue class
public class Queue {
LinkedList list;
public Queue() {
list = new
LinkedList();
}
public void enqueue(int item) {
// add item to end of
queue
Node newNode = new
Node(item);
list.append(newNode);
}
public int dequeue() {
// remove & return
item from the front of the queue
if(isEmpty()){
System.out.println("Queue is empty");
return 0;
}
Node node =
list.removeHead();
return
node.getValue();
}
public int peek() {
// return item from
front of queue without removing it
if(isEmpty()){
System.out.println("Queue is empty");
return 0;
}
Node node =
list.getHead();
return
node.getValue();
}
public boolean isEmpty() {
// return true if the
Queue is empty, otherwise false
return
list.isEmpty();
}
public int getElementCount() {
// return current total
number of elements in Queue
return
list.getLength();
}
public static void main(String[] args)
{
Queue s = new
Queue();
int[] inputArr = {1, 2,
3, 4, 5, 6};
System.out.println("\nTrying to dequeue and peek on empty
Queue:");
int test =
s.dequeue();
test = s.peek();
System.out.println("\nPopulating the Queue:");
for (int v : inputArr)
{
System.out.println("Enqueuing: " + v);
s.enqueue(v);
}
System.out.println("\nRemoving from the Queue:");
while (!s.isEmpty())
{
System.out.println("Dequeuing: " + s.dequeue());
}
System.out.println("\nTrying to dequeue and peek on empty
Queue:");
test =
s.dequeue();
test = s.peek();
}
}
=============================================== =
Output