Queue and Stack Program
Algorithm to Interleave the first half of the queue with second half
My Encoding Algorithm.
Following are the steps used to encode a list of words:
1. Push the first half elements of queue to stack.
2. Enqueue back the stack elements.
3. Dequeue the first half elements of the queue and enqueue them back.
4. Again push the first half elements into the stack.
5. Interleave the elements of queue and stack.
Interleave the first half of the queue with second half (ie encode)
Given a queue of integers of even length,
rearrange the elements by interleaving the first half of the queue with the second half of the queue.
The data forthis problem, fill the queue with the values 1 to 30. The algorithm given above.
Only a stack (one stack) a queue (one queue) can be used as an auxiliary space.
Input : 1 2 3 4
Output : 1 3 2 4
Input : 11 12 13 14 15 16 17 18 19 20
Output : 11 16 12 17 13 18 14 19 15 20
Number TWO (You are to decode the input)
I have an encoded data file that I need decode using the above algorithm.
The algorithm I used to encode the data is shown above.
Your problem is to decode the encoded data.
The input will be a ‘…txt’ file of words which consists of a list of words that have been encoded as described above.
Write a program to input the txt file and print out the decoded results. Input a string and enqueue to your string queue to start with.
Restrictions: You must use aggregate Stack and Queue class. To solve this problem, you can have ONE Queue and ONE Stack and only one of each.
The input data will be loaded into a Queue to start with and from the Queue start your algorithm to decode the text. (Decoding as you read in your data will not be acceptable.) This is an exercise in Stacks, Queues and aggregate classes.
loop cin >> str; yourqueue.enqueue(str); endloop
endloop CS yourqueue.enqueue(str); 372 str; Program >>
Assignment cin Queue loop Program acceptable.) Interleave be the not first will half data of your the in queue read with you second as half (Decoding My text.
Encoding the Algorithm decode Following to are algorithm the your steps start used Queue to the encode from a and list with of start words:
to 1. Queue Push a the into first loaded half be elements will of data queue input to The stack. each.
2. of Enqueue one back only the and stack Stack elements. ONE
3. and Dequeue Queue the ONE first have half can elements you of problem, the this queue solve and To enqueue class. them Queue back. and
4. Stack Again aggregate push use the must first You half Restrictions: elements above. into described the as stack. encoded
5. been Interleave have the that elements words of of queue list and a stack. of Your consists problem which is ‘wordlist.txt’ to be decode will the input encoded The data.