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. 宿主机mac os无法连接到虚拟机centos

    宿主机: Mac OS 10.9.2 虚拟机: [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final) [root ...

  2. mydumper安装及安装故障汇总

     mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具,备份方式术语逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性. 因此该工具是DBA们的不二选 ...

  3. 使用XMLHttpRequest解析json

    不适用内函数或者promise的方式,可以在外部提取到json数据 <!DOCTYPE html> <html lang="en"> <head> ...

  4. android drawable资源调用使用心得

    1. 调用顺序 android 调用应用图片资源时,会优先选择当前手机屏幕dpi对应的的文件夹(如drawable-ldpi, drawable-mdpi, drawable-hdpi, drawab ...

  5. linux下面增加磁盘空间

    1.先看看情况 [root@localhost tmp]# fdisk -l Disk /dev/sda: 3221 MB, 3221225472 bytes 255 heads, 63 sector ...

  6. vc应用CPictureEx类(重载CStatic类)加载gif动画

    1.PictureEx.h文件: //////////////////////////////////////////////////////////////////////// PictureEx. ...

  7. Linux就该这么学 20181007第十章Apache)

    参考链接https://www.linuxprobe.com/ /etc/httpd/conf/httpd.conf 主配置文件 SElinux域 ---服务功能的限制 SElinux安全上下文 -- ...

  8. Qt开发环境的搭建

    首先讲讲为什么要用Qt这个东东吧!用了以后才知道,这门语言真的很不错,我权当把它当成了类库来用,Linux本身的C语言编程是很烦的,比如说串口编程,虽说C编程也不难,但是使用Qt这种封装的类库来操作的 ...

  9. 乌班图 之 apt命令 及 VMware共享文件夹

    apt是Advanced Packaging Tool ,是Ubuntu下的一个安装包管理工具 大部分软件的安装.更新.卸载 都是利用apt命令来实现 直接在终端输入apt即可查阅命令的帮助信息 常用 ...

  10. Hibernate框架学习(二)——api详解

    一.Configuration对象 功能:配置加载类,用于加载主配置,orm元数据加载. //1.创建,调用空参构造(还没有读配置文件) Configuration conf=new Configur ...