Priority queue's

This is a discussion on Priority queue's within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a program that creates a 'priority queue'. I'ts something from datastructures,i've no clue how too ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Priority queue's

    Hi,

    I have to write a program that creates a 'priority queue'.
    I'ts something from datastructures,i've no clue how too explain that here.
    Just hopeing my code makes some sense.
    I'ts not compiling im getting a lot of errors.
    thanks

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    /*prototype statements*/
    void buildHeap(int *array,int SIZE);
    int insertHeap(int *array,int *last,int data);
    void reheapUp(int *array,int index);
    int getPriority(void);
    int getData(void);
    int array[SIZE];
    
    int main(void){
       
       int i,data,priority,ans;
       buildHeap(array,SIZE);
       for(i=0,i<=SIZE;i++){
         priority = getPriority(void);
    	 data = getData(void);
         ans = insertHeap(array,&i,priority);
       }
    }
    void buildHeap(int *array,int SIZE){
    	int walker = 1;
    	loop(walker <= SIZE){
    		reheapUp(array,walker);
    		walker++;
    	}
    }
    int insertHeap(int *array,int *last,int data){
    	int index;
    	if (*last >= SIZE){
    		return 0;
    	}
        index = *last++;
    	array[index] = data;
    	reheapUp(array,index);
    	return 1;
    }
    void reheapUp(int *array,int index){
    	int parent,hold;
    	if(index){
    		parent = (index - 1)/ 2;
    		if(array[index] > array[parent]){
    			hold = array[parent];    
    			array[parent] = array[index];
    			array[index] = hold;
    			reheapUp(array,parent);
    		}
    	}
    }
    
    int getpriority(void){
    	int pri;
    	printf("Enter the priority");
    	scanf("%d",&pri);
    	return pri;
    }
    int getData(void){
    	int dat;
    	printf("Enter the data");
    	scanf("%c",&dat);
    	return dat;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'ts not compiling im getting a lot of errors.
    Really? I don't see any. How about you post the first few of them?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    for instance, in the prototype statement ; void buildHeap(int *array,int SIZE);
    it says error C2059 syntax error: ')'

    thanks for the help

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    /*prototype statements*/
    void buildHeap(int *array,int SIZE);
    After preprocessing, this would be:
    Code:
    void buildHeap(int *array,int 10);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well WTF is loop()?
    Code:
    	loop(walker <= SIZE)
    Is that supposed to be while(walker <= SIZE)?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Quote Originally Posted by itsme86
    Well WTF is loop()?
    Code:
    	loop(walker <= SIZE)
    Is that supposed to be while(walker <= SIZE)?
    yes I allready changed that, it was because I had too rewrite algorithms in C

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Quote Originally Posted by Dave_Sinkula
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    /*prototype statements*/
    void buildHeap(int *array,int SIZE);
    After preprocessing, this would be:
    Code:
    void buildHeap(int *array,int 10);
    So how should I do that , use the define here ?

  8. #8
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    SIZE is just a #define, so it's visible in the whole file. That means you don't have to pass the second arg at all, but it's usually better to do so. Just change the name of the variable in the prototype and function:
    Code:
    void buildHeap(int *array,int sz); /* sz or whatever you want */
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. priority inversion due to pthread_mutex_lock(): how to avoid?
    By mynickmynick in forum C Programming
    Replies: 11
    Last Post: 04-07-2009, 10:34 AM
  2. crazy pipe/fork
    By fortune2k in forum C Programming
    Replies: 8
    Last Post: 03-13-2009, 11:28 AM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 11:48 PM
  4. priority list won't work, i give up
    By teamster in forum C Programming
    Replies: 8
    Last Post: 10-19-2006, 12:26 PM
  5. BST implementation of priority queues
    By jcleong in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 09:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21