Question

In: Computer Science

Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...

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();

}

Solutions

Expert Solution

------------------------------------

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


Related Solutions

Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This program will read a String array called letters and write each word onto a new line in the file. The method should include an appropriate throws clause and should be defined within a class called TFEditor. The string should contain the following words: {“how”, “now”, “brown”, “cow”}
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT