Description

template <typename E>

class LinkedList
{
private:
 
  // inner class: linked-list node
  class Node
  {
  public:
    E data;
    Node * next;
  };
 
  Node * first;
 
public:
  LinkedList() {
    first = 0;
  }
 
  ~LinkedList() {
    while (first != 0) {
      removeFirst();
    }
  }
 
  E getFirst() {
    return first->data;
  }
 
  bool isEmpty() {
    return first == 0;
  }
 
// 实现下列4个函数:
  LinkedList(const LinkedList & that);
  LinkedList & operator= (const LinkedList & that);
  void removeFirst() ;
  void addFirst(E data);
};

Hint

链表的插入使用头插法,只需提交模板类函数的实现即可,不需要提交main函数,如下列代码所示:

template <typename E>
void LinkedList<E>::removeFirst()
{
    Node * node = first;
    first = node->next;
    delete node;
}
 
代码如下:
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的更多相关文章

  1. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  2. java中三种for循环之间的对比

    普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac ...

  3. LinkedList竟然比ArrayList慢了1000多倍?(动图+性能评测)

    数组和链表是程序中常用的两种数据结构,也是面试中常考的面试题之一.然而对于很多人来说,只是模糊的记得二者的区别,可能还记得不一定对,并且每次到了面试的时候,都得把这些的概念拿出来背一遍才行,未免有些麻 ...

  4. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  5. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  6. arraylist与linkedlist的区别与性能测试

    /** *arraylist和linkedlist的适用场合. **/ import java.util.List; import java.util.ArrayList; import java.u ...

  7. Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列

    在这里我们介绍一下最简单的链表LinkedList: 看一下add()方法: public boolean add(E e) { linkLast(e); return true; } void li ...

  8. 你真的说的清楚ArrayList和LinkedList的区别吗

    参见java面试的程序员,十有八九会遇到ArrayList和LinkedList的区别?相信很多看到这个问题的人,都能回答个一二.但是,真正搞清楚的话,还得花费一番功夫. 下面我从4个方面来谈谈这个问 ...

  9. Java基础-ArrayList和LinkedList的区别

    大致区别:  1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为Lin ...

随机推荐

  1. Oracle性能分析1:开启SQL跟踪和获取trace文件

    当Oracle查询出现效率问题时,我们往往须要了解问题所在,这样才干针对问题给出解决方式.Oracle提供了SQL运行的trace信息,当中包括了SQL语句的文本信息.一些运行统计,处理过程中的等待, ...

  2. PostgreSQL hstore 列性能提升一例

    PostgreSQL 支持hstore 来存放KEY->VALUE这类数据, 事实上也相似于ARRAY或者JSON类型.  要高效的使用这类数据,当然离不开高效的索引.我们今天就来看看两类不同的 ...

  3. 使用malloc分别分配2KB的空间,然后用realloc调整为6KB的内存空间,打印指针地址

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...

  4. c3p0在spring中的配置

    在大家的开发和学习其中应该经经常使用到数据库的连接和使用,只是连接 的方式就有非常多种方式了,例如说用最最简单的JDBC 也好,还实用比 较复杂一点的就是数据库连接池.当然还有使用DBCP的连接的,各 ...

  5. bzoj4519: [Cqoi2016]不同的最小割(分治最小割)

    4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...

  6. CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。

    官方参数解释: Convolution 2D tflearn.layers.conv.conv_2d (incoming, nb_filter, filter_size, strides=1, pad ...

  7. javascript系列-class10.DOM(下)

    1.node节点(更详细的获取(设置)页面中所有的内容)         根据 W3C 的 HTML DOM 标准,HTML 文档中的所有内容都是节点:   元素是节点的别称,节点包含元素当然节点还有 ...

  8. 5分钟学会 CSS Grid 布局

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 这是一篇快速介绍网站未来布局的文章. Grid 布局是网站设计的基础,CSS Grid 是创建网格布局最强大和最简单的工 ...

  9. 91.Bower : ENOGIT git is not installed or not in the PATH 解决方法

    转自:https://www.haorooms.com/post/bower_error 今天在用bower安装依赖的时候,出现了Bower : ENOGIT git is not installed ...

  10. nginx的location 匹配的规则问题

    正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...