【发布时间】:2020-03-15 19:25:35
【问题描述】:
我正在尝试使用结构来实现队列。这是一个结构本身:
struct Item
{
datatype data;
Item* next;
};
但是,当我尝试添加新元素时,出现错误:
void enqueue(Item** front, Item** rear, datatype D) { //add element to queue
Item* temp;
temp = new Item;
temp->data = D;
temp->next = NULL;
if (*front == NULL){ // if queue is empty, make temp the first element
*front = temp;
}
else { // else add it to the end
rear->next = temp; //error is here
*rear = temp;
}
}
我尝试过类似(*rear)->next = temp; 的操作,但接下来会保持 NULL。
您能帮忙解决这个问题吗?谢谢。
【问题讨论】: