#include #define ERROR 0 #define OK 1 using namespace std; typedef int Status; typedef float QElemType; typedef struct NQ { float data; struct NQ* next; } NQ, * Queneptr; typedef struct { Queneptr front; Queneptr rear; } LinkQueue; Status InitQueue(LinkQueue& Q) { Q.front = Q.rear = new NQ; Q.front->next = NULL; return OK; } Status EnQueue(LinkQueue& Q, QElemType e) { Queneptr p; p = new NQ; p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } QElemType GetHead(LinkQueue& Q) { if (Q.rear == Q.front) return ERROR; return Q.front->next->data; } bol DeQueue(LinkQueue& Q, QElemType s) { if (Q.front == Q.rear) { return ERROR; } s = Q.front->next->data; Queneptr p; p = Q.front->next; Q.front->next = p->next; delete p; return OK; }

QElemType s; int main() { LinkQueue lq; InitQueue(lq); //初始化链队 QElemType s; int i; // 修改输入部分 float input[] = { 2.2, 3.3, 4.4, 5.5, 6.6 }; for (i = 0; i < 5; i++) { s = input[i]; EnQueue(lq, s); //入队 } cout << GetHead(lq) << endl; //取队头元素 DeQueue(lq, s); //出队 cout << "Dequeue:" << s << endl; for (i = 0; i < 5; i++) { if (DeQueue(lq, s)) cout << "Dequeue:" << s << endl; else cout << "Queue empty!" << endl; }

return 0;

}

C++ 链队列实现及修改示例:输出2.2 3.3 4.4 5.5 6.6

原文地址: https://www.cveoy.top/t/topic/cO4U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录