链结点的定义,链式存储结构由结点相连而成, class Qnode { public: int data = 0; Qnode* next = nullptr; }; 构造函数,实现链队列的初始化,把初始化时的队列当作空队列,第一个结点不作数据存储 析构函数,回收链队申请的内存 class Linkqueue { public: Qnode* front;//队头指针 Qnode* rear;//队尾指针 Linkqueue() { front = new Qnode(); rear = front; } ~Linkqueue() { while (front) { rear = front->next; delete front; front = rear; } } void inQueue(int value); void outQueue(int &value); }; 入队,在队尾入队 void Linkqueue::inQueue(int value) { rear->next = new Qnode(); rear = rear->next; rear->data = value; } 出队,从队头出队 void Linkqueue::outQueue(int &value) { if (!front->next) { cout << "队空了,不能出队" << endl; return; } Qnode* p = front->next; front->next = p->next; value = p->data; delete p; }