用c++写一个链队列
来源:互联网 发布:java redis使用教程 编辑:程序博客网 时间:2024/06/16 15:49
1.首先要对队列中的每个节点进行定义。每个节点含有两项:储存的数据和指向下一个节点的指针。定义如下:
struct Node {int data;Node *next;};2.接下来要对链队列进行类定义。如下:
class LinkQueue {private:Node *front; //队头指针Node *real; //队尾指针public:LinkQueue(); //构造函数,创建一个空队列~LinkQueue(); //析构函数,清除队列void EnterQueue(int obj); //将obj入队int DelQueue(); //将队头元素出队int GetHead(); //获取队头元素的值void DisplayQueue(); //遍历整个队列};其中front节点和real节点分别表示队头指针和队尾指针。
3.各个函数的具体实现如下所示:
vLinkQueue::LinkQueue() { //构造函数,创建一个空队列front = new Node;front->next = NULL;real = front;}LinkQueue::~LinkQueue() { //析构函数,清除队列Node *temp;if (front != NULL) {temp = front;front = front->next;delete temp;}}void LinkQueue::EnterQueue(int obj){ //入队操作Node *temp;temp = new Node;temp->data = obj;temp->next = real->next;real->next = temp;real = temp;if (front->next == NULL) {front->next = temp;}}int LinkQueue::DelQueue(){ //出队操作int e;if (front == real) {cout << "error:下溢";return -1; }else {Node *temp;temp = front->next;e=temp->data;front->next = temp->next;if (temp->next = NULL)real = front;delete temp;return e;}}int LinkQueue::GetHead(){ //获取队头的值int e;if (real == front) {cout << "error:该队列为空队列";return -1;}else {e = front->next->data;cout << e;return e;}}void LinkQueue::DisplayQueue(){ //遍历整个队列Node *temp;temp = new Node;if (real != front) {cout << "该栈的遍历依次为:";for (temp = front->next; temp->next != NULL; temp = temp->next){cout << temp->data << " ";}cout << real->data<<"\n";}else cout << "该栈为空";}4.测试各个函数,测试程序如下:
int main() { //测试程序LinkQueue linkqueue;linkqueue.EnterQueue(2);linkqueue.EnterQueue(4);linkqueue.EnterQueue(3);linkqueue.EnterQueue(5);linkqueue.DisplayQueue(); //测试入队函数和遍历函数linkqueue.GetHead(); //测试出队函数和获取队头元素函数linkqueue.DelQueue();linkqueue.DisplayQueue();}测试结果如下:
阅读全文
0 0
- 用c++写一个链队列
- C语言:写一个循环队列
- 用java语言写一个环形队列
- 用c++11写的一个线程安全的队列
- 用c写的一个栈的链表实现
- 用队列写贪吃蛇(C语言版)
- 用C写的一个扫描器源码
- 用C写的一个扫描器源码
- 【用C语言写一个内存管理程序】
- 用C写的一个扫描器源码
- 用C写一个自动关机程序
- 一个用c写的cgi库
- 用纯c写一个类
- 用C写一个简单病毒
- 用C语言写的一个万年历
- 用c/c++写一个堆栈管理程序
- 用C语言写一个的计算器
- 用C写一个简单病毒
- LookupAccountName函数接受系统和帐户的名称作为输入检索帐户的安全标识符(SID)和找到该帐户的域的名称
- android自定义jar包之合并jar包
- BZOJ 4094 [Usaco2013 Dec]Optimal Milking 线段树
- 6.0动态权限
- iDEA启动后报Error:Abnormal build process termination
- 用c++写一个链队列
- 每日杂谈 -- 2017.10.16
- Android——四大组件之一:BroadcastReceiver 广播接收器
- mc_pos_control.cpp 之 control_offboard(dt)
- Spring MVC 使用支付宝接口DEMO
- android 怎么跳转直接到qq群
- 不常用系统命令
- 解决Android单个dex文件不能超过65536个方法问题,DexIndexOverflowException
- 基于 CentOS 搭建 WordPress