我嘗試使用鏈接列表來執行隊列的實現,但總是出現錯誤:賦值和賦值中的不兼容類型使得整型指針沒有強制轉換。c - 使用鏈表執行隊列
這裏是我的代碼:
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"
struct QUEUE_ELEMENT{
int element;
struct QUEUE_ELEMENT *next;
};
int size;
struct QUEUE_ELEMENT *head, *tail;
void initQueue(){
head = NULL;
tail = NULL;
size = 0;
} // void initQueue()
int queueEmpty(void) {
return (head == NULL && tail == NULL);
} // int queueEmpty(void)
int enqueue(QUEUE_ELEMENT e) {
struct QUEUE_ELEMENT *temp;
if (tail == NULL){
tail -> next = NULL;
tail -> element = e;
head = tail;
}
else {
temp = malloc(sizeof(QUEUE_ELEMENT));
tail -> next = temp;
temp -> element = e;
temp -> next = NULL;
tail = temp;
}
return size++;
} // int enqueue(QUEUE_ELEMENT e)
int dequeue(QUEUE_ELEMENT *e){
struct QUEUE_ELEMENT *temp;
temp = malloc(sizeof(QUEUE_ELEMENT));
if (queueEmpty() != 0){
temp = head;
if(temp -> next != NULL){
temp = temp -> next;
free(head);
head = temp;
}
else{
free(head);
head = NULL;
tail = NULL;
}
}
return size--;
} // int dequeue(QUEUE_ELEMENT *e)
我修改我的代碼了很多。
爲什麼'tail - > element = e''在enqueue()中發生錯誤'賦值中的不兼容類型'?我該如何解決它?
其中???發佈實際的編譯器錯誤 – yamafontes
具有'size','head','tail',*和*'next'的「節點」看起來不是一個好設計。 – user2864740
這個代碼有太多的錯誤,它讓我的頭部受傷...爲什麼你需要這麼多的指針來建立一個簡單的鏈表? – littleadv