parcels.c
来自「掌握如何用C来实现各种算法」· C语言 代码 · 共 98 行
C
98 行
/*****************************************************************************
* *
* ------------------------------- parcels.c ------------------------------ *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "parcel.h"
#include "parcels.h"
#include "pqueue.h"
/*****************************************************************************
* *
* ------------------------------ get_parcel ------------------------------ *
* *
*****************************************************************************/
int get_parcel(PQueue *parcels, Parcel *parcel) {
Parcel *data;
if (pqueue_size(parcels) == 0)
/**************************************************************************
* *
* Return that there are no parcels. *
* *
**************************************************************************/
return -1;
else {
if (pqueue_extract(parcels, (void **)&data) != 0)
/***********************************************************************
* *
* Return that a parcel could not be retrieved. *
* *
***********************************************************************/
return -1;
else {
/***********************************************************************
* *
* Pass back the highest-priority parcel. *
* *
***********************************************************************/
memcpy(parcel, data, sizeof(Parcel));
free(data);
}
}
return 0;
}
/*****************************************************************************
* *
* ------------------------------ put_parcel ------------------------------ *
* *
*****************************************************************************/
int put_parcel(PQueue *parcels, const Parcel *parcel) {
Parcel *data;
/*****************************************************************************
* *
* Allocate storage for the parcel. *
* *
*****************************************************************************/
if ((data = (Parcel *)malloc(sizeof(Parcel))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the parcel into the priority queue. *
* *
*****************************************************************************/
memcpy(data, parcel, sizeof(Parcel));
if (pqueue_insert(parcels, data) != 0)
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?