Glad you're back for another great week!
Today's problem is about combining two common data structures in to one. Your goal is to implement a data structure that can pop and push like a stack and also dequeue and enqueue like a queue. Here is an example of how your data structure should work.
store.push(5) > [5] store.enqueue(6) > [5,6] store.push(7) > [7,5,6] store.pop() > 7 store.dequeue() > 5
Note: pop and dequeue will essentially do the same thing since they always pull from the front of the line. As always, bonus points for efficiency!
Comments:
Kevin Benton - 10 years, 8 months ago
reply permalink
Sanket Apte - 10 years, 8 months ago
import java.util.LinkedList; import java.util.List;
public class StackQueue<T> {
}
reply permalink
klm - 10 years, 8 months ago
Sanket, shouldn't pop and dequeue return the same object. The object that is at the front of the queue or at the top of the stack (however you like to look at it)?
reply permalink
Sanket Apte - 10 years, 8 months ago
Yes. My bad. I ended up into combining functionality of stack and queue into one data structure. I just missed out a part of problem that both functions pop and dequeue should remove from front of the line.
reply permalink
Sanket Apte - 10 years, 8 months ago
I'll refactor and post it again. Thaks klm. :-)
reply permalink
Jt - 10 years, 8 months ago
C#, not sure why, but it seemed to me that using a list or an array would be cheating somehow. Thus the string, so it's not very efficient, but it works.
reply permalink
Jt - 10 years, 8 months ago
Sorry, I didn't like that answer after I posted it. So... refactor time. Let's take advantage of built in classes.
reply permalink
Bumbleguppy - 10 years, 8 months ago
javascript, adhering to the spirit of the problem, I tried to re-invent the native array functions from scratch.
reply permalink
Hueho - 10 years, 8 months ago
"c suxx"
I couldn't do it with efficiency (using a simple double-linked list with head and tail), so I did it with a terrible gimmick: generic stacks/queues!
(Also I called it a deque, but I'm pretty sure it's not an actual deque, since it only allow to get elements from the end of the list)
Behold:
The most amazing thing: no warnings even with -Wall and -pedantic. :D
reply permalink
Anonymous - 10 years, 8 months ago
Oh man this looks pretty intense
reply permalink
Pegleg - 10 years, 8 months ago
Python how you so sexy?
reply permalink
Mre64 - 10 years, 6 months ago
/* Mre64 6/12/14
*/ import java.util.LinkedList;
public class Struct {
} /* Mre64 6/12/14
*/ import java.util.ArrayList; import java.util.LinkedList;
public class StackQueues extends Struct {
}
reply permalink
Mre64 - 10 years, 6 months ago
Edit comments would be great...
reply permalink