answersLogoWhite

0


Best Answer

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements.

However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack.

You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue.

Your queue will have the exact same elements as in the beginning, but in reverse order.

The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers.

Following is pseudocode:

Queue<Item> reverse (Queue<Item> queue) {

Stack<Item> stack;

Item item;

while (queue.remove(&item)) {

stack.push(item);

}

while(stack.pop(&item)) {

queue.add(item);

}

return queue;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

You could put elements in a stack as you remove them from the front of the queue. Then, once it's empty, add the elements to the queue as you remove them from the stack. They'll be reversed.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

how to save a queue in file in c langiage

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in c language to reverse elements of a queue?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program that outputs a given characters in reverse?

write the javascript code to display the reverse no. of given no. (e.g. 247 reverse of 742)


How do you Write a program in 'c' language which accepts int numbers from the users print its reverse number x function which return value?

question clarity


How do you Write a C program to reverse an Integer number.?

Reference:cprogramming-bd.com/c_page2.aspx# reverse number


Write the program in Linux to find the reverse of any string?

i am sam


Write a program to read your name and reverse it using arrays?

abdulrahman


Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Write an assembly language program to print a to z on screen?

write a program to print A to Z on screen in c?


Can you write program by c language in dos?

Yes.


Example of Password program code in assembly language?

How to write the program un Assembly language to set a password for personal computers?


What are the advantages and disadvantages of machine language?

AdvantageThe only advantage is that program of machine language run very fast because no translation program is required for the CPU.DisadvantagesIt is very difficult to program in machine language. The programmer has to know details of hardware to write program.The programmer has to remember a lot of codes to write a program which results in program errors.It is difficult to debug the program.


Assembly language program for string concatenation using 8086 microprocessor?

write program to concatenating two sting in 8086 assembly language