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 rear
How 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 ...
随机推荐
- android-基础编程-ToolBar
Android 3.0 Android 推了 ActionBar 这个控件,而到了2013 年 (4.0)Google 开始大力地推动所谓的 android style,material desig ...
- Hibernate 和 Mybatis 两者相比的优缺点
1.开发上手难度 hibernate的真正掌握(封装的功能和特性非常多)要比Mybatis来得难. 在真正产品级应用上要用Hibernate,不仅对开发人员的要求高,hibernate往往还不适合(多 ...
- Xutils简
//解析 private void myinitData() { RequestParams parms=new RequestParams("http://huixinguiyu.cn/A ...
- SOPC与 hello world
本次设计实验源码位于:http://download.csdn.net/detail/noticeable/9922865 实验目的:通过系统的搭建进一步了解FPGA的SOPC开发流程,并借此了姐pl ...
- zookeeper学习day01
1.zkAPI:(借助闭锁来实现) 1)创建闭锁对象 2)创建zk对象 3)连接zk客户端(连接成功执行countDown方法) 4)执行await方法(保证链接成功) 5)zk对象调用对 ...
- RabbitMQ 常用操作
RabbitMQ简介 1.首先安装erlang rpm -Uvh https://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos ...
- XCode 设置自定义环境变量
XCode 设置自定义环境变量 Product -> Scheme -> Edit Scheme -> 之后设置环境变量.
- 【安全狗SRC】抗D设备哪家强?你来!大佬告诉你答案
上周,安全狗SRC联合SRC部落,携手推出了爆款话题:传统抗D设备 vs 新兴CDN抗D:抗D效果哪个好? 一经发布简直好评如潮,热评无数,四方雷动(?)原帖在此,错过的吃瓜表哥们可以再围观一下~ht ...
- 2.SlidingMenu(侧边栏效果)
> 使用步骤库:别的程序可以用它的方法.图片. 下载的其中一个框架的例子是没有actionBar的,example_update 引入出错可能是俩个v4包冲突了,删掉工程里的一个,不要删了库里的 ...
- WCF绑定netTcpBinding寄宿到IIS
继续沿用上一篇随笔中WCF服务类库 Wettery.WcfContract.Services WCF绑定netTcpBinding寄宿到控制台应用程序 服务端 添加WCF服务应用程序 Wettery. ...