# Recursive- Priority Queue

Page 1 of 1

## 3 Replies - 2995 Views - Last Post: 07 December 2008 - 10:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=75418&amp;s=c1cfd02a915ef8088a228200d13bbfe5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shannonw

Reputation: 0
• Posts: 3
• Joined: 06-December 08

# Recursive- Priority Queue

Posted 07 December 2008 - 12:29 AM

```# <stdio.h>

/* LIST DEFINED*/

typedef struct listnode{
int datum;
struct listnode * next;
}*list;

/* Random Number Generator*/

int randu(){
static int seed=17;
seed= (25173*seed + 13849)% 65536;
return seed;
}

/* Incomplete Recursive Sub-Programs*/

}

show(){
}

delete(){
}

main(){
char choice[2];
int quit=0;

while (quit !=1) {
printf("d- delete what number?\n");
printf("l-to list\n");
printf("q-quit\n");

scanf("%s", choice);

switch( choice[0]) {
case 'a':
break;

case'l':
case'L': show();
break;

case'd':
case'D': delete();
break;

case'q':
case'Q': quit()
break;
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Recursive- Priority Queue

### #2 shannonw

Reputation: 0
• Posts: 3
• Joined: 06-December 08

## Re: Recursive- Priority Queue

Posted 07 December 2008 - 12:42 AM

I'm new the forum just wondering if there is anybody that can tell me if I'm goin in the right direction with the code. I can't really figure out how to get it to work like FIFO, instead of a stack code.... All I really need the code to do is list random numbers as commanded, as well as delete them. I have everything but the three subprograms to make it all go together.

### #3 DoubleFission

Reputation: 24
• Posts: 223
• Joined: 20-September 08

## Re: Recursive- Priority Queue

Posted 07 December 2008 - 06:47 AM

NOTE:
You wont be able to use delete() as a function, it's a reserved word, so maybe change it to "deleteNode()" or something.
And Next wont be accepted as an appropriate name for the next node (Same reason) so use "nextNode" or similar

Anyways;

Your driver (Main) function looks correct so all you need to do is implement the list...

first off you need to make your queue, seeing you've already made a node structure all you have to do is declare the head of the linked list:

```listnode* Head = NULL;

```

This head pointer points to the first node in your list, which points to the second and so on...

Your Add() function will have to accommodate when your Head node points to NULL, which is done in a simple if statement. HOWEVER you have to create an instance of your node before you can actually add it. All this will be done on the heap, something like this:

```listnode* SOMETHING = NULL; // It's always a wise Idea to set pointers to NULL when you aren't using them

SOMETHING = new listnode;
SOMETHING.nextNode = NULL;

{
return;
}

// If the program gets this far it needs to search through the list (Using a current node pointer) until it finds a node that's next node points to NULL. Then you go

current.nextNode = SOMETHING;

```

That's the basics of the adding of a simple FIFO queue, where the new nodes are added into the end, however if you are going to implement a priority queue you have to implement a method that looks to see if the nextNode's data is of a lower priority of the current one you are trying to put in.

For Delete you should take from the front and then move the Head pointer to the Head.NextNode.

### #4 shannonw

Reputation: 0
• Posts: 3
• Joined: 06-December 08

## Re: Recursive- Priority Queue

Posted 07 December 2008 - 10:20 PM

Thanks for getting me in the right direction... I'm still working out the kinks defining things, and putting everything where it belongs though.. I'm havin trouble cutting and pasting from emacs, I'm having second thoughts using a random generator also.. So if you or anyone could add to my code written previously, to atleast get the proiority queue running, that'd be appreciated very much.