| | |
Help with Queues and Stacks please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 27
Reputation:
Solved Threads: 0
Hey guys.
I need help with these questions please
1. Write a function that takes as a parameter a queue of integers and returns a copy of the queue with all the elements stored in reverse order. Use a stack as a helper data structure.
For example, after creating a queue in the following way and calling the function:
then the resulting rQueue has 1 at the back, 2 in the middle, and 3 at the front.
2. Write a function that reverses a string by making use of a stack. Your function could push elements on the stack one by one, and then build up the result by popping them off the stack.
Any help would be appreciated.
Thanks
I need help with these questions please

1. Write a function that takes as a parameter a queue of integers and returns a copy of the queue with all the elements stored in reverse order. Use a stack as a helper data structure.
C++ Syntax (Toggle Plain Text)
queue<int> reverseQueue(queue<int> q) // pre: none // post: returns a queue with the elements stored in reverse order
For example, after creating a queue in the following way and calling the function:
C++ Syntax (Toggle Plain Text)
queue<int> myQueue, rQueue; myQueue.push(1); myQueue.push(2); myQueue.push(3); rQueue = reverseQueue(myQueue);
then the resulting rQueue has 1 at the back, 2 in the middle, and 3 at the front.
2. Write a function that reverses a string by making use of a stack. Your function could push elements on the stack one by one, and then build up the result by popping them off the stack.
C++ Syntax (Toggle Plain Text)
string reverseString(string s)
Any help would be appreciated.
Thanks
prob 1.
Remove all the elements of the queue and push it in the stack.
Now pop from the stack one element at a time and add it to the new queue until the stack is empty. Now return the new queue.
prob 2.
the answer is there in your question itself. Just do as its said.
***IMP : Dont expect readymade code in here.
If u want them then u r in the wrong place.
Remove all the elements of the queue and push it in the stack.
Now pop from the stack one element at a time and add it to the new queue until the stack is empty. Now return the new queue.
prob 2.
the answer is there in your question itself. Just do as its said.
***IMP : Dont expect readymade code in here.
If u want them then u r in the wrong place.
Use the push function to add each element of the string onto the stack, then use the pop function to remove each element. You can print each value after popping, then move onto the next element until the stack is empty.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
![]() |
Similar Threads
- Sudoku Mini grid help! (C++)
- how to allocate x number of queues? (C++)
- java projects (Java)
- How did you learn C++? (C++)
- Stacks and Queues (C++)
- Data Structures??? (C++)
- Need help on order queuing program (C++)
- introduction (Community Introductions)
- Beyond C++ console program.. (C++)
Other Threads in the C++ Forum
- Previous Thread: Reading an outside file mortgage calculator
- Next Thread: Family Age Statistics Program
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





