Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be creating a FIFO (First In First Out) data structure a queue. In our queue new data enters at the head, and old

You will be creating a FIFO (First In First Out) data structure a queue. In our queue new data enters at the head, and old data leaves at the tail.

Page 402: "*10.10 (The Queue class) Section 10.6 gives a class for Stack. Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retrieved in a last-in first-out fashion. In a queue, the elements are retrieved in a first-in first-out fashion. The class contains:"

Your queue must accept the following input from standard in:

integer if queue not full, add number to queue

d..dequeue print and removes tail item

The word d..dequeue implies the command may have only a d, but can have up to the entire word dequeue.

You don't have to check for words like Deqq, but you can have d deq dx or DeQ etc., which are all just dequeues.

s..size return number of items on queue

e..empty return true if queue is empty

f..full return true if query is full, we only allow 8 customers to wait

? or p..print print current queue contents with head and tail pointers, this should be your toString method

q..quit to quit - you also should quit on EOF.

For all commands just check the first letter of the word, e.g., s, ss, and size all mean size. q, quk, Qu, and QQ all mean q

Input data is white space delimited, aka standard free form data.

NetBeans Sample Run 1 "1 3 s Size ? 4 full d d S e d e d ?" :

run: Size: 2 Size: 2 Queue can hold: 8 It has 2 elements head--> 3, 1-->tail Full: false Dequeue: 1 Dequeue: 3 Size: 1 Empty: false Dequeue: 4 Empty: true Dequeue: -1 Queue can hold: 8 It has 0 elements head--> -->tail BUILD SUCCESSFUL (total time: 1 second)

String data = "1 3 s Size ? 4 full d d S e d e d ?";

NetBeans Sample Run 2: (User enters: 20 30 -100 ? d 55 ? abc q )

run:

20 30 -100 ? Queue can hold: 8 It has 3 elements head--> -100, 30, 20-->tail d Dequeue: 20 55 ? Queue can hold: 8 It has 3 elements head--> 55, -100, 30-->tail abc Unknown command->abc<- q BUILD SUCCESSFUL (total time: 23 seconds)

Sample run String data="= "100 200 300 ? 400 2 3 200 3 full size d ?"; run: Queue can hold: 8 It has 3 elements head--> 300, 200, 100-->tail Full: true Size: 8 Dequeue: 100 Queue can hold: 8 It has 7 elements head--> 3, 200, 3, 2, 400, 300, 200-->tail BUILD SUCCESSFUL (total time: 23 seconds) 

Hints:

You are implementing a queue using a static array, i.e., int[] data = new int[ CAPACITY ];

Use a count variable (aka size) to keep track of number of elements in queue; Just use mod division on head and tail.

For example, To add and element, aka enqueue, you would just increment then mod divide an index pointer. We increment to go to the next element, the mod division will take us from the last element back to the first. A queue should join last to first array element.

You may have to sketch out the array on a piece of paper to understand whats happening

Also quit program on q or EOF.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions