A Priority Queue

13 篇文章 0 订阅

下面是实现优先队列的C代码,其中一定的很多不足,希望大家多多指教!如果大家有什么好的编程规范也可以告诉我,谢谢!

/**
*******************************************************************************
*   
*   @frief    	implement a simple priority queue
*   @version	$1.0$
*   @date	    $2012.9.11$
*   @author     $Alfred Wang$
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define NUM 10
#define NEGATIVE_INF -9999
/**
*******************************************************************************
*   
*   @brief    	get the index of parent to the given leaf
*   @param	    leaf: Input; the index of the leaf
*   @param	    return: Output; return the index of leaf's parent
*
*******************************************************************************/
int Parent(int leaf)
{
    assert(leaf >= 0);
	return (leaf - 1) / 2;
}

/**
*******************************************************************************
*   
*   @brief    	get the index of left child to the given parent
*   @param	    parent: Input; the index of the parent
*   @param	    return: Output; return the index of left child
*
*******************************************************************************/
int LChild(int parent)
{
    assert(parent >= 0);
	return 2 * parent + 1;
}

/**
*******************************************************************************
*   
*   @brief    	get the index of right child to the given parent
*   @param	    parent: Input; the index of the parent
*   @param	    return: Output; return the index of right child
*
*******************************************************************************/
int RChild(int parent)
{
    assert(parent >= 0);
	return 2 * (parent + 1);
}

/**
*******************************************************************************
*   
*   @brief    	exchange two numbers *tempPtr1 and *tempPtr2
*   @param	    tempPtr1: Input; pointer points to the number needed to exchange
*   @param	    tempPtr2: Input; pointer points to the number needed to exchange
*
*******************************************************************************/
void Swap(int *tempPtr1, int *tempPtr2)
{
	int temp;
	temp = *tempPtr1;
	*tempPtr1 = *tempPtr2;
	*tempPtr2 = temp;
}

/**
*******************************************************************************
*   
*   @brief    	adjust max heap in the premise that its subtrees are max heap
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    node: Iutput; the node needs to be adjust
*   @param	    size: Iutput; the size of the array
*
*******************************************************************************/
void AdjustMaxHeap(int *arrPtr, int node, int size)
{
    assert(arrPtr != NULL && size >= 1);
	int largest;
	int lChild = LChild(node);
	int rChild = RChild(node);
	if(lChild < size && arrPtr[node] < arrPtr[lChild])
	{
	    largest = lChild;
	}
	else
	{
	    largest = node;
	}
	if(rChild < size && arrPtr[largest] < arrPtr[rChild])
	{
	    largest = rChild;
	}
	if(node != largest)
	{
	    Swap(&arrPtr[node], &arrPtr[largest]);
		AdjustMaxHeap(arrPtr, largest, size);
	}
}

/**
*******************************************************************************
*   
*   @brief    	build a max heap
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    node: Iutput; the node needs to be adjust
*   @param	    size: Iutput; the size of the array
*
*******************************************************************************/
void BuildMaxHeap(int *arrPtr, int size)
{
    assert(arrPtr != NULL && size >= 1);
	int i;
	for(i = size / 2 - 1; i >= 0; i--)
	{
		AdjustMaxHeap(arrPtr, i, size);
	}
}

/**
*******************************************************************************
*   
*   @brief    	get max value of the priority
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    return: Output; return the max value of the priority queue 
*
*******************************************************************************/
int PQMax(int *arrPtr)
{
	assert(arrPtr != NULL);
	return *arrPtr;
}

/**
*******************************************************************************
*   
*   @brief    	get the max value and delete it from the array
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    size: Iutput; the size of the array
*   @param	    return: output; the node needs to be adjust
*
*******************************************************************************/
int ExtractMax(int *arrPtr, int *sizePtr)
{
    assert(arrPtr != NULL && *sizePtr >= 1);
	int max = *arrPtr;
    *arrPtr = *(arrPtr + *sizePtr - 1);
	(*sizePtr)--;
	AdjustMaxHeap(arrPtr, 0, *sizePtr);
	return max;
}

/**
*******************************************************************************
*   
*   @brief    	increase the given number to key
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    size: Iutput; the size of the array
*   @param	    node: Iuput; the index-specific
*   @param	    key: Input; to which the given number should be increased
*   @param	    return: Onput; return the current location of the original number 
*
*******************************************************************************/
int IncreaseToKey(int *arrPtr, int node, int size, int key)
{
	assert(arrPtr != NULL && size >= 1);
	assert(*(arrPtr + node) <= key);
	*(arrPtr + node) = key;
	while(node > 0 && *(arrPtr + Parent(node)) < *(arrPtr + node))
	{
		Swap((arrPtr + Parent(node)), (arrPtr + node));
		node = Parent(node);
	}
	return node;
}

/**
*******************************************************************************
*   
*   @brief    	insert a number named key
*   @param	    arrPtr: Input; the pointer points to the array 
*   @param	    size: Iutput; the size of the array
*   @param	    key: Input; to which the given number should be increased
*   @param	    return: Onput; return the current location of the original number 
*
*******************************************************************************/
int Insert(int *arrPtr, int *sizePtr, int key)
{
    assert(arrPtr != NULL && *sizePtr >= 1);
	(*sizePtr)++;
	*(arrPtr + *sizePtr - 1) = NEGATIVE_INF;
	IncreaseToKey(arrPtr, *sizePtr - 1, *sizePtr, key);
}

int main(void)
{
	int a[NUM] = { 2,4,8,23,37,-23,1,2,10,6};  
	int index;
	int size = NUM;
	int max1;
	int max2;
	int location1;
	int location2;
	int key1 = 34;
	int key2 = 21;
	/****************************/
	
	BuildMaxHeap(a, size);
	
	/****************************/
	
	max1 = PQMax(a);
	for(index = 0; index < size; index++)
	{
		printf("%d ", a[index]);
	}
	printf("\n");
	printf("max1 = %d\n", max1);
	
	/****************************/
	
	max2 = ExtractMax(a, &size);
	for(index = 0; index < size; index++)
	{
		printf("%d ", a[index]);
	}
	printf("\n");
	printf("max2 = %d\n", max2);
	
	/****************************/
	
	location1 = IncreaseToKey(a, 3, size, key1);
	for(index = 0; index < size; index++)
	{
		printf("%d ", a[index]);
	}
	printf("\n");
	printf("location1 = %d\n", location1);
	
	/****************************/
	
	location2 = Insert(a, &size, key2);
	for(index = 0; index < size; index++)
	{
		printf("%d ", a[index]);
	}
	printf("\n");
	printf("location2 = %d\n", location2);
	
	/****************************/
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值