| | |
concerns of priority queue with unsorted array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 19
Reputation:
Solved Threads: 0
hi
im trying to write some code for a priority queue using an unsorted array..it should be easy but i have a few concerns about it..
say if i have a graph of something like this:
node 0, priority 100
node 1, priority 50
node 2, priority 200
node 3, priority 250
then the array would look something like
pq[0] = 100
pq[1] = 50
pq[2] = 200
pq[3] = 250
so far so good..the index of the array corresponds to the node number and the value corresponds to the priority...
but what if i was to do a delete min operation? the array would look something like
pq[0] = 100
pq[1] = 200
pq [2] = 250
i deleted the element with lowest priority and shifted everything down to fit the array..but the problem is now pq[1] = 200, which would mean that the node number 1 has a priority of 200 however this is not the case? node 1 just got deleted, the node with priotiy 200 is node 2...
i was thinking maybe implement it using 2 arrays, 1 for storing the node numbers and the other for storing the priorities..but then again that might be doing too much
any help would be appreciated. thanks.
im trying to write some code for a priority queue using an unsorted array..it should be easy but i have a few concerns about it..
say if i have a graph of something like this:
node 0, priority 100
node 1, priority 50
node 2, priority 200
node 3, priority 250
then the array would look something like
pq[0] = 100
pq[1] = 50
pq[2] = 200
pq[3] = 250
so far so good..the index of the array corresponds to the node number and the value corresponds to the priority...
but what if i was to do a delete min operation? the array would look something like
pq[0] = 100
pq[1] = 200
pq [2] = 250
i deleted the element with lowest priority and shifted everything down to fit the array..but the problem is now pq[1] = 200, which would mean that the node number 1 has a priority of 200 however this is not the case? node 1 just got deleted, the node with priotiy 200 is node 2...
i was thinking maybe implement it using 2 arrays, 1 for storing the node numbers and the other for storing the priorities..but then again that might be doing too much
any help would be appreciated. thanks.
•
•
Join Date: May 2008
Posts: 19
Reputation:
Solved Threads: 0
0
#3 Nov 10th, 2009
no i have not, i was simply wondering if it was possible to do it with an array. I have sort of got a work around by setting the value of the priority to -1 when you try to do delete it. this way the index still corresponds to the correct node and as there are no negative priorities, the next delete min would just check for the node with lowest priority that is >0. is this ok?
![]() |
Similar Threads
- Adding a delete method to a Priority Queue (Java)
- a simple question for priority queue? (C++)
- priority queue ordering (C++)
- please help me with my homework- priority queue (C++)
- priority queue using array!!! (C++)
- priority queue (C)
- priority queue (C++)
Other Threads in the C Forum
- Previous Thread: Advanced Programming
- Next Thread: help me
Views: 270 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable feet fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft multi mysql oddnumber odf open openwebfoundation pattern pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user visualstudio voidmain() win32 win32api windows.h





