How does this enqueue function work?
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 rearHow does this enqueue function work?的更多相关文章
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- javascript数据结构与算法---队列
javascript数据结构与算法---队列 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据,先进先出,这点和栈不一样(后入先出).在栈中,最后入栈的元素 ...
- javascript数据结构-优先队列
这里之所以扩充一个 有限队列 是因为,生活使用中队列通常会附加优先级,比如排队买票,一般老人和军人等会有优先权限. 实现:继承上篇的 普通队列实现.这里用一种方法,入队的时候,进行排序插入到指定位置, ...
- javascript数据结构-队列
gihub博客地址 队列(Queue)是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插 ...
- 对于amqplib的使用心得
最近在nodejs使用了amqplib--rabbitmq的nodejs客户端.封装在了express中,先来代码. var amqp = require('amqplib/callback_api' ...
- javascript数据结构和算法
一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...
- Javascript中的队列
队列遵循FIFO (First In First Out)原则. 普通队列 function Queue() { var items=[]; //向队列尾部添加一个或者多个元素 this.enqueu ...
- 常见数据结构之JavaScript实现
常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...
- Java Attach API
catalog . instrucment与Attach API . BTrace: VM Attach的两种方式 . Sun JVM Attach API 1. instrucment与Attach ...
随机推荐
- Reading | 《Python基础教程》第1次阅读
目录 一.基础知识 1.数和表达式 浮点除法和整数除法 负数整除和取余 圆整 乘方运算 2.变量名 3.获取用户输入 4.模块 5.让脚本像普通程序一样 6.字符串 单.双引号 引号的转义 字符串拼接 ...
- pgsqls修改表字段长度
alter table T_RPACT_PROTO_EDIT_RECORD alter column remark type VARCHAR(1024); 需要注意type关键字
- setAttribute的浏览器兼容性(转)
1.element要用getElementById or ByTagName来得到, 2.setAttribute("class", vName)中class是指改变"c ...
- MySQL:索引
索引的目的在于提高查询效率,它的作用就相当于一本书的目录: 1. 常见的索引模型 1.1 哈希表 优点:适用于等值查询的场景: 缺点:范围查询效率较低: 1.2 有序数组 优点:范围查询和等值查询效率 ...
- MySQL:binlog 和 redo log
[参考文章]:MySQL中Redo与Binlog顺序一致性问题? [参考文章]:极客时间 1. 数据更新时的日志处理流程 1.1 redo log(prepare状态) 此时SQL已经成功执行了,已经 ...
- Thread-方法以及wait、notify简介
Thread.sleep()1.静态方法是定义在Thread类中.2.Thread.sleep()方法用来暂停当前执行的线程,将CPU使用权释放给线程调度器,但不释放锁(也就是说如果有synchron ...
- JS闭包与JS函数
先说说在网上看到的一个闭包案例: var add = (function () {var counter = 0;return function () {return counter += 1;}}) ...
- Django--Ajax 提交
一 什么是Ajax AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传 ...
- glide使用
glide在github地址:https://github.com/bumptech/glide 1.添加依赖(最好看官网,因为有时候添加依赖的会有轻微改动,版本也会一直更新) repositorie ...
- unity5.5 ugui使用美术字
文件转载自:http://www.jianshu.com/p/a4e6d1ca3ca0 项目需要使用美术字加强战斗效果表现,按以往NGUI的使用经验,这个应该很简单,随便Google一下都有数篇技术博 ...