I need help with the programming segments below, so I can study for them...I showed my effort on trying to do them. However, I don't believe they are right. If someone can assist me with this, I will definitely appreciate it.


1. Determine if a word is a palindrome or not using queues

q.addQueue(v);

for (int i = q.front(); i < q.front(); i--)
{
for (int j = q.back(); i<q.back(); i++
{
if ( q.front() == q.back() )
{
cout<<"It is a palindrome"<<endl;
}
else 
{
cout<<"It is not a palindrome"<<endl;
}
}
}

2. Return the last item of a queue leaving the queue unchanged

if (!p.QueueIsEmpty() )
{
p.deleteQueue(v);
}
p.addQueue(v);
cout<<v;

3. Replace every occurence of every (e) in a queue with an (x)

if (!s.QueueIsEmpty)
{
for (int i=q.front(); i<q.front(); i--)
{
for (int j=q.back(); i<q.back(); i++)
{
if (q.front() == 'e') || (q.back() == 'e')
{
q.addQueue(x);
}
}
}

4. Give the sum of the 1st 3 elements in a queue leaving the queue unchanged

if (q.QueueIsEmpty())
{
for (i = q.front(); i<q.front()-3; i--)
{
q.DeleteQueue(v)
s.AddQueue(v)
sum+=val;
}
for (int i =0; i<3; i++)
{
s.DeleteQueue(v);
q.AddQueue(v);
}
else
{
cout<<"It is empty"<<endl;
}
cout<<"Sum is "<<sum;

5. Reverse the elements in a queue

while (!s.QueueIsEmpty())
{
p.DeleteQueue(v);
}
p.AddQueue(v);
cout<<v<<" ";

6. Exchange the front 2 elements of a queue

p.DeleteQueue(v);
p.DeleteQueue(e);
p.AddQueue(v);
p.AddQueue(e);

Recommended Answers

All 3 Replies

We don't have the definition of your queue class so can't really tell if some of those are right or wrong.

2. Probably wrong. You are not supposed to delete the item from the queue. Just retrieve it. There is probably another way to get the item out of the queue without deleting it.

3. Definitely wrong. Just think a minute about what you have done with those loops. You first initialize variable i to be q.front() and then test to see if i is less and q.front() ??? Most likely should be this? for(int i = q.front(); i < q.back(); i++) What I don't know if what kind of data is in the queue. Is it a queue of strings (most likely answer considering the question), a queue of ints or what??

4. Similar answer as given for #2, above.

5 & 6. I have no idea since I don't know the definition of the queue class.

I am not using a queue class for these program segments. I apologize for any confusion. They are just programming segments made up to help me understand how to do them. On my test tomorrow, I have to write program segments "similar" to those that I put, and I just wanted to make sure I am prepared. They are basically just used for practice, I guess like pseudo code. Sorry for any confusion

An example of a program segment that I did that were correct:

Swap top 2 elements in a stack

s.pop(v);
s.pop(e);
s.push(v);
s.push(e);

Duplicate top element in a stack

s.pop(v);
s.push(v);
s.push(v);

Print the elements in the stack from top to bottom

while (!s.isempty())
{
s.pop(v);
cout<<v;
}

Did my above post help? =/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.