473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Explanation

does someone can understand this function?can you explain me what do they do
one by one?thankyou

PQUEUE *pqueue_new(void)
{
PQUEUE *pq;

if ((pq = (PQUEUE *) my_malloc(sizeof(PQUEUE))) == NULL)
return (NULL);
pq->count = 0;
pq->priorities = NULL;
return pq;
}

int pqueue_insert(PQUEUE * pq, COORD coord, int wt)
{
ELIST *e;
PLIST *p, *pp;

if (pq == NULL)
return -1;

if ((e = (ELIST *) my_malloc(sizeof(ELIST))) == NULL)
return -1;
e->coord = coord;
e->next = NULL;
if (pq->priorities == NULL || pq->priorities->wt > wt) {

if ((p = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;

p->wt = wt;
p->elements = e;
p->next = pq->priorities;
pq->priorities = p;
pq->count++;

return 0;
}
pq->count++;

for (p = pq->priorities; p->next && p->next->wt <= wt; p = p->next);

if (p->wt == wt) {
e->next = p->elements;
p->elements = e;
return 0;
}
if ((pp = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;

pp->wt = wt;
pp->elements = e;
pp->next = p->next;
p->next = pp;
pq->count++;

return 0;
}

int pqueue_peekmin(PQUEUE * pq, PCOORD coord)
{
if (pq == NULL || pq->priorities == NULL || pq->priorities->elements ==
NULL)
return -1;

*coord = pq->priorities->elements->coord;

return 0;
}

int pqueue_popmin(PQUEUE * pq, PCOORD coord)
{
ELIST *tmpe;
PLIST *tmpp;

if (pqueue_peekmin(pq, coord) < 0)
return -1;

pq->count--;

tmpe = pq->priorities->elements;
pq->priorities->elements = pq->priorities->elements->next;
my_free(tmpe, sizeof(ELIST));

if (pq->priorities->elements == NULL) {
tmpp = pq->priorities;
pq->priorities = pq->priorities->next;
my_free(tmpp, sizeof(PLIST));
}
return 0;
}
Nov 14 '05 #1
2 1601
"Alessandro Zucchini" <a.********@treamail.biz> wrote:
does someone can understand this function?can you explain me what do they do
one by one?thankyou


Well, a couple of type definitions wouldn't have gone amiss, but it
looks like code for handling a priority queue. You can call pqueue_new()
to create a new, empty queue; pqueue_insert() to insert a new item with
priority wt and payload coord into a queue; and pqueue_peekmin() and
pqueue_popmin() to look at, resp. pop, the first element with maximum
priority off the queue.
Note that:
- if the queue can get large, a tree may be a better implementation than
a list;
- this higher-level interface makes it possible to switch to a tree
implementation without many changes, if any at all, to the user code,
should it become necessary, and is thus a Good Thing;
- pqueue_peekmin() and pqueue_popmin() seem misnamed to me, since they
refer to the element with the _maximum_, not minimum, priority;
- should I comment on the wisdom of casting malloc(), or writing a
malloc() wrapper which does not return a void *? Thought not.

Richard
Nov 14 '05 #2
Alessandro Zucchini wrote:
does someone can understand this function?can you explain me what do they do
one by one?thankyou

PQUEUE *pqueue_new(void)
{
PQUEUE *pq;

if ((pq = (PQUEUE *) my_malloc(sizeof(PQUEUE))) == NULL)
return (NULL);
pq->count = 0;
pq->priorities = NULL;
return pq;
}

int pqueue_insert(PQUEUE * pq, COORD coord, int wt)
{
ELIST *e;
PLIST *p, *pp;

if (pq == NULL)
return -1;

if ((e = (ELIST *) my_malloc(sizeof(ELIST))) == NULL)
return -1;
e->coord = coord;
e->next = NULL;
if (pq->priorities == NULL || pq->priorities->wt > wt) {

if ((p = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;

p->wt = wt;
p->elements = e;
p->next = pq->priorities;
pq->priorities = p;
pq->count++;

return 0;
}
pq->count++;

for (p = pq->priorities; p->next && p->next->wt <= wt; p = p->next);

if (p->wt == wt) {
e->next = p->elements;
p->elements = e;
return 0;
}
if ((pp = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;

pp->wt = wt;
pp->elements = e;
pp->next = p->next;
p->next = pp;
pq->count++;

return 0;
}

int pqueue_peekmin(PQUEUE * pq, PCOORD coord)
{
if (pq == NULL || pq->priorities == NULL || pq->priorities->elements ==
NULL)
return -1;

*coord = pq->priorities->elements->coord;

return 0;
}

int pqueue_popmin(PQUEUE * pq, PCOORD coord)
{
ELIST *tmpe;
PLIST *tmpp;

if (pqueue_peekmin(pq, coord) < 0)
return -1;

pq->count--;

tmpe = pq->priorities->elements;
pq->priorities->elements = pq->priorities->elements->next;
my_free(tmpe, sizeof(ELIST));

if (pq->priorities->elements == NULL) {
tmpp = pq->priorities;
pq->priorities = pq->priorities->next;
my_free(tmpp, sizeof(PLIST));
}
return 0;
}

I would suggest to get a good book on data structures and look out
for priority queues and what they mean. And then of course, about lists
( singly linked lists). Then come back to this code. It would be much
simple to understand this then.
--
Karthik
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Swartz | last post by:
Hi all. I'm building a development webserver (redhat-based). I'm trying to compile PHP (v4.3.4 if anyone cares) with all the features I might require in the near future. I've ran into a problem...
0
by: Premshree Pillai | last post by:
Hey, For the uninitiated: an explanation of makeExe.py (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/266471), in the form of an article, “Create Python Executables Automatically”, is...
3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
by: MatthewRoberts | last post by:
Howdy All, I have a Windows Service that often stops in its tracks with no exception and no explanation on our QA system. During testing on the development machine, it can handle any workload,...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
2
by: Dave Taylor | last post by:
Is there a decent explanation of how menu merging with MDI forms work in VB.NET? I've read through the online help and it still seems that whenever I change menus around or whatever, it breaks...
12
by: jacob navia | last post by:
Hi I am writing this tutorial stuff again in the holidays and I came across this problem: The "width" field in printf is a minimum width. Printf will not truncate a field. for instance:...
4
by: dismantle | last post by:
Hi all, this is my 3rd week in studying VB codes and i came across with this codes from a online tutorial about classes. Public Function MiddleInitial() As String MiddleInitial =...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.