2016-04-18 14 views
1

私はキューリンクリストキュー[ポップ機能]

/** 
* This function extracts from the queue the patient with maximum priority 
* @param queue is the extraction point 
* @return the patient with maximum priority or a Patient type value with all the fields 0 if the queue is empty 
*/ 

struct Patient priorityQueuePop(struct PriorityQueue *queue); 

構造体の優先度つきキューから要素をポップ機能を作成しようとしているがこれです:

struct PriorityQueue{ 
    unsigned size; 
    struct PriorityQueue *next; 
    struct PriorityQueue *front; 
    struct PriorityQueue *rear; 
} 

、患者はこれです:

enum Gender { 
    MALE = 'M', 
    FEMALE = 'F' 
}; 

struct Patient { 
    char firstName[20]; 
    char lastName[20]; 
    unsigned char age; 
    enum Gender gender; 
}; 

私はこのようなものを作ってみました

struct Patient priorityQueuePop(struct PriorityQueue *queue){ 

struct Patient *item =(struct Patient*)malloc(sizeof(struct Patient)); 

    queue->front = queue->front->next; 
    queue->size--; 

return item; 
} 

が、私はそれをコンパイルするとき、私はエラーを取得:

priorityQueue.c:72:2: error: incompatible types when returning type ‘struct Patient *’ but ‘struct Patient’ was expected
        return item;

誰かがこれを行うべきであるか嘆願を説明できますか? ありがとうございました

+2

質問はどこですか? – Boiethios

+1

構造が正しくないようです。 'PriorityQueue'構造体には、すべてのノードの頭や尾へのポインタが含まれており、' PriorityQueue'構造体と 'Patient'構造体の間には関係がありません。 – interjay

+0

エラーメッセージは自己説明可能です: 'priorityQueuePop'関数は' struct Patient'を返すと宣言しますが、 'struct Patient *'型の 'item'を返しています。 – ericbn

答えて

0

あなたのpop関数に正しい署名があるとします。

struct Patient priorityQueuePop(struct PriorityQueue *queue); 

これは、関数priorityQueuePopstruct Patientを返すべきであると述べています。これはstruct Patient *とは異なります。

struct Patient a_patient; 
struct Patient *a_pointer_to_a_patient; 

どのように構造を返すのですか?動的に割り当てる必要はありません。

struct Patient 
priorityQueuePop(struct PriorityQueue *queue) 
{ 
    struct Patient patient; 
    struct PriorityQueue *item; 

    item = queue->front 
    queue->front = queue->front->next; 
    queue->size -= 1; 
    if (queue->size == 0) { 
     queue->front = queue->rear = queue->next = NULL; 
    } 
    item->front = item->rear = item->next = NULL; 
    populatePatientFromItem(&patient, item); 
    priorityQueueDestroy(item); 

    return patient; 
} 

、それはまた、プライオリティキューであることを優先キューのCDRを考慮することが理論的に正しいですが、それはそれはあなたのインターフェイスが邪魔に定義されていない現れるので、この場合には行き過ぎであることに注意してくださいそれを利用する。

関連する問題