473,487 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

priority queue by stack(( have code that I am strugling to write ))

3 New Member
I have code that I am strugling to write could u plz help me with it ...I 'll be thankfull
Implement a Priority Queue using Stacks. I am not allowed to use any other data structure. Example:

Class PriorityQueue

{
Stack PQ;


….
}
I should implement the following interface:

Public void insert (double data, int priority) {}
Public double remove () { }
Public boolean is-empty() { }
Public double peekmin() {}
Public int size () {}
Public remove (int n) { } // remove the element number n
Nov 26 '06 #1
2 2812
Ganon11
3,652 Recognized Expert Specialist
Implementing a Queue using stacks!? That's just craziness. A Stack is a LIFO structure - last in first out, while a Queue is a FIFO structure - first in first out. They're completely different.

I guess the biggest thing to do is insert. In order to do that, you'd take the following steps:

1) Declare an array of stack nodes with size equal to the stack's size.
2) From 0 to size - 1, pop each node of the stack and set the nodeArray at the position to the node. For example:

Expand|Select|Wrap|Line Numbers
  1. nodeArray[0] = PQ.pop();
  2. nodeArray[1] = PQ.pop();
Of course, you'd use a for... loop for this.

3) The stack is now empty. Add the node given to the function as a parameter to the stack.
4) Finally, traverse your node array backwards, inserting each node onto the stack again.

That would be a generic queue insert function - doing it this way means that the function to get the first node in the queue would be the same as the stack.pop() function.

Of course, I'm not all too familiar with priority queues - you may have to insert the extra node somewhere in the array based on its priority. That would have to happen between steps 2 and 3.

Good luck!
Nov 26 '06 #2
i am lost
3 New Member
THank u for helpin me I know it is totally diffrient but my doctor wrote it ^_^
Nov 28 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

38
5729
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
5
13200
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
16
5367
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
4
10339
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
6
9006
by: imutate | last post by:
How do you use std::priority_queue to store say an index type (call it intidx) and sort using another integer type (but smallest weight at the top) ? How do you pop off the top index ? I mean...
3
4345
by: Chris Lasher | last post by:
Hello, I am working with large graphs (~150,000 to 500,000 nodes) which I need decompose node-by-node, in order of a node's value. A node's value is determined by the sum of its edge weights....
3
7394
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
1
3948
by: operatingsystem | last post by:
I need to generate such scheduler in operating system subject which satisfy below conditions...may i request u if anybody knows abt this plz help me out in this..topics.. Scheduler...
14
16920
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.