Click here to Skip to main content
13,903,133 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
To be honest, I have solve this question but to make sure I did it right, I need to ask you guys if you think I have did it right.

(a)
Explain concisely but clearly what the C++ program below does. Note that template versions of the Stack and Queue ADTs are used, but their functionality and API are otherwise the same.

void main() {
    	char inChar, stackChar, queueChar;
    	StackType<char> stack;
    	QueueType<char> queue;
    	bool check = true;
    	cout << "Enter a string and press return." << endl;
    	cin.get(inChar);
    	while (inChar != '\n') {
    		stack.Push(inChar);
    		queue.Enqueue(inChar);
    		cin.get(inChar);
    	}
    	while (check && !queue.IsEmpty()) {
    		stack.Top(stackChar); stack.Pop();
    		queue.Dequeue(queueChar);
    		if (stackChar != queueChar) check = false;
    	}
    	if (check) cout << "Yes it is!" << endl;
        else cout << "No it's not." << endl;
    }


- I think the given c++ program is checking whether the given string is palindrome are not by placing string in both stack and queue. using pop and dequeue operation get the first and last character and checking whether they are equal if not exit.

(b) Explain what performance issue the above program has, and suggest how to fix it. No code is needed; just explain briefly what you would do.

- Here the character variable stackChar and queueChar need to be initialized , because they are used for pop and dequeue functions and after these the values are to be updated with current top and first values of stack and queue.

I'll be very appreciated!

What I have tried:

Answering the questions as given above.
Posted
Updated 27-Jun-16 11:41am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You are not quite right.

The variables stackChar and queueChar don't have to be initialized. It is assumed that the values are assigned to them as you pop and dequeue data. Moreover, if you were right, it would have nothing to do with performance, rather if would mean defunct code, or, say, unreliable code, which results may depends on some random factors (such as different results for the execution under the debugger and without the debugger). This is not really the case. By the way, such issues are impossible in decent programming language (sorry, but no, I don't think C++ is decent, not even close, not even in terms of good low-level support) — the uninitialized variables either caught compilation errors, or the compiler figures out that this is not needed.

So, the summary: 1) the problem you pointed out is not a problem, 2) it would not be related to performance.

The better verdict on performance would be this: using just the array would be faster. The key here is: there is no need to use any data collection of the variable size. The user enter the test phrase at once, so your code could create the whole array at once, which removes all possible performance problems; there is no anything like reallocation; and in all other aspects, array would be the fastest. Overall, the quality of the code is… so-so…

Now, about the first part of your answer: "checking whether the given string is palindrome". It would be true if your intuitive expectation of what this code really does was true. In other works, the answer is good as soon as you don't say "strictly speaking".

But what is that code fragment, strictly speaking? Strictly speaking, this is… who knows what. Something uncertain. Why? Because there is no indication that StackType and QueueType really implement stack "the usual" generic (template) data structures "stack" and "queue", that the implementation is correct, that the functions you are using are what you expect, and so on. Strictly speaking, the name of a type or a template type carries no information on what it really does. The code is not shown, and the formulation of the problem says no word about what it is. Maybe I would not pick on that, but using the word "Type" in the name of type tells the tail; it does not look as an reasonable or even adequate developer's behavior. By the way, then names of standard C++ type implementing the stack and the queue are: std::stack and std::queue; they are the template classes:
std::stack,
queue — C++ Reference.

So, I would not trust much the software engineer who wrote "StackType" or "QueueType". :-)

—SA
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I agree with you for answer A, but for answer B.

Hint: think to what append to stack and queue structures as you add and remove data.
   
Comments
Sergey Alexandrovich Kryukov 27-Jun-16 17:42pm
   
Do you? Then pleas see Solution 2. :-)
—SA
Patrice T 27-Jun-16 17:49pm
   
Just a Quick Answer :)
Sergey Alexandrovich Kryukov 27-Jun-16 18:20pm
   
I know. Can you see my point? Anyway, your question does not explain what's wrong with B. The last sentence is related to A.
—SA
Patrice T 27-Jun-16 20:03pm
   
I was thinking about memory allocation and structure resizing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 27 Jun 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100