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