• Post Reply Bookmark Topic Watch Topic
  • New Topic

stack to work as queue  RSS feed

 
rekha singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,



How make stack to work as queue internally?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rekha singh wrote:
How make stack to work as queue internally?


This is a common homework question -- so it might be better if you tried to figure it out yourself first.

Hint: you need two stacks to simulate that queue.


Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rekha singh wrote:How make stack to work as queue internally?

I'm probably going to be castigated for being too purist here, but:

A stack IS a queue. It's just a LIFO queue (which is basically what 'stack' means).

I suspect that the question was something like: How do you make a Stack work like a Java (ie, FIFO) Queue - and for that, Henry is absolutely right: If you can't change anything about your stack, then you'll need a structure that holds two of them.

However (and I know I'm being pedantic), your question was: "How [to] make a stack to work as queue internally"?

Answer: Make sure it knows where its last element is, as well as its first; and make sure that pop() and peek()) work on the last element, rather than the first.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I'm probably going to be castigated for being too purist here, but:

A stack IS a queue. It's just a LIFO queue (which is basically what 'stack' means).

To be fair, it's fairly common usage to consider "queue" to mean FIFO. The Wikipedia page defines it that way, and that's what queueing theory considers a queue to be as well as far as I know. Donald Knuth's book has a section called "Stacks, Queues, and Deques".
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:To be fair, it's fairly common usage to consider "queue" to mean FIFO...

Yeah, I know: dark ages thinking. I knew somebody'd pull me up on it. It's how I was taught though; and the fact is that the API for a stack and a queue need only differ in where pop() and peek() get their information from.

And as for a Queue being a Collection...

Winston
 
rekha singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this code is correct?if any mistake correct me.


Thanks in advance
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work the way you expected it to? Instead of asking is this correct, its better to ask a more specific question like, "I expected ___ and got ___"
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I dont get your question. I thought you wanted to implement a queue. I dont see any interface or methods for it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!