Question

JAVA based programming assignment that will Implement a linked-based queue, where items 1, 2 and 3...

JAVA based programming assignment that will Implement a linked-based queue, where items 1, 2 and 3 are equeued, followed by a dequeue operation. Print the dequeued item. Note: item1 51 item2 52 item3 53.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class LinkedQueue {

    static class Node {
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }
    }

    private Node head;
    private Node tail;

    public LinkedQueue() {
        head = null;
        tail = null;
    }

    public void enqueue(int num) {
        Node n = new Node(num);
        if (tail == null) {
            tail = n;
            head = n;
        } else {
            tail.next = n;
            tail = n;
        }
    }

    public int dequeue() {
        int n = head.data;
        head = head.next;
        if (head == null) {
            tail = null;
        }
        return n;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public static void main(String[] args) {
        LinkedQueue queue = new LinkedQueue();
        queue.enqueue(51);
        queue.enqueue(52);
        queue.enqueue(53);
        System.out.println("Queue is: " + queue.dequeue() + " " + queue.dequeue() + " " + queue.dequeue());
    }
}

Queue is: 51 52 53 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
JAVA based programming assignment that will Implement a linked-based queue, where items 1, 2 and 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA 1.         Implement an array-based queue that enqueus 3 items and prints them, dequeues one element...

    JAVA 1.         Implement an array-based queue that enqueus 3 items and prints them, dequeues one element and print it, and prints the front and rear items. Sample output: 1 enqueued 2 enqueued 3 enqueued 1 dequeued 2 is front item 3 is rear item

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • 2. Short programming assignment. Implement a bottom-up mergesort that first sorts blocks of M elements using...

    2. Short programming assignment. Implement a bottom-up mergesort that first sorts blocks of M elements using insertion sort. Test your program with large sets of random data and determine what value of M works best. 3. The following refer to linked list mergesort Explain why mergesort is more suitable for linked lists than other sorting methods. a. Explain why mergesort is more suitable for linked lists than other b. Consider a file that is “mostly sorted.” Explain how the principles...

  • 1. What would happen if you execute the following code just before traversing a linked list?...

    1. What would happen if you execute the following code just before traversing a linked list? head.setNext(head); 2. The linked list that follows represents a queue. If we dequeue once, what item is dequeued? (7, Ajay, NFL)à(3, Sarah, Mario)à(9, Jim, Golf)àhead (5, Joe, Sonic)ànull tail 3. The linked list that follows represents a stack. After we push the player (5,Joe,Sonic) onto the stack, what are the first and last items on the stack? (7, Ajay, NFL)à(3, Sarah, Mario)à(9, Jim, Golf)ànull...

  • ALGORITHM Given the following Knapsack problem instance and its DP solution: 1 2 3 4 5...

    ALGORITHM Given the following Knapsack problem instance and its DP solution: 1 2 3 4 5 weight value To 10 10 10 10 item1 1 10 item2 | 2 17 item3 11 11 21 21 28 28 item4 15 0 11 121 121 128 36 According to the solution table, the maximum item value that we can achieve 36. By reconstructing the solution, we know that the following items {1,3,4} are included in the solution. Carefully, fill in the following...

  • On Java: Problems Problem 1. (Deque) Hints: my Use a doubly-linked list Node to implement the...

    On Java: Problems Problem 1. (Deque) Hints: my Use a doubly-linked list Node to implement the API — each node in the list stores a generic item, and references next and prev to the next and previous nodes in the list null itemi → item2 item3 ... itemn null Instance variables wy Reference to the front of the deque, Node first my Reference to the back of the deque, Node last my Size of the deque, int n + LinkedDeque...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT