Question

Data Structures & Algorithms . Solve this problem in java(not C language) . Question 1(Queue) Write...

Data Structures & Algorithms . Solve this problem in java(not C language) .

Question 1(Queue) Write a program that display list of choices for the user to interact with the program as follow:

Queue Operations Menu:

1.Add a new item

2.Delete the first item

3.Show number of items in the Queue

4.Find an item

5.Show all items

6.Exit

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

import java.util.*;

/* Class arrayQueue */

class arrayQueue

{

protected int Queue[] ;

protected int front, rear, size, len;

/* Constructor */

public arrayQueue(int n)

{

size = n;

len = 0;

Queue = new int[size];

front = -1;

rear = -1;

}

/* Function to check if queue is empty */

public boolean isEmpty()

{

return front == -1;

}

/* Function to check if queue is full */

public boolean isFull()

{

return front==0 && rear == size -1 ;

}

/* Function to get the size of the queue */

public int getSize()

{

return len ;

}

/* Function to check the front element of the queue */

public int peek()

{

if (isEmpty())

throw new NoSuchElementException("Underflow Exception");

return Queue[front];

}

/* Function to insert an element to the queue */

public void insert(int i)

{

if (rear == -1)

{

front = 0;

rear = 0;

Queue[rear] = i;

}

else if (rear + 1 >= size)

throw new IndexOutOfBoundsException("Overflow Exception");

else if ( rear + 1 < size)

Queue[++rear] = i;

len++ ;

}

/* Function to remove front element from the queue */

public int remove()

{

if (isEmpty())

throw new NoSuchElementException("Underflow Exception");

else

{

len-- ;

int ele = Queue[front];

if ( front == rear)

{

front = -1;

rear = -1;

}

else

front++;

return ele;

}

}

/* Function to display the status of the queue */

public void display()

{

System.out.print("\nQueue = ");

if (len == 0)

{

System.out.print("Empty\n");

return ;

}

for (int i = front; i <= rear; i++)

System.out.print(Queue[i]+" ");

System.out.println();

}

public int search(int val) {

for (int i = front; i <= rear; i++) {

if (Queue[i] == val) {

return i;

}

}

return -1;

}

}

/* Class QueueImplement */

public class QueueImplement

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.println("Enter Size of Integer Queue ");

int n = scan.nextInt();

/* creating object of class arrayQueue */

arrayQueue q = new arrayQueue(n);

/* Perform Queue Operations */

char ch;

while(true) {

System.out.println("\nQueue Operations");

System.out.println("1. Add an item");

System.out.println("2. Remove the first item");

System.out.println("3. Show number of items in the Queue");

System.out.println("4. Find an item");

System.out.println("5. Show all items");

System.out.println("6. Exit");

int choice = scan.nextInt();

switch (choice)

{

case 1 :

System.out.println("Enter integer element to insert");

try

{

q.insert( scan.nextInt() );

}

catch(Exception e)

{

System.out.println("Error : " +e.getMessage());

}

break;

case 2 :

try

{

System.out.println("Removed Element = "+q.remove());

}

catch(Exception e)

{

System.out.println("Error : " +e.getMessage());

}

break;

case 3 :

System.out.println("Number of items: " + q.getSize());

break;

case 4 :

System.out.println("Enter integer element to be searched");

int pos = q.search(scan.nextInt());

if (pos != -1) {

System.out.println("Element found at position: " + pos);

} else {

System.out.println("Element not found!!");

}

break;

case 5 :

q.display();

break;

case 6 :

return;

break;

default : System.out.println("Wrong Entry \n ");

break;

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
Data Structures & Algorithms . Solve this problem in java(not C language) . Question 1(Queue) Write...
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
  • Please make a JAVA program for the following using switch structures Write a program that simulates...

    Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...

  • Java Data Structures

    Programming Instructions:Using      Java Object Oriented Principles, write a program which produces the      code as indicated in the following specifications: Your       program must be a console application that provides a user this exact       menu:Please select one of the following:1: Add Client to Bank2: Display Clients in the Bank3: Set Bank Name4: Search for a Client5: Exit Enter your Selection: <keyboard input here> The       menu must be displayed repeatedly until 5...

  • please solve this question: Write a character Max-Heap Builder program in C++. The program should display...

    please solve this question: Write a character Max-Heap Builder program in C++. The program should display the menu below. Each item in the menu should be implemented in a function. a. Add a node. One node to be added to the max-heap. b. Delete a node. One node to be deleted from the max-heap. C. Search a node. Returns true if the node exists in the max-heap, otherwise it returns false. d. Print the tree. Prints the heap in level-order...

  • You will design a program to keep track of a restaurants waitlist using a queue implemented...

    You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. Add the following operations to your program: Return the first person in the queue Return the last person in the queue Add a person to the queue Delete a person from the queue Create a main program to...

  • Note: You must write this program in Python. General Requirements The program should display a menu...

    Note: You must write this program in Python. General Requirements The program should display a menu and allow the user to perform one of the following tasks. Add an item to the list Delete an item from the list Print the list Print the list in reverse Quit the program The program should run until the user chooses to quit. In Python, programmatically quitting the application is achieved with the exit() procedure. You should use a List to store the...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • Computer Science. Java programming language. Data Structures. 1) List all the nonprimitive types of data structures....

    Computer Science. Java programming language. Data Structures. 1) List all the nonprimitive types of data structures. 2) Write a simple program for each of them (in Java)

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

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

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

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