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. [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter

    Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...

  2. Hibernate的延迟检索和立即检索

    一.立即检索 所谓立即检索就是立即装载和初始化检索方法指定的对象,即使Session关闭了,依然可以正常访问.立即检索策略的启用是通过在映射配置文件中将lazy实行值设置为false实现的. 通俗讲就 ...

  3. js判断是对象还是集合

    /*============================================ 函数功能:对返回数据中的列表数据进行非空处理 ============================== ...

  4. ES不设置副本是非常脆弱的,整个文章告诉了你为什么

    Delaying Shard Allocation As discussed way back in Scale Horizontally, Elasticsearch will automatica ...

  5. nyoj--37--回文字符串(动态规划)

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然, ...

  6. mysql表空间传输(ERROR 1808) row_format设置

    文章结构如下: 从MYSQL5.6版本开始,引入了传输表空间这个功能,可以把一张表从一个数据库移到另一个数据库或者机器上.迁移的时候很方便,尤其是大表. 由于本次达到测试使用版本5.6.38传到5.7 ...

  7. 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master" "slave1"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)

    不多说,直接上干货! 前期博客 apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)(非HA和HA)  问题详情 org.apache.storm.utils.Nimbu ...

  8. github上下载开源项目

    1.首先获取到设置信息 2.找到克隆的路径(本步骤不包含下载.zip的方法) 3.打开 Git Shell ->输入: cd ../../ ->输入: cd  想要安装的路径 ->输 ...

  9. JavaScript学习——使用JS完成注册页面表单校验

    1.步骤分析 第一步:确定事件(onsubmit)并为其绑定一个函数 第二步:书写这个函数(获取用户输入的数据<获取数据时需要在指定位置定义一个 id>) 第三步:对用户输入的数据进行判断 ...

  10. 分库分表中间件Sharding-JDBC

    数据库分库分表从互联网时代开启至今,一直是热门话题.在NoSQL横行的今天,关系型数据库凭借其稳定.查询灵活.兼容等特性,仍被大多数公司作为首选数据库.因此,合理采用分库分表技术应对海量数据和高并发对 ...