Can I reverse a queue without using stack?
up vote
1
down vote
favorite
I have a queue with some numbers for example 5,6,7,8 is in queue, 5 is q->front and 8 is q->rear.. Can I reverse them in queue but without using stacks?
algorithm stack queue reverse
|
show 1 more comment
up vote
1
down vote
favorite
I have a queue with some numbers for example 5,6,7,8 is in queue, 5 is q->front and 8 is q->rear.. Can I reverse them in queue but without using stacks?
algorithm stack queue reverse
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
2
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a queue with some numbers for example 5,6,7,8 is in queue, 5 is q->front and 8 is q->rear.. Can I reverse them in queue but without using stacks?
algorithm stack queue reverse
I have a queue with some numbers for example 5,6,7,8 is in queue, 5 is q->front and 8 is q->rear.. Can I reverse them in queue but without using stacks?
algorithm stack queue reverse
algorithm stack queue reverse
edited Dec 16 '17 at 13:11
asked Dec 14 '17 at 23:42
Orkhan Salahov
194
194
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
2
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09
|
show 1 more comment
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
2
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
2
2
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Of course! But it won't be as efficient.
Using a stack: O(N) time. O(1) memory.
Using an associative array: O(N) time. O(1) memory.
Using a fixed-size array: O(N) time. O(N) memory.
Using an extendable array: O(N) time. O(N) memory.
Using a queue: O(N^2) time. O(1) memory.
Using an associative array will use more time than using a stack, but the performance and memory usage of both will scale identically.
The following snippets show how each of those data structures can be used.
Stack:
# O(N) time. O(1) memory.
sub reverse_queue_using_stack {
my ($in_q) = @_;
my $stack = Stack->new();
while ( my ($item) = $in_q->dequeue() ) {
$stack->push($item);
}
my $out_q = Queue->new();
while ( my ($item) = $stack->pop() ) {
$out_q->enqueue($item);
}
return $out_q;
}
Associative array:
# O(N) time. O(1) memory.
sub reverse_queue_using_dict {
my ($in_q) = @_;
my $dict = Dictionary->new();
my $i = 0;
while ( my ($item) = $in_q->dequeue() ) {
$dict->set($i++, $item);
}
my $out_q = Queue->new();
while ($i--) {
$out_q->enqueue($dict->delete($i));
}
return $out_q;
}
Fixed-size array (and a queue if you can't get the size of a queue):
# O(N) time. O(N) memory.
sub reverse_queue_using_array {
my ($in_q) = @_;
my $count = 0;
my $queue = Queue->new();
while ( my ($item) = $in_q->dequeue() ) {
++$count;
$queue->enqueue($item);
}
my $array = Array->new($count);
for my $i (0..$count-1) {
$array->set($i, $queue->dequeue());
}
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($array->get($i));
}
return $out_q;
}
Extendable array:
# O(N) time. O(N) memory.
sub reverse_queue_using_list {
my ($in_q) = @_;
my $list = List->new();
while ( my ($item) = $in_q->dequeue() ) {
$list->append($item);
}
my $count = $list->size();
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($list->get($i));
}
return $out_q;
}
Queue:
# O(N^2) time. O(1) memory.
sub reverse_queue_using_queue {
my ($in_q) = @_;
my $queue = Queue->new();
my $out_q = Queue->new();
while (1) {
my ($tail) = $in_q->dequeue()
or last;
while ( my ($item) = $in_q->dequeue() ) {
$queue->enqueue($tail);
$tail = $item;
}
$out_q->enqueue($tail);
($in_q, $queue) = ($queue, $in_q);
}
return $out_q;
}
Tested using the following harness:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
# The implementation of these don't matter;
# if the problem can be solved using only the methods provided by these classes,
# the problem can be solved using any implementation of these classes.
{
package Queue;
sub new { my $class = shift; bless(, $class) }
sub enqueue { my $self = shift; push @$self, @_; }
sub dequeue { my $self = shift; @$self ? shift(@$self) : () }
}
{
package Stack;
sub new { my $class = shift; bless(, $class) }
sub push { my $self = shift; push @$self, @_; }
sub pop { my $self = shift; @$self ? pop(@$self) : () }
}
{
package Array; # Fixed-size array.
use Carp qw( croak );
sub new { my ($class, $size) = @_; bless([ (undef) x $size ], $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] = $item; }
}
{
package List; # Extendable array.
use Carp qw( croak );
sub new { my $class = shift; bless(, $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0; $self->[$i] = $item; }
sub append { my ($self, $item) = @_; push @$self, $item; }
}
{
package Dictionary;
use Carp qw( croak );
sub new { my $class = shift; bless({}, $class) }
sub get { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); $self->{$k} }
sub set { my ($self, $k, $item) = @_; $self->{$k} = $item; }
sub exists { my ($self, $k) = @_; exists($self->{$k}) }
sub delete { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); delete($self->{$k}) }
}
sub purge_queue {
my ($q) = @_;
my @vals;
while ( my ($item) = $q->dequeue() ) {
push @vals, $item;
}
return @vals;
}
# ...
for my $reverse_func_name (qw(
reverse_queue_using_stack
reverse_queue_using_dict
reverse_queue_using_array
reverse_queue_using_list
reverse_queue_using_queue
)) {
my $reverse_func = &$reverse_func_name;
my $in_q = Queue->new();
$in_q->enqueue($_) for 'a'..'j';
my $out_q = $reverse_func->($in_q);
say sprintf "%-26s %s", "$reverse_func_name:", join ' ', purge_queue($out_q);
}
add a comment |
up vote
0
down vote
You could use the recursive stack to reverse a queue internally:
#include <queue>
void reverseQueue(queue<int> &q) {
if (!q.empty()) {
int front = q.front();
q.pop();
reverseQueue(q);
q.push(front);
}
}
source
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47823796%2fcan-i-reverse-a-queue-without-using-stack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Of course! But it won't be as efficient.
Using a stack: O(N) time. O(1) memory.
Using an associative array: O(N) time. O(1) memory.
Using a fixed-size array: O(N) time. O(N) memory.
Using an extendable array: O(N) time. O(N) memory.
Using a queue: O(N^2) time. O(1) memory.
Using an associative array will use more time than using a stack, but the performance and memory usage of both will scale identically.
The following snippets show how each of those data structures can be used.
Stack:
# O(N) time. O(1) memory.
sub reverse_queue_using_stack {
my ($in_q) = @_;
my $stack = Stack->new();
while ( my ($item) = $in_q->dequeue() ) {
$stack->push($item);
}
my $out_q = Queue->new();
while ( my ($item) = $stack->pop() ) {
$out_q->enqueue($item);
}
return $out_q;
}
Associative array:
# O(N) time. O(1) memory.
sub reverse_queue_using_dict {
my ($in_q) = @_;
my $dict = Dictionary->new();
my $i = 0;
while ( my ($item) = $in_q->dequeue() ) {
$dict->set($i++, $item);
}
my $out_q = Queue->new();
while ($i--) {
$out_q->enqueue($dict->delete($i));
}
return $out_q;
}
Fixed-size array (and a queue if you can't get the size of a queue):
# O(N) time. O(N) memory.
sub reverse_queue_using_array {
my ($in_q) = @_;
my $count = 0;
my $queue = Queue->new();
while ( my ($item) = $in_q->dequeue() ) {
++$count;
$queue->enqueue($item);
}
my $array = Array->new($count);
for my $i (0..$count-1) {
$array->set($i, $queue->dequeue());
}
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($array->get($i));
}
return $out_q;
}
Extendable array:
# O(N) time. O(N) memory.
sub reverse_queue_using_list {
my ($in_q) = @_;
my $list = List->new();
while ( my ($item) = $in_q->dequeue() ) {
$list->append($item);
}
my $count = $list->size();
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($list->get($i));
}
return $out_q;
}
Queue:
# O(N^2) time. O(1) memory.
sub reverse_queue_using_queue {
my ($in_q) = @_;
my $queue = Queue->new();
my $out_q = Queue->new();
while (1) {
my ($tail) = $in_q->dequeue()
or last;
while ( my ($item) = $in_q->dequeue() ) {
$queue->enqueue($tail);
$tail = $item;
}
$out_q->enqueue($tail);
($in_q, $queue) = ($queue, $in_q);
}
return $out_q;
}
Tested using the following harness:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
# The implementation of these don't matter;
# if the problem can be solved using only the methods provided by these classes,
# the problem can be solved using any implementation of these classes.
{
package Queue;
sub new { my $class = shift; bless(, $class) }
sub enqueue { my $self = shift; push @$self, @_; }
sub dequeue { my $self = shift; @$self ? shift(@$self) : () }
}
{
package Stack;
sub new { my $class = shift; bless(, $class) }
sub push { my $self = shift; push @$self, @_; }
sub pop { my $self = shift; @$self ? pop(@$self) : () }
}
{
package Array; # Fixed-size array.
use Carp qw( croak );
sub new { my ($class, $size) = @_; bless([ (undef) x $size ], $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] = $item; }
}
{
package List; # Extendable array.
use Carp qw( croak );
sub new { my $class = shift; bless(, $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0; $self->[$i] = $item; }
sub append { my ($self, $item) = @_; push @$self, $item; }
}
{
package Dictionary;
use Carp qw( croak );
sub new { my $class = shift; bless({}, $class) }
sub get { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); $self->{$k} }
sub set { my ($self, $k, $item) = @_; $self->{$k} = $item; }
sub exists { my ($self, $k) = @_; exists($self->{$k}) }
sub delete { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); delete($self->{$k}) }
}
sub purge_queue {
my ($q) = @_;
my @vals;
while ( my ($item) = $q->dequeue() ) {
push @vals, $item;
}
return @vals;
}
# ...
for my $reverse_func_name (qw(
reverse_queue_using_stack
reverse_queue_using_dict
reverse_queue_using_array
reverse_queue_using_list
reverse_queue_using_queue
)) {
my $reverse_func = &$reverse_func_name;
my $in_q = Queue->new();
$in_q->enqueue($_) for 'a'..'j';
my $out_q = $reverse_func->($in_q);
say sprintf "%-26s %s", "$reverse_func_name:", join ' ', purge_queue($out_q);
}
add a comment |
up vote
0
down vote
accepted
Of course! But it won't be as efficient.
Using a stack: O(N) time. O(1) memory.
Using an associative array: O(N) time. O(1) memory.
Using a fixed-size array: O(N) time. O(N) memory.
Using an extendable array: O(N) time. O(N) memory.
Using a queue: O(N^2) time. O(1) memory.
Using an associative array will use more time than using a stack, but the performance and memory usage of both will scale identically.
The following snippets show how each of those data structures can be used.
Stack:
# O(N) time. O(1) memory.
sub reverse_queue_using_stack {
my ($in_q) = @_;
my $stack = Stack->new();
while ( my ($item) = $in_q->dequeue() ) {
$stack->push($item);
}
my $out_q = Queue->new();
while ( my ($item) = $stack->pop() ) {
$out_q->enqueue($item);
}
return $out_q;
}
Associative array:
# O(N) time. O(1) memory.
sub reverse_queue_using_dict {
my ($in_q) = @_;
my $dict = Dictionary->new();
my $i = 0;
while ( my ($item) = $in_q->dequeue() ) {
$dict->set($i++, $item);
}
my $out_q = Queue->new();
while ($i--) {
$out_q->enqueue($dict->delete($i));
}
return $out_q;
}
Fixed-size array (and a queue if you can't get the size of a queue):
# O(N) time. O(N) memory.
sub reverse_queue_using_array {
my ($in_q) = @_;
my $count = 0;
my $queue = Queue->new();
while ( my ($item) = $in_q->dequeue() ) {
++$count;
$queue->enqueue($item);
}
my $array = Array->new($count);
for my $i (0..$count-1) {
$array->set($i, $queue->dequeue());
}
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($array->get($i));
}
return $out_q;
}
Extendable array:
# O(N) time. O(N) memory.
sub reverse_queue_using_list {
my ($in_q) = @_;
my $list = List->new();
while ( my ($item) = $in_q->dequeue() ) {
$list->append($item);
}
my $count = $list->size();
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($list->get($i));
}
return $out_q;
}
Queue:
# O(N^2) time. O(1) memory.
sub reverse_queue_using_queue {
my ($in_q) = @_;
my $queue = Queue->new();
my $out_q = Queue->new();
while (1) {
my ($tail) = $in_q->dequeue()
or last;
while ( my ($item) = $in_q->dequeue() ) {
$queue->enqueue($tail);
$tail = $item;
}
$out_q->enqueue($tail);
($in_q, $queue) = ($queue, $in_q);
}
return $out_q;
}
Tested using the following harness:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
# The implementation of these don't matter;
# if the problem can be solved using only the methods provided by these classes,
# the problem can be solved using any implementation of these classes.
{
package Queue;
sub new { my $class = shift; bless(, $class) }
sub enqueue { my $self = shift; push @$self, @_; }
sub dequeue { my $self = shift; @$self ? shift(@$self) : () }
}
{
package Stack;
sub new { my $class = shift; bless(, $class) }
sub push { my $self = shift; push @$self, @_; }
sub pop { my $self = shift; @$self ? pop(@$self) : () }
}
{
package Array; # Fixed-size array.
use Carp qw( croak );
sub new { my ($class, $size) = @_; bless([ (undef) x $size ], $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] = $item; }
}
{
package List; # Extendable array.
use Carp qw( croak );
sub new { my $class = shift; bless(, $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0; $self->[$i] = $item; }
sub append { my ($self, $item) = @_; push @$self, $item; }
}
{
package Dictionary;
use Carp qw( croak );
sub new { my $class = shift; bless({}, $class) }
sub get { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); $self->{$k} }
sub set { my ($self, $k, $item) = @_; $self->{$k} = $item; }
sub exists { my ($self, $k) = @_; exists($self->{$k}) }
sub delete { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); delete($self->{$k}) }
}
sub purge_queue {
my ($q) = @_;
my @vals;
while ( my ($item) = $q->dequeue() ) {
push @vals, $item;
}
return @vals;
}
# ...
for my $reverse_func_name (qw(
reverse_queue_using_stack
reverse_queue_using_dict
reverse_queue_using_array
reverse_queue_using_list
reverse_queue_using_queue
)) {
my $reverse_func = &$reverse_func_name;
my $in_q = Queue->new();
$in_q->enqueue($_) for 'a'..'j';
my $out_q = $reverse_func->($in_q);
say sprintf "%-26s %s", "$reverse_func_name:", join ' ', purge_queue($out_q);
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Of course! But it won't be as efficient.
Using a stack: O(N) time. O(1) memory.
Using an associative array: O(N) time. O(1) memory.
Using a fixed-size array: O(N) time. O(N) memory.
Using an extendable array: O(N) time. O(N) memory.
Using a queue: O(N^2) time. O(1) memory.
Using an associative array will use more time than using a stack, but the performance and memory usage of both will scale identically.
The following snippets show how each of those data structures can be used.
Stack:
# O(N) time. O(1) memory.
sub reverse_queue_using_stack {
my ($in_q) = @_;
my $stack = Stack->new();
while ( my ($item) = $in_q->dequeue() ) {
$stack->push($item);
}
my $out_q = Queue->new();
while ( my ($item) = $stack->pop() ) {
$out_q->enqueue($item);
}
return $out_q;
}
Associative array:
# O(N) time. O(1) memory.
sub reverse_queue_using_dict {
my ($in_q) = @_;
my $dict = Dictionary->new();
my $i = 0;
while ( my ($item) = $in_q->dequeue() ) {
$dict->set($i++, $item);
}
my $out_q = Queue->new();
while ($i--) {
$out_q->enqueue($dict->delete($i));
}
return $out_q;
}
Fixed-size array (and a queue if you can't get the size of a queue):
# O(N) time. O(N) memory.
sub reverse_queue_using_array {
my ($in_q) = @_;
my $count = 0;
my $queue = Queue->new();
while ( my ($item) = $in_q->dequeue() ) {
++$count;
$queue->enqueue($item);
}
my $array = Array->new($count);
for my $i (0..$count-1) {
$array->set($i, $queue->dequeue());
}
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($array->get($i));
}
return $out_q;
}
Extendable array:
# O(N) time. O(N) memory.
sub reverse_queue_using_list {
my ($in_q) = @_;
my $list = List->new();
while ( my ($item) = $in_q->dequeue() ) {
$list->append($item);
}
my $count = $list->size();
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($list->get($i));
}
return $out_q;
}
Queue:
# O(N^2) time. O(1) memory.
sub reverse_queue_using_queue {
my ($in_q) = @_;
my $queue = Queue->new();
my $out_q = Queue->new();
while (1) {
my ($tail) = $in_q->dequeue()
or last;
while ( my ($item) = $in_q->dequeue() ) {
$queue->enqueue($tail);
$tail = $item;
}
$out_q->enqueue($tail);
($in_q, $queue) = ($queue, $in_q);
}
return $out_q;
}
Tested using the following harness:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
# The implementation of these don't matter;
# if the problem can be solved using only the methods provided by these classes,
# the problem can be solved using any implementation of these classes.
{
package Queue;
sub new { my $class = shift; bless(, $class) }
sub enqueue { my $self = shift; push @$self, @_; }
sub dequeue { my $self = shift; @$self ? shift(@$self) : () }
}
{
package Stack;
sub new { my $class = shift; bless(, $class) }
sub push { my $self = shift; push @$self, @_; }
sub pop { my $self = shift; @$self ? pop(@$self) : () }
}
{
package Array; # Fixed-size array.
use Carp qw( croak );
sub new { my ($class, $size) = @_; bless([ (undef) x $size ], $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] = $item; }
}
{
package List; # Extendable array.
use Carp qw( croak );
sub new { my $class = shift; bless(, $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0; $self->[$i] = $item; }
sub append { my ($self, $item) = @_; push @$self, $item; }
}
{
package Dictionary;
use Carp qw( croak );
sub new { my $class = shift; bless({}, $class) }
sub get { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); $self->{$k} }
sub set { my ($self, $k, $item) = @_; $self->{$k} = $item; }
sub exists { my ($self, $k) = @_; exists($self->{$k}) }
sub delete { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); delete($self->{$k}) }
}
sub purge_queue {
my ($q) = @_;
my @vals;
while ( my ($item) = $q->dequeue() ) {
push @vals, $item;
}
return @vals;
}
# ...
for my $reverse_func_name (qw(
reverse_queue_using_stack
reverse_queue_using_dict
reverse_queue_using_array
reverse_queue_using_list
reverse_queue_using_queue
)) {
my $reverse_func = &$reverse_func_name;
my $in_q = Queue->new();
$in_q->enqueue($_) for 'a'..'j';
my $out_q = $reverse_func->($in_q);
say sprintf "%-26s %s", "$reverse_func_name:", join ' ', purge_queue($out_q);
}
Of course! But it won't be as efficient.
Using a stack: O(N) time. O(1) memory.
Using an associative array: O(N) time. O(1) memory.
Using a fixed-size array: O(N) time. O(N) memory.
Using an extendable array: O(N) time. O(N) memory.
Using a queue: O(N^2) time. O(1) memory.
Using an associative array will use more time than using a stack, but the performance and memory usage of both will scale identically.
The following snippets show how each of those data structures can be used.
Stack:
# O(N) time. O(1) memory.
sub reverse_queue_using_stack {
my ($in_q) = @_;
my $stack = Stack->new();
while ( my ($item) = $in_q->dequeue() ) {
$stack->push($item);
}
my $out_q = Queue->new();
while ( my ($item) = $stack->pop() ) {
$out_q->enqueue($item);
}
return $out_q;
}
Associative array:
# O(N) time. O(1) memory.
sub reverse_queue_using_dict {
my ($in_q) = @_;
my $dict = Dictionary->new();
my $i = 0;
while ( my ($item) = $in_q->dequeue() ) {
$dict->set($i++, $item);
}
my $out_q = Queue->new();
while ($i--) {
$out_q->enqueue($dict->delete($i));
}
return $out_q;
}
Fixed-size array (and a queue if you can't get the size of a queue):
# O(N) time. O(N) memory.
sub reverse_queue_using_array {
my ($in_q) = @_;
my $count = 0;
my $queue = Queue->new();
while ( my ($item) = $in_q->dequeue() ) {
++$count;
$queue->enqueue($item);
}
my $array = Array->new($count);
for my $i (0..$count-1) {
$array->set($i, $queue->dequeue());
}
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($array->get($i));
}
return $out_q;
}
Extendable array:
# O(N) time. O(N) memory.
sub reverse_queue_using_list {
my ($in_q) = @_;
my $list = List->new();
while ( my ($item) = $in_q->dequeue() ) {
$list->append($item);
}
my $count = $list->size();
my $out_q = Queue->new();
for (1..$count) {
my $i = $count - $_;
$out_q->enqueue($list->get($i));
}
return $out_q;
}
Queue:
# O(N^2) time. O(1) memory.
sub reverse_queue_using_queue {
my ($in_q) = @_;
my $queue = Queue->new();
my $out_q = Queue->new();
while (1) {
my ($tail) = $in_q->dequeue()
or last;
while ( my ($item) = $in_q->dequeue() ) {
$queue->enqueue($tail);
$tail = $item;
}
$out_q->enqueue($tail);
($in_q, $queue) = ($queue, $in_q);
}
return $out_q;
}
Tested using the following harness:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
# The implementation of these don't matter;
# if the problem can be solved using only the methods provided by these classes,
# the problem can be solved using any implementation of these classes.
{
package Queue;
sub new { my $class = shift; bless(, $class) }
sub enqueue { my $self = shift; push @$self, @_; }
sub dequeue { my $self = shift; @$self ? shift(@$self) : () }
}
{
package Stack;
sub new { my $class = shift; bless(, $class) }
sub push { my $self = shift; push @$self, @_; }
sub pop { my $self = shift; @$self ? pop(@$self) : () }
}
{
package Array; # Fixed-size array.
use Carp qw( croak );
sub new { my ($class, $size) = @_; bless([ (undef) x $size ], $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0 || $i>=@$self; $self->[$i] = $item; }
}
{
package List; # Extendable array.
use Carp qw( croak );
sub new { my $class = shift; bless(, $class) }
sub size { my $self = shift; 0+@$self }
sub get { my ($self, $i) = @_; croak "!" if $i<0; $self->[$i] }
sub set { my ($self, $i, $item) = @_; croak "!" if $i<0; $self->[$i] = $item; }
sub append { my ($self, $item) = @_; push @$self, $item; }
}
{
package Dictionary;
use Carp qw( croak );
sub new { my $class = shift; bless({}, $class) }
sub get { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); $self->{$k} }
sub set { my ($self, $k, $item) = @_; $self->{$k} = $item; }
sub exists { my ($self, $k) = @_; exists($self->{$k}) }
sub delete { my ($self, $k) = @_; croak "!" if !exists($self->{$k}); delete($self->{$k}) }
}
sub purge_queue {
my ($q) = @_;
my @vals;
while ( my ($item) = $q->dequeue() ) {
push @vals, $item;
}
return @vals;
}
# ...
for my $reverse_func_name (qw(
reverse_queue_using_stack
reverse_queue_using_dict
reverse_queue_using_array
reverse_queue_using_list
reverse_queue_using_queue
)) {
my $reverse_func = &$reverse_func_name;
my $in_q = Queue->new();
$in_q->enqueue($_) for 'a'..'j';
my $out_q = $reverse_func->($in_q);
say sprintf "%-26s %s", "$reverse_func_name:", join ' ', purge_queue($out_q);
}
edited Dec 15 '17 at 16:35
answered Dec 15 '17 at 1:28
ikegami
260k11174394
260k11174394
add a comment |
add a comment |
up vote
0
down vote
You could use the recursive stack to reverse a queue internally:
#include <queue>
void reverseQueue(queue<int> &q) {
if (!q.empty()) {
int front = q.front();
q.pop();
reverseQueue(q);
q.push(front);
}
}
source
add a comment |
up vote
0
down vote
You could use the recursive stack to reverse a queue internally:
#include <queue>
void reverseQueue(queue<int> &q) {
if (!q.empty()) {
int front = q.front();
q.pop();
reverseQueue(q);
q.push(front);
}
}
source
add a comment |
up vote
0
down vote
up vote
0
down vote
You could use the recursive stack to reverse a queue internally:
#include <queue>
void reverseQueue(queue<int> &q) {
if (!q.empty()) {
int front = q.front();
q.pop();
reverseQueue(q);
q.push(front);
}
}
source
You could use the recursive stack to reverse a queue internally:
#include <queue>
void reverseQueue(queue<int> &q) {
if (!q.empty()) {
int front = q.front();
q.pop();
reverseQueue(q);
q.push(front);
}
}
source
answered Nov 22 at 16:52
Prabhsimran Singh
167
167
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47823796%2fcan-i-reverse-a-queue-without-using-stack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Pretty easy to reverse these using a temporary variable.
– tadman
Dec 14 '17 at 23:47
Can you describe a little bit and also is it better way to do or using stack is better?
– Orkhan Salahov
Dec 14 '17 at 23:49
Assuming the queue interface is FIFO, you'd need to pop elements from the queue into an array, linked-list, or stack, in order to then recreate the queue in reverse order.
– rcgldr
Dec 14 '17 at 23:51
You should put some code in here that demonstrates the type of queue you're using and whatever limitations it might have.
– tadman
Dec 14 '17 at 23:55
2
I vote to close this, because it is far to broad without a context. It all depends on how the queue is implemented and how the general environment looks like.
– Broman
Dec 15 '17 at 0:09