Sequence to insert an elements in stacks and queues - data-structures

Does the sequence to insert an element really matters in stacks or queues,i mean to push or to enqueue the data either at start or at the end given we are popping from top in a stack and dequeue from the head in a queue.........Forgive me if this has been already asked.
I hope the question is clear.Thanks in advance.

Check this picture
A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. So yeah this is a data structures not a open source project, sequence matters.
Same goes for Queue

Related

Container of fixed size where items exist within it according to their demand

I'm trying to implement a container with the following characteristics:
The container has a fixed size n.
When an item is inserted into the container, if the item is in the data structure it will be moved to the front of it. If not it will be inserted to the front of the data structure, but the last item at the back of the container will be removed to respect the fixed size n.
Building on for 2, it will be required to check whether an item exists in this container in order to know whether to insert or move an item in the container.
The reasoning behind this container, is to keep frequently accessed items in the container. The cost of inserting a new item into the container is large thus it is in my interest to keep it in the container for as long as it is in demand.
Is there a container/data structure that exists that achieves something similar to what I've described? If not can you provide any advice on how to implement it? I'm using C++ but any examples or pseudocode will be equally appreciated.
Edit:
I suppose what I need is a kind of queue with no duplicate items. The queue needs to be searched to see if an item exists within it, and if so moves it to the front of the queue. A fixed size isn't that difficult to adhere to (just check the size before insertion and if it will go over remove the last item in the queue). Basically this post but not allowing any duplicates in the container, and also fast search capabilities to check if an item is within it.
I'm not following the requirements you gave but this seems like it can be implemented as as a double-ended queue (C++ deque or Java Deque). Each time an element is accessed implies a linear search (can't be avoided), then this element is moved to the front (constant time), and the last element removed (also constant time). This should result that the most frequently accessed elements move to the front of the queue over time, decreasing the real-time cost of a linear search.
A double-ended queue can be implemented as a ring-buffer or as a doubly-linked-list. Since you stated a fixed number of elements, the ring buffer seems like the better option.
However, I can't vouch for the implementations of C++ or Java deque.. you may look at the source code to see if its backed as an array or a linked node structure.
Maybe wrap a priority queue with elements having a last-accessed-time attribute?
You may check Splay Tree. If you do some operation on element X, that element move to root.

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).

Unique Queue Overflow Condition (having less elements than its size)

Got this competitive question:
Which of the following data structure may give overflow error, even though the current number of elements in it is less than its size?
a. Stack
b. Circular queue
c. Simple queue
d. None of the above
I tried to Google the answer for a proper explanation however several sources marked (c) and (b) as answers which confused me even more. What's the explanation and the proper answer?
Thanks.
This question seems somewhat strange because if you implement any of these structures correctly, there will be no such premature overflow.
With that in mind, Circular queue does seem like the most sensible answer. Here's why:
Note: In my explanation, the queue adds to back and removes from front
After certain number of insertions/deletions, the pointers to front and back in the circular queue (implemented as an array) may be on either side of each other.
This means that when adding elements to this queue, on top of standard checks, we also have to be aware of the relative position of the front and back pointers.
In the second picture above we have to realize that adding to back has to add to the beginning of the array now, since back is at the end of the array. In other words, adding elements has to "circle around". If we don't implement this check properly, we would end up with an overflow even though there's still room in the queue.
Ans is (c)simple queue.we are assuming that elements can be inserted using rear and can be deleted using front pointer..And also assume that maximum size of queue is 10(eg.) ....Now if the rear is point at index no 9...Means there are total 10 elements in queue. And the position of front is 0 index .Now if we remove elements from other end then the value of front become 1 .........here, actually queue is not full bcz index 0 is empty... But due to the condition of the rear pointer which is max-1 the output shows that queue is full....And the sol of that is implemented in circular queue.

Implement a priority queue using only ONE stack

Implementing a queue or a priority queue using two stacks are not hard.
Now the questions are
How about using only ONE stack to implement a priority queue?
How about using only ONE stack to implement a normal queue?
Are they even possible?
p.s. of course you should use constant extra space other than ONE stack if necessary
No, it's not possible using only methods provided through a stack interface (i.e. using only push and pop methods) with constant extra space. [1]
Consider trying to simulate a queue using a stack.
When enqueueing, if we simply push onto the stack, we'll end up with another element we need to do something with to get to the front of the queue for a dequeue. It's easy to see that a bunch of enqueues will make it impossible for the next dequeue to take a constant amount of space, as all these enqueued elements need to be popped to get to the front of the queue. We could also put the enqueued element a constant number of elements from the top of the stack, but this doesn't really help much either - the elements below it will need to be dequeued first, so we run into the same problem. Trying to put the enqueued element further than a constant number of elements from the top of the stack will of course take more than a constant amount of space already.
Now consider a priority queue where each new item has lower priority than all items already in the queue. This is synonymous to a simply queue, which, from the above argument, can't be simulated using a single stack with constant space.
[1]: If the stack was implemented as an array or linked-list, as it typically is, it would of course be possible using the functionality for those, but I'm sure that's not what you're asking.
Say you push n elements onto your stack. Now you want the element at the bottom of it (if you want to implement a queue). You're going to need extra O(n) space to keep your other n-1 elements so you can access the bottom one.
=> Under your constraints and only the stack interface methods, there's no way you're going to implement a queue using a single stack and constant space.
It is impossible to perform the task if you only allow standard stack operations (pop, push), as queueing an item would mean poping all the items from the stack, pushing the new item, and then push back all the items. If you use constant time, you can't pop all the items and keep track of them with O(1) memory.
Let's assume you implement a stack with a two-way linked list and allow a reverse method- meaning you flip the order of the stack (tail of the linked list becomes the tail).
Now, for a regular queue, you can perform:
queue- perform reverse, push the new item, and reverse again.
dequeue- perform pop.
Note once you allow a reverse action, it's not a standard stack anymore.
A priority queue is still impossible impossible to implement even if you allow reverse, as you'll still have to perform various comparisons when queueing or dequeueing, and will still encounter the problem of having no space to store the items you want to compare.
Here's a solution that is technically correct, but almost certainly useless for your purposes.
We use a stack S of queues (regular or priority). We initialise S to hold a single empty queue. For any queue operation, we peek at the top element of S and apply the queue operation to it in place. This way, S will only ever hold a single element.
This uses zero memory outside of the stack, if you take the viewpoint that the stack "owns" the memory of all its elements.

Design a stack that can also dequeue in O(1) amortized time?

I have an abstract data type that can be viewed as a list stored left to right, with the following possible operations:
Push: add a new item to the left end of the list
Pop: remove the item on the left end of the list
Pull: remove the item on the right end of the list
Implement this using three stacks and constant additional memory, so that the amortized time for any push, pop, or pull operation is constant. The stacks have basic operations, isEmpty, Push, and Pop.
Amortized time means "If I spend this amount of time, I can spend another block of it and store it in a bank of time to be used later." like for each push operation, spend three blocks of constant time, so for every element pushed, you have 2 extra blocks of constant time.
Making a few assumptions:
That this is homework
That this paragraph is part of the assignment
Implement this using three stacks and
constant additional memory, so that
the amortized time for any push, pop,
or pull operation is constant. The
stacks have basic operations, isEmpty,
Push, and Pop.
Then my first advice would be to ignore the people talking to you about linked lists. True, that's how any reasonable person would implement it without the three stack requirement, but the key factor in homework isn't to do it the way a reasonable person would, but rather how your instructor wants you to.
My second bit of advice would be to get some blocks, a deck of cards, or a bunch of styrofoam cups and designate three stacks (e.g. with coasters or something). Start playing around with what happens when you transfer the contents of one stack to another, and that should give you an idea.
You could do something which uses only the 3 stacks. Think tower of Hanoi.
Use a doubly-linked list and keep pointers to the head and tail. For the rest, you need to write your own code first and then let us help you correct it.
Start by implementing a queue in terms of two stacks (a pretty standard problem) and generalize.

Resources