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

  • Problem Description to implement a Java application, called ShoppingApplication, that can be used in a retail...

    Problem Description to implement a Java application, called ShoppingApplication, that can be used in a retail store. You are asked to implement three classes: Item, Invoice, and InvoiceDriver. Each of these classes is described below. Item class The Item class represents of an item that is being sold in the retail store (e.g., book or pencil) where an item is identified by three instance variables: name (of type Sring), weight (of type double), price (of type double), and currentDiscount (of...

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • Implement an array-based Linked List in Java. Use double as the item. You need to create...

    Implement an array-based Linked List in Java. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE your language's Library List. You will receive zero points. Write a LinkedList class Write a driver (tester) call LinkListedDriver to show you have...

  • Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

    Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...

  • (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...

  • 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...

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