sicily 1000. LinkedList
Description
template <typename E>
Hint
链表的插入使用头插法,只需提交模板类函数的实现即可,不需要提交main函数,如下列代码所示:
template <typename E>
LinkedList<E>::LinkedList(const LinkedList & that) {
Node* current = ;
Node* node = that.first;
while (node != ) {
if (current == ) current= first = new Node();
else {
current->next = new Node();
current = current->next;
}
current->data = node->data;
current->next = ;
node = node->next;
}
} template <typename E>
LinkedList<E>& LinkedList<E>::operator= (const LinkedList & that) {
LinkedList<E> tmp(that);
while (first != ) removeFirst();
Node* current = ;
Node* node = tmp.first;
while (node != ) {
if (current == ) current= first = new Node();
else {
current->next = new Node();
current = current->next;
}
current->data = node->data;
current->next = ;
node = node->next;
}
return *this;
} template <typename E>
void LinkedList<E>::removeFirst()
{
Node * node = first;
first = node->next;
delete node;
} template <typename E>
void LinkedList<E>::addFirst(E data) {
Node* newFirst = new Node();
newFirst->data = data;
newFirst->next = first;
first = newFirst;
}
测试代码:
template <typename E>
class LinkedList
{
private: // inner class: linked-list node
class Node
{
public:
E data;
Node * next;
}; Node * first; public:
LinkedList() {
first = ;
} ~LinkedList() {
while (first != ) {
removeFirst();
}
} E getFirst() {
return first->data;
} bool isEmpty() {
return first == ;
} // TODO:
LinkedList(const LinkedList & that);
LinkedList & operator= (const LinkedList & that);
void removeFirst() ;
void addFirst(E data);
}; /*template <typename E>
LinkedList<E>::LinkedList(const LinkedList<E> & that)
{ } template <typename E>
LinkedList<E> & LinkedList<E>::operator= (const LinkedList<E> & that)
{ } template <typename E>
void LinkedList<E>::removeFirst() {
Node * node = first;
first = node->next;
delete node;
} template <typename E>
void LinkedList<E>::addFirst(E data)
{ } */ //#include "source.cpp" #include <iostream>
using namespace std; LinkedList<double> read() {
LinkedList<double> list;
for (int i = ; i < ; ++ i) {
double value;
cin >> value;
list.addFirst(value);
}
return list;
} void removeAndPrintAll(LinkedList<double> list) {
while (! list.isEmpty()) {
cout << list.getFirst() << endl;
list.removeFirst();
}
} int main() {
LinkedList<double> list = read();
LinkedList<double> list2;
list2 = list;
removeAndPrintAll(list2);
}
sicily 1000. LinkedList的更多相关文章
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- java中三种for循环之间的对比
普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac ...
- LinkedList竟然比ArrayList慢了1000多倍?(动图+性能评测)
数组和链表是程序中常用的两种数据结构,也是面试中常考的面试题之一.然而对于很多人来说,只是模糊的记得二者的区别,可能还记得不一定对,并且每次到了面试的时候,都得把这些的概念拿出来背一遍才行,未免有些麻 ...
- To Java程序员:切勿用普通for循环遍历LinkedList
ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- arraylist与linkedlist的区别与性能测试
/** *arraylist和linkedlist的适用场合. **/ import java.util.List; import java.util.ArrayList; import java.u ...
- Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列
在这里我们介绍一下最简单的链表LinkedList: 看一下add()方法: public boolean add(E e) { linkLast(e); return true; } void li ...
- 你真的说的清楚ArrayList和LinkedList的区别吗
参见java面试的程序员,十有八九会遇到ArrayList和LinkedList的区别?相信很多看到这个问题的人,都能回答个一二.但是,真正搞清楚的话,还得花费一番功夫. 下面我从4个方面来谈谈这个问 ...
- Java基础-ArrayList和LinkedList的区别
大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为Lin ...
随机推荐
- iOS-UIImage imageWithContentsOfFile 和 imageName 对照
1.imageWithContentsOfFile NSString *imagePath = [NSString stringWithFormat:@"%@/%@",[[NSBu ...
- C++语言笔记系列之十八——虚函数(1)
1.C++中的多态 (1)多态性:同一个函数的调用能够进行不同的操作,函数重载是实现多态的一种手段. (2)联编:在编译阶段进行联接.即是在编译阶段将一个函数的调用点和函数的定义点联接起来. A.静态 ...
- Ubuntu 16.04 安装 Open Jdk
sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-get update sudo apt-get install openjdk-7-jdk
- rsync来传输文件(可断点续传)
scp传文件的话如果出错就得重新来过, 用rsync可以实现断点上传的功能 大概就是这样用: rsync -P --rsh=ssh home.tar 192.168.205.34:/home/h ...
- BZOJ 2301 莫比乌斯函数+分块
思路: 同BZOJ1101 就是加个容斥 - http://blog.csdn.net/qq_31785871/article/details/54340241 //By SiriusRen #inc ...
- Ubuntu16.04下Mongodb官网卸载部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 前期博客 Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐) https://docs.mongodb.com/manual/tutorial/ins ...
- 我网站用session做的登录,为什么清除浏览器数据后还是得重新登录?session是存在服务器上的。
答案一: 你清除了浏览器数据,相当于把cookie也清了,那么你的sessionId也就没有了,所以你再次请求的时候服务器无法根据你携带的sessionid来获取对应的session,所以说需要重新登 ...
- 《汇编语言(第三版)》pushf 和 popf 指令,以及标志寄存器在 Debug 中的表示
pushf 和 popf pushf 的功能是将标志寄存器的值压栈,而 popf 是从栈中弹出数据,输入标志寄存器. pushf 和 popf,为直接访问寄存器提供了方法. 格式 pushf popf ...
- 【原创】查询占CPU高的oracle进程
1:首先使用TOP命令传到占用CPU高的SPID号 PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND3575 oracle 1 12 ...
- 忘记了本地mysql密码应该怎么找回
1. 首先以系统管理员身份登陆系统 2. 打开命令行窗口停止mysql服务 C:\>net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止 3.到mys ...