Question

Suppose that a minus sign in the input indicates dequeue the queue and write the return...

Suppose that a minus sign in the input indicates dequeue the queue and write the return value to standard output, and any other string indicates enqueue the string onto the queue. Further suppose that following input is processed:

it was - the - best - of times - - it was - the - - worst - of times -

a) What is written to standard output?

b) What are the contents (head to tail) left on the queue?

Homework Answers

Answer #1

Program implemented in Cpp. Answers to a) and b) are mentioned as per the implementation.

#include <iostream.h>

#include<string>

using namespace std;

#define N 200

int main()
{
   string S;
   count<<"Enter the required String: ";
   cin>>S;

   int len= S.length();
   char Q[len]; //maximum length of the queue is the length of the given string.
   int front=-1, rear=-1;
   for(int i=0; i<len; i++)
   {
       if(S[i]=='-')
       {
           if(front==-1 || rear==-1)
           {
               cout<<"\nUnderflow";
               exit(0);
           }

           Q[front]=0;

           if(front==rear)
           front=rear=-1;
           else
           front= (front+1)%len;
       }
       else
       {
           if((rear+1)%len==front)
           {
               cout<<"\nOverflow";
               exit(1);
           }

           Q[rear]=S[i];

            if(front==rear && rear==-1)
               front=rear=0;

              else
           rear=(rear+1)%len;
       }
   }

   for(int i=front; i<=rear; i=(i+1)%len)
   {
       count<<Q[i];
   }

return 0;
}   

a) Overhere, we consider the spaces in between two words. So the given input is translated as:

it_was_-_the_-_best_-_of_times_-_-_it_was_-_the_-_-_worst_-_of_times_-

, where underscore represents the spaces in the string.

Length of the queue is the max length of the string, which here is 70, front and rear are inititally set to -1 indicating an empty queue. On the insertion of the very first element, front and rear are set to 0.

So, the contents of the queue displayed onto the console as the given code is,

e__best__of_times___it_was__the___worst__of_times_

b) Content of the queue is as follows:

        e__best__of_times___it_was__the___worst__of_times_
   front                                                                              rear

,where Q[front]='e'

Q[rear]='_'

Know the answer?
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
Similar Questions
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
A Queue is a linked list with pointers to both the head of the list as...
A Queue is a linked list with pointers to both the head of the list as well as the tail (or the last element) of the list. Given a queue Q, Q.head gives the head of the queue and Q.tail gives the tail of the queue. Give O(1) time algorithms for the following tasks. Enqueue • Input: A queue Q of distinct integers and a queue element a not in Q. 1 • Output: Q with the element a added...
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the...
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Total utility can be objectively measured in numbers that indicate usefulness or benefit to the consumer....
Total utility can be objectively measured in numbers that indicate usefulness or benefit to the consumer. ____ 2. Consumers should purchase quantities of a good to the point where MU > P. ____ 3. Voluntary exchange requires that there must be mutual gain. ____ 4. Points along a budget line represent the maximum combinations of two commodities that a consumer can afford. ____ 5. The budget line represents a consumer's preferences for a commodity. ____ 6. A change in consumer...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT