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
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...
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....
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
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