Постановка в очередь на C с использованием реализации связанного списка

#c #queue

#c #очередь

Вопрос:

Я получаю прокомментированные ошибки в этой функции при использовании enqueue в следующем коде: я не могу их разрешить

 void enqueue (tweet ** head, tweet ** tail, tweet * node)
{
    tweet * p = (tweet*)malloc(sizeof(tweet));
    p->id = 10000;
    strcpy(p->text,"Hi");
    strcpy(p->user, "Me");
    p->next = NULL;

    if(isEmpty(*head))
    {
        head = tail = p;  // incompatible pointer type error here
    }
    else
    {
        tail->next = p;  /*error: '*tail' is a pointer; did you mean to use '->'?, when I try to use '->' 
                           still it shows this error*/
        tail = p; // incompatible pointer type error here
    }
}
 

Это заголовочный файл, который я включил:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct microtweet
{
    int id;                       //non-unique integer value
    char user [51];               // the username / userid of the person who wrote the tweet
    char text [141];              // the text of the tweet
    struct microtweet *next;      //dynamic connection to the next tweet
}
tweet;
tweet *head;
tweet *tail;
void enqueue (tweet ** head, tweet ** tail, tweet * node);
int isEmpty (tweet * head);
 

Комментарии:

1. Примечание: вместо того, чтобы использовать два двойных указателя, я бы создал структуру «списка» : typedef struct { tweet *head; tweet *tail; } list_t; . Затем измените на: void enqueue(list_t *list,tweet *node) и передайте указатель структуры списка на все / большинство функций (например int isEmpty(const list_t *list) { return (list->head == NULL); } )

Ответ №1:

Проблема в том, что head и tail имеют тип tweet ** , в то время p как имеют тип tweet * . Вы можете исправить это, изменив head на *head и tail на *tail .

Первое исправление:

 // head = tail = p; <-- incorrect
*head = *tail = p;
 

Второе исправление:

 // tail->next = p; <-- incorrect
(*tail)->next = p;
 

Третье исправление:

 // tail = p; <-- incorrect
*tail = p;