Question:

I'm having trouble understanding this line:

rear->next = temp;

in this queue function:

 void Queue::enqueue(int data) {

    Node *temp = new Node();    // make a temporary node
temp->info = data; // assign passed in data to it
temp->next = 0; // make it point to null if(front == 0) // if there is no front node
front = temp; // make this a front node else // else, if there is already a front node
rear->next = temp; // make this rear's next pointer???? why? rear = temp; // in any case make this a rear node }

Wouldn't it make more sense to do it like this?

    else                    // else, if there is already a front node
temp->next = rear; // make temp point to REAR; not other way around rear = temp; // make temp a new rear node Answer:

The rear points to the last element. What is wanted is to place temp after the current rear, and then move rear to point to the newly placed last element. So, if we were wanting to enqueue 4 to the queue (1, 2, 3), we want:

1 -> 2 -> 3 -> 4
| |
front rear

Your solution lets temp cut in front of the current rear, and then moves rear to the cut position. It doesn't even cut properly, since the item before the rear is still pointing to the original rear. The rear isn't pointed to the last item anymore, and your queue would thus be in an inconsistent state.

1 -> 2 -> 3
| 4 -^
| |
front rear

How does this enqueue function work?的更多相关文章

  1. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  2. javascript数据结构与算法---队列

    javascript数据结构与算法---队列 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据,先进先出,这点和栈不一样(后入先出).在栈中,最后入栈的元素 ...

  3. javascript数据结构-优先队列

    这里之所以扩充一个 有限队列 是因为,生活使用中队列通常会附加优先级,比如排队买票,一般老人和军人等会有优先权限. 实现:继承上篇的 普通队列实现.这里用一种方法,入队的时候,进行排序插入到指定位置, ...

  4. javascript数据结构-队列

    gihub博客地址 队列(Queue)是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插 ...

  5. 对于amqplib的使用心得

    最近在nodejs使用了amqplib--rabbitmq的nodejs客户端.封装在了express中,先来代码. var amqp = require('amqplib/callback_api' ...

  6. javascript数据结构和算法

    一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...

  7. Javascript中的队列

    队列遵循FIFO (First In First Out)原则. 普通队列 function Queue() { var items=[]; //向队列尾部添加一个或者多个元素 this.enqueu ...

  8. 常见数据结构之JavaScript实现

    常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...

  9. Java Attach API

    catalog . instrucment与Attach API . BTrace: VM Attach的两种方式 . Sun JVM Attach API 1. instrucment与Attach ...

随机推荐

  1. C++顺序容器之list初探

    C++顺序容器之list初探 双向链表,支持双向顺序访问.在list中任何位置进行插入和删除速度都很快. list不支持随机访问,为了访问一个元素,必须遍历整个容器. #include<iost ...

  2. 一个友盟BUG的思考和分析:Invalid update

    1.友盟错误信息 Invalid update: invalid number of rows . The number of rows contained ) must be equal to th ...

  3. 详解Android数据存储技术

    前言 学习Android相关知识,数据存储是其中的重点之一,如果不了解数据,那么让你跟一款没有数据的应用玩,你能玩多久呢?答案是这和没有手机几乎是差不多的.我们聊QQ,聊微信,看新闻,刷朋友圈等都是看 ...

  4. Python--Click

    Click Click 是 Flask 的开发团队 Pallets 的另一款开源项目,它是用于快速创建命令行的第三方模块. 我们知道,Python 内置了一个 Argparse 的标准库用于创建命令行 ...

  5. 【LeetCode】7. 整数反转

    题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1:输入: 123 输出: 321  示例 2:输入: -123 输出: -321 示例 3:输入: 120 输出 ...

  6. [原创]Struts2奇葩环境任意文件上传工具(解决菜刀无法传文件或上传乱码等问题)

    上面这问题问得好  1 不知道大家有没碰到有些Strus2站点  上传JSP后访问404 或者503    注意我说的是404或503不是403(要是403换个css/img等目录或许可以)    但 ...

  7. odoo开发笔记 -- 后台日志输出及分析

    odoo开发笔记 -- 后台日志输出及分析 附:日志分析软件

  8. springboot属性注入转化为对象

    第一种方式:用spel表达式解析 @Value("#{ T(com.alibaba.fastjson.JSON).parseObject('${train.purchase}')}" ...

  9. maven多环境参数配置

    maven中properties加载顺序 <build><filters></filters></build>中的配置 pom.xml中的<pro ...

  10. GO入门——7. 并发

    1 并发 1.1 goroutine Goroutine 奉行通过通信来共享内存,而不是共享内存来通信 goroutine 只是由官方实现的超级"线程池"而已. 每个实例 4-5K ...