algorithm for reversing contents in a queue

Posted on 2006-05-19
Medium Priority
Last Modified: 2008-02-26
i want a algorithm for reversing contents in a queue,i want this to be implemented in c
Question by:dinakarsoupaty
LVL 85

Accepted Solution

ozo earned 250 total points
ID: 16726417
Is it ok to say
dequeue each element from queue, push elements onto stack
pop elements off stack, enqueue elements into queue
(if you don't have an explicit stack, you can use the function call stack recursively)
LVL 53

Expert Comment

ID: 16733921
Depending on the queue implementation, this can be done by just reversing the pointers (if the queue is built up from nodes connected with pointers) ... That prevents a lot of memory copies and/or (recursive) function calls. It can be done in O(n) time.

Can you tell a bit more about your queue implementation ?

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question