怎么初始化一个只有头结点和尾指针的链队啊,这样可以吗?

struct LNode
{
    int data;
    LNode *next:
   };


struct LinkQueue
{
    LNode * rear;
};
void InitQueue(LinkQueue Q)
{
    LNode *H;
    *H=new LNode;
    Q.rear=H;
    Q.rear->next=Q.rear;
}

*H=new LNode;
这里不要星号

有点语法错误,我帮你改一下

struct LNode {
    int data;
    LNode *next;
};

struct LinkQueue {
    LNode *front; // add front pointer to keep track of the front of the queue
    LNode *rear;
};

void InitQueue(LinkQueue& Q) { // add reference to LinkQueue to modify it
    Q.front = Q.rear = new LNode; // allocate memory for front and rear
    Q.front->next = nullptr; // set front's next to nullptr
}


不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

解决了,谢谢


#include<iostream>
using namespace std;
struct LNode
{
    int data;
    LNode* next;
};
struct LinkQueue
{
    LNode* rear;
};

bool InitQueue(LinkQueue Q)
{    
    LNode* H;
    H = new LNode;
    H->next = NULL;
    if (!H)
        return false;
    Q.rear = H;
    Q.rear->next = Q.rear;
    H->data = char("空");
    return true;
}