我學習鏈表中C.執行我有一個問題,我下面的實現:問題返回第一個元素時
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct msg * Message;
struct msg{
int SN;
Message next;
};
Message dequeue(Message *head);
void new_msg(Message *head);
int main(){
Message M, head = NULL;
new_msg(&head);
M= dequeue(&head);
if(M != NULL) printf("Message %d has been deleted:", M->SN);
}
Message dequeue(Message *head)
{
Message m, temp;
if(*head == NULL){
return NULL;
}
else
{
temp = *head;
m = temp; // To return the message before removing it from the queue
head = head->next; // ERROR IS HERE
free(temp);
}
return (m);
}
void new_msg(Message *head){
Message m, last;
m = malloc(sizeof(struct msg));
last = *head;
srand(0);
m->SN = rand();
if (*head == NULL)
{
*head = m;
}
else
{
while (last->next != NULL)
{
last = last->next;
}
last->next = m;
}
}
我會延長我的計劃爲行動隊列需要完全出隊之前返回隊列(即頭)的第一個節點,但我不斷收到此錯誤:
/bin/sh -c 'make -j 8 -e -f Makefile'
----------Building project:[ hello - Debug ]----------
gcc -c "/Users/CFC/Library/Application Support/codelite/test/hello/main.c" -g -O0 -Wall -o ./Debug/main.c.o -I. -I.
/Users/CFC/Library/Application Support/codelite/test/hello/main.c:38:20: error: member reference base type 'Message' (aka 'struct msg *') is not a structure or union
head = head->next;
~~~~^ ~~~~
1 error generated.
make[1]: *** [Debug/main.c.o] Error 1
make: *** [All] Error 2
====1 errors, 0 warnings====
在這條線
head = head->next;
在此功能Message dequeue(Message *head)
。
請問,有人可以解釋爲什麼嗎?
謝謝。
而錯誤是? – kaylum
將'head = head-> link'更改爲'head = head-> next'。另外請注意,在'main'中,如果'dequeue'返回一個'NULL','printf'將會出錯。 –
什麼是錯誤? – immibis