Recursive- Priority Queue

I trying to code a priority queue list, that can add, delete and list

Page 1 of 1

3 Replies - 1741 Views - Last Post: 07 December 2008 - 10:20 PM Rate Topic: -----

#1 shannonw  Icon User is offline

  • New D.I.C Head

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

Recursive- Priority Queue

Post icon  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*/

add(){
}

show(){
}

delete(){
}

/* Main Program with menu*/

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

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

   scanf("%s", choice);
   
   switch( choice[0]) {
	 case 'a':
	 case 'A': add();
	 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 DoubleFission  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • 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;

if(Head == NULL)
{
Head = SOMETHING;
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.
Was This Post Helpful? 0
  • +
  • -

#4 shannonw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1