Is the direction in the queue matter? - data-structures

Queue Is described as enqueued items added to the back/tail and dequeued items removed from the front/head
What about if I did the opposite
Add from the first (Add first) and remove from the tail (remove last)
Is it still a queue or do I have to stick with the first one?

Related

Constant time to access first and last node

How would you modify a linked list based queue so that first and last node can be accessed in a constant time regardless of data nodes in queue?
You will keep two pointers/reference variables, one for head of queue and other for tail. When you insert an item, you will set the tail to last inserted item, and when you remove an item, your head will obviously go to next item in queue. Since you have two variables for head and tail, it will be a constant time operation to access them.
This is the general way to create a queue with linked list itself, this will be needed to insert items and remove items from queue, nothing special here.

How to implement a stack using a priority queue?

A priority queue is used to implement a stack that stores characters.
Push(C) is used to implement Insert(Q,C,K) where K is the appropriate key chosen by the implementation.
Pop is implemented as Delete_Min(Q) ,for a sequence of operations in what order must the keys be chosen , strictly decreasing or strictly increasing ?
Let me begin by saying that priority queue and stack are two completely different data structures with different uses and applications. One can not always be used to implement the other.
Yes, there are instances where a data structure can be defined in terms of another: for example you can create a stack or queue using a linked list (quite trivially actually), however implementing a stack using a priority queue will not always work. Why?
Because a stack is first in last out. The last thing you push on a stack WILL be the first thing to pop out. A stack's sole job is to keep the order of pushed items intact and pop in the reverse order.
A priority queue however, will always give you the minimum (or maximum based on implementation) with a pop. A priority queue will have to -by definition- restructure itself to always maintain the "heap property". This means the original order in which you pushed will not necessarily be preserved.
Now, your question should be phrased as "In what situation will a priority queue and a stack behave the same way?"
You mentioned your priority queue pop() will delete the minimum value from your queue which indicates you have a min-heap at hand. In this scenario the only case where a series of pops from priority queue will resemble those that of a stack, would be when the items were all pushed in non-increasing order. It does not have to be strictly decreasing. (think about pushing all of the same values).

how to store and delete sorted items in a file

I am trying to store elements in a file in a sorted order.
The elements will be in the following format:
1 MessageA
2 MessageB
.
.
54 MessageM
68 MessageN
Each element will have a number(timestamp) & a message(size is variable).
The elements must be sorted by timestamp.
Operation allowed are insert and delete(Pop).
(Growing file size is not an issue)
and we can delete only from the lower most element(i.e. delete one after another).
Currently I have implemented it as a linked list which is very slow on inserts when the number of elements are large.
what will be the most efficient data structure to store this?
I'm not sure if you want to delete the oldest or the newest element but you should probably look into stacks and queues.
Stacks are First In Last Out, meaning that the element inserted first will be deleted (popped) last, as it would happen with a real stack, hence the name. Here the popped element would be the newest.
Queues are First In First Out. Here the deleted element (dequeued) is the oldest still present in the queue.

Implement a queue which keeps non-decreasing

As everyone knows, a standard queue supports two basic operations: insert and popout. And insert occurs at the tail of the queue, while popout occurs at the head of the queue. Here, I have no idea whether I can keep this queue ordered, like non-decreasing, based on this two operations, or perhaps with some additional help functions to achieve that goal?
You just have to change insert function a little. Every time you insert into the queue you have to find the new element place in the queue it could be achieved with following algorithm:
Use a recursive function and every time check if your new element is greater than the element in the middle of the list, if not, try to insert new element in left side of queue else try to insert in the right side of queue, do it recursively until you find the proper place for the element.
Thin algorithm for insert is O(lg(n)).
For popout you could just do what you are done now, popout the last element.

stack using two queues [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Implement Stack using Two Queues
I want the most efficient way in terms of time complexity to implement stack using two queues
I don't get why you'd ever need two queues. I've seen the answers here and in the dupe thread, and you can do it with just one queue.
From the dupe thread, this has two versions, one that optimizes push, and one that optimizes pop.
Push optimized:
push:
enqueue in queue
pop:
n = queue size.
dequeue an object, and enqueue it immediately after. Do this n - 1 times.
dequeue object, and return this.
Pop optimized:
push:
enqueue in queue
n = queue size.
dequeue an object, and enqueue it immediately after. Do this n - 1 times.
pop:
dequeue from queue and return.
Then again, I don't get why you'd ever want to do this ever. Lambast your professor for making you waste your time with pointless programming questions.
I may be wrong, but to me this does not compute.
A Queue is (typically) a FIFO structure, a Stack is a LIFO structure. I cannot off the top of my head envisage any simple combination of 2 FIFO's that will yield a LIFO, though I simply may not have had enough coffee yet today.
It may be possible, but I suspect that an implementation of a stack involving 2 queues is almost certainly going to take longer to implement and be more error prone than a simple implementation of a stack.
However, having said that...
If you already have a Queue implementation and if that Queue allows you to remove items from it's TAIL rather than from the HEAD (actual terms may differ in your implementation) then you can simply use the Queue as if it were a stack by retrieving items from the TAIL.
It is simple,
Let's say you have Queue A and Queue B
u use one queue to hold data, and other as a temporary container... BUT A and B interchange roles all the time:
When you first insert data, you insert into Queue A.
To POP the last Item you inserted, you DEQUE all of Queue A elements except the last one and ENQUEUE them in Queue B.
DEQUEUE the only element from Queue A and you've got what you want: The TOP of the stack, the last element... etc...
Now to POP the latest item, you re-do the same work but A and B interchange roles

Resources